Skip to content

Commit

Permalink
Merge pull request #149 from tigrisdata/main
Browse files Browse the repository at this point in the history
Beta release
  • Loading branch information
adilansari authored Oct 31, 2022
2 parents eab0b1a + 49d4e40 commit 369ade2
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"collectCoverage": true,
"coverageDirectory": "coverage",
"coverageProvider": "v8",
"collectCoverageFrom": ["src/*.ts"]
"collectCoverageFrom": ["src/*.ts", "src/consumables/*.ts", "src/utils/*.ts", "src/search/*.ts"]
}
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"copy_api": "copyfiles -u 2 \"./src/proto/**/*.{js,ts}\" \"./dist/proto\"",
"tsc": "tsc && npm run copy_api",
"build": "npm install && npm run prettier-check && npm run protoc && npm run tsc",
"test": "jest --runInBand --coverage",
"test": "jest --runInBand --coverage --silent",
"prettier-check": "npx prettier --check .",
"all": "npm run clean && npm run build && npm run prettier-check && npm run lint && npm run test",
"prepare": "npm run update_api && npm run protoc && npm run tsc",
Expand All @@ -89,6 +89,7 @@
"@types/jest": "^28.1.8",
"@typescript-eslint/eslint-plugin": "^5.35.1",
"@typescript-eslint/parser": "^5.35.1",
"chalk": "4.1.2",
"copyfiles": "^2.4.1",
"eslint": "^8.22.0",
"eslint-config-prettier": "^8.5.0",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/data/models/catalog/orders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const NotSchema = { key: "value" };
22 changes: 22 additions & 0 deletions src/__tests__/data/models/catalog/products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
TigrisCollectionType,
TigrisDataTypes,
TigrisSchema
} from '../../../../types'

export interface Product extends TigrisCollectionType {
id?: number;
title: string;
description: string;
price: number;
}

export const ProductSchema: TigrisSchema<Product> = {
id: {
type: TigrisDataTypes.INT32,
primary_key: { order: 1, autoGenerate: true }
},
title: { type: TigrisDataTypes.STRING },
description: { type: TigrisDataTypes.STRING },
price: { type: TigrisDataTypes.NUMBER }
}
Empty file.
3 changes: 3 additions & 0 deletions src/__tests__/tigris.rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {capture, spy } from "ts-mockito";
describe("rpc tests", () => {
let server: Server;
const SERVER_PORT = 5002;

beforeAll((done) => {
server = new Server();
TestTigrisService.reset();
Expand All @@ -45,9 +46,11 @@ describe("rpc tests", () => {
done();

});

beforeEach(() => {
TestTigrisService.reset();
});

afterAll((done) => {
server.forceShutdown();
done();
Expand Down
82 changes: 82 additions & 0 deletions src/__tests__/utils/manifest-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { canBeSchema, loadTigrisManifest, TigrisManifest } from "../../utils/manifest-loader";
import { TigrisDataTypes } from "../../types";
import { TigrisFileNotFoundError } from "../../error";

describe("Manifest loader", () => {

it("generates manifest from file system", () => {
const schemaPath = process.cwd() + "/src/__tests__/data/models";
const manifest: TigrisManifest = loadTigrisManifest(schemaPath);
expect(manifest).toHaveLength(2);

const expected: TigrisManifest = [{
"dbName": "catalog",
"collections": [{
"collectionName": "products",
"schema": {
"id": { "type": "int32", "primary_key": { "order": 1, "autoGenerate": true } },
"title": { "type": "string" },
"description": { "type": "string" },
"price": { "type": "number" }
},
"schemaName": "ProductSchema"
}]
}, { "dbName": "empty", "collections": [] }];
expect(manifest).toStrictEqual(expected);
});

it("throws error for invalid path", () => {
const schemaPath = "/src/__tests__/data/models";
expect(() => loadTigrisManifest(schemaPath)).toThrow(TigrisFileNotFoundError);
});

const validSchemaDefinitions = [
{ key: { type: "value" } },
{
id: {
type: TigrisDataTypes.INT32,
primary_key: {
order: 1,
autoGenerate: true
}
},
active: { type: TigrisDataTypes.BOOLEAN }
}
];
test.each(validSchemaDefinitions)(
"identifies valid schema definition %p",
(definition) => {
expect(canBeSchema(definition)).toBeTruthy();
}
);

const invalidSchemaDefinitions = [
{ key: "value" },
12,
{
id: {
type: TigrisDataTypes.INT32,
primary_key: {
order: 1,
autoGenerate: true
}
},
active: false
},
{
id: {
key: "value"
}
},
{ type: "string" },
undefined,
null
];

test.each(invalidSchemaDefinitions)(
"identifies invalid schema definition %p",
(definition) => {
expect(canBeSchema(definition)).toBeFalsy();
}
);
});
3 changes: 2 additions & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Utility } from "./utility";
import { Metadata, ServiceError } from "@grpc/grpc-js";
import { Topic } from "./topic";
import { TigrisClientConfig } from "./tigris";
import { Log } from "./utils/logger";

/**
* Tigris Database
Expand Down Expand Up @@ -80,7 +81,7 @@ export class DB {
): Promise<R> {
return new Promise<R>((resolve, reject) => {
const rawJSONSchema: string = Utility._toJSONSchema(name, type, schema);
console.log(rawJSONSchema);
Log.debug(rawJSONSchema);
const createOrUpdateCollectionRequest = new ProtoCreateOrUpdateCollectionRequest()
.setDb(this._db)
.setCollection(name)
Expand Down
16 changes: 16 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@ export class TigrisCursorInUseError extends TigrisError {
return "TigrisCursorInUseError";
}
}

/**
* An error thrown when path is invalid or not found
*
* @public
* @category Error
*/
export class TigrisFileNotFoundError extends TigrisError {
constructor(message) {
super(message);
}

override get name(): string {
return "TigrisFileNotFoundError";
}
}
30 changes: 30 additions & 0 deletions src/tigris.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
import { DB } from "./db";
import { AuthClient } from "./proto/server/v1/auth_grpc_pb";
import { Utility } from "./utility";
import { loadTigrisManifest, TigrisManifest } from "./utils/manifest-loader";
import { Log } from "./utils/logger";

const AuthorizationHeaderName = "authorization";
const AuthorizationBearer = "Bearer ";
Expand Down Expand Up @@ -274,6 +276,34 @@ export class Tigris {
});
});
}

/**
* Automatically provision Databases and Collections based on the directories
* and {@link TigrisSchema} definitions in file system
*
* @param absoluteSchemaPath - Directory location in file system from root
* directory (/) to load schemas from
*/
public async registerSchemas(absoluteSchemaPath: string) {
const manifest: TigrisManifest = loadTigrisManifest(absoluteSchemaPath);

for (const dbManifest of manifest) {
// create DB
const tigrisDb = await this.createDatabaseIfNotExists(dbManifest.dbName);
Log.event(`Created database: ${dbManifest.dbName}`);

for (const coll of dbManifest.collections) {
// Create a collection
const collection = await tigrisDb.createOrUpdateCollection(
coll.collectionName,
coll.schema
);
Log.event(
`Created collection: ${collection.collectionName} from schema: ${coll.schemaName} in db: ${dbManifest.dbName}`
);
}
}
}
}

/**
Expand Down
33 changes: 33 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import chalk from "chalk";

class Logger {
private prefixes = {
debug: chalk.green("debug") + " -",
info: chalk.cyan("info") + " -",
warn: chalk.yellow("warn") + " -",
error: chalk.red("error") + " -",
event: chalk.magenta("event") + " -",
};

public debug(...message: unknown[]) {
console.log(this.prefixes.debug, ...message);
}

public info(...message: unknown[]) {
console.log(this.prefixes.info, ...message);
}

public error(...message: unknown[]) {
console.log(this.prefixes.error, ...message);
}

public warn(...message: unknown[]) {
console.log(this.prefixes.warn, ...message);
}

public event(...message: unknown[]) {
console.log(this.prefixes.event, ...message);
}
}

export const Log = new Logger();
Loading

0 comments on commit 369ade2

Please sign in to comment.