Skip to content

Commit

Permalink
Merge pull request #213 from tigrisdata/main
Browse files Browse the repository at this point in the history
beta release
  • Loading branch information
adilansari authored Jan 18, 2023
2 parents 45db10a + 610a140 commit 4050d6a
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 88 deletions.
2 changes: 0 additions & 2 deletions src/__tests__/test-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ export class TestTigrisService {
assert(call.request.getBranch() === TestTigrisService.ExpectedBranch);

const reply: CommitTransactionResponse = new CommitTransactionResponse();
reply.setStatus("committed-test");
callback(undefined, reply);
},
createProject(
Expand Down Expand Up @@ -604,7 +603,6 @@ export class TestTigrisService {
assert(call.request.getBranch() === TestTigrisService.ExpectedBranch);

const reply: RollbackTransactionResponse = new RollbackTransactionResponse();
reply.setStatus("rollback-test");
callback(undefined, reply);
},
update(
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/tigris.rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { SearchIterator } from "../consumables/search-iterator";
import { CacheService } from "../proto/server/v1/cache_grpc_pb";
import { DatabaseBranchError } from "../error";
import { Status } from "@grpc/grpc-js/build/src/constants";
import { Status as TigrisStatus } from "../constants";

describe("rpc tests", () => {
let server: Server;
Expand Down Expand Up @@ -326,7 +327,7 @@ describe("rpc tests", () => {
},
});
updatePromise.then((value) => {
expect(value.status).toBe('updated: {"id":1}, {"$set":{"title":"New Title"}}');
expect(value.status).toBe(TigrisStatus.Updated);
expect(value.modifiedCount).toBe(1);
});
return updatePromise;
Expand Down Expand Up @@ -569,7 +570,7 @@ describe("rpc tests", () => {
beginTxPromise.then((session) => {
const commitTxResponse = session.commit();
commitTxResponse.then((value) => {
expect(value.status).toBe("committed-test");
expect(value.status).toBe(TigrisStatus.Success);
});
return beginTxPromise;
});
Expand All @@ -582,7 +583,7 @@ describe("rpc tests", () => {
beginTxPromise.then((session) => {
const rollbackTransactionResponsePromise = session.rollback();
rollbackTransactionResponsePromise.then((value) => {
expect(value.status).toBe("rollback-test");
expect(value.status).toBe(TigrisStatus.Success);
});
});
return beginTxPromise;
Expand Down
5 changes: 2 additions & 3 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class Cache {
if (error) {
reject(error);
} else {
resolve(new CacheSetResponse(response.getStatus(), response.getMessage()));
resolve(new CacheSetResponse(response.getMessage()));
}
});
});
Expand Down Expand Up @@ -117,13 +117,12 @@ export class Cache {
if (response.getOldValue() !== undefined && response.getOldValue_asU8().length > 0) {
resolve(
new CacheGetSetResponse(
response.getStatus(),
response.getMessage(),
Utility._base64DecodeToObject(response.getOldValue_asB64(), this._config)
)
);
} else {
resolve(new CacheGetSetResponse(response.getStatus(), response.getMessage()));
resolve(new CacheGetSetResponse(response.getMessage()));
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class Collection<T extends TigrisCollectionType> implements ICollection {
response.getMetadata().getCreatedAt(),
response.getMetadata().getUpdatedAt()
);
resolve(new UpdateResponse(response.getStatus(), response.getModifiedCount(), metadata));
resolve(new UpdateResponse(response.getModifiedCount(), metadata));
}
});
});
Expand Down Expand Up @@ -354,7 +354,7 @@ export class Collection<T extends TigrisCollectionType> implements ICollection {
response.getMetadata().getCreatedAt(),
response.getMetadata().getUpdatedAt()
);
resolve(new DeleteResponse(response.getStatus(), metadata));
resolve(new DeleteResponse(metadata));
}
});
});
Expand Down
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum Status {
Created = "created",
Updated = "updated",
Deleted = "deleted",
Dropped = "dropped",
Success = "success",
Set = "set",
}
31 changes: 16 additions & 15 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ const BeginTransactionMethodName = "/tigrisdata.v1.Tigris/BeginTransaction";
* ```
* {
* name: "my_database_branch",
* isTemplated: false
* dynamicCreation: false
* }
* ```
* @example A dynamically generated branch name "my_db_${GIT_BRANCH}" would translate to:
* ```
* export GIT_BRANCH=feature_1
* {
* name: "my_db_feature_1",
* isTemplated: true
* dynamicCreation: true
* }
* ```
*/
Expand All @@ -70,7 +70,7 @@ const NoBranch: TemplatedBranchName = { name: "", dynamicCreation: false };
*/
export class DB {
private readonly _db: string;
private _branchVar: TemplatedBranchName;
private _branch: string;
private readonly grpcClient: TigrisClient;
private readonly config: TigrisClientConfig;
private readonly schemaProcessor: DecoratedSchemaProcessor;
Expand Down Expand Up @@ -107,8 +107,7 @@ export class DB {
this.config = config;
this.schemaProcessor = DecoratedSchemaProcessor.Instance;
this._metadataStorage = getDecoratorMetaStorage();
// TODO: Should we just default to `main` or empty arg or throw an exception here?
this._branchVar = Utility.branchNameFromEnv(config.branch) ?? NoBranch;
this._branch = NoBranch.name;
this._ready = this.initializeDB();
}

Expand All @@ -124,27 +123,29 @@ export class DB {
* @private
*/
private async initializeDB(): Promise<this> {
if (this._branchVar.name === NoBranch.name) {
const branchVar = Utility.branchNameFromEnv(this.config.branch) ?? NoBranch;
if (branchVar.name === NoBranch.name) {
return this;
}
const description = await this.describe();
const branchExists = description.branches.includes(this.branch);
const branchExists = description.branches.includes(branchVar.name);

if (!branchExists) {
if (this._branchVar.dynamicCreation) {
if (branchVar.dynamicCreation) {
try {
await this.createBranch(this.branch);
await this.createBranch(branchVar.name);
} catch (error) {
if ((error as ServiceError).code !== Status.ALREADY_EXISTS) {
throw error;
}
}
Log.event(`Created database branch: ${this.branch}`);
Log.event(`Created database branch: ${branchVar.name}`);
} else {
throw new DatabaseBranchError(this.branch);
throw new DatabaseBranchError(branchVar.name);
}
}
Log.info(`Using database branch: '${this.branch}'`);
Log.info(`Using database branch: '${branchVar.name}'`);
this._branch = branchVar.name;
return this;
}

Expand Down Expand Up @@ -300,7 +301,7 @@ export class DB {
if (error) {
reject(error);
} else {
resolve(new DropCollectionResponse(response.getStatus(), response.getMessage()));
resolve(new DropCollectionResponse(response.getMessage()));
}
}
);
Expand Down Expand Up @@ -399,7 +400,7 @@ export class DB {
// user code successful
const commitResponse: CommitTransactionResponse = await session.commit();
if (commitResponse) {
resolve(new TransactionResponse("transaction successful"));
resolve(new TransactionResponse());
}
} catch (error) {
// failed to run user code
Expand Down Expand Up @@ -482,7 +483,7 @@ export class DB {
}

get branch(): string {
return this._branchVar.name;
return this._branch;
}

get ready(): Promise<this> {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./session";
export * from "./tigris";
export * from "./types";
export * from "./search/types";
export * from "./constants";
export { Field } from "./decorators/tigris-field";
export { PrimaryKey } from "./decorators/tigris-primary-key";
export { TigrisCollection } from "./decorators/tigris-collection";
Expand Down
4 changes: 2 additions & 2 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Session {
if (error) {
reject(error);
} else {
resolve(new CommitTransactionResponse(response.getStatus()));
resolve(new CommitTransactionResponse());
}
});
});
Expand All @@ -70,7 +70,7 @@ export class Session {
if (error) {
reject(error);
} else {
resolve(new RollbackTransactionResponse(response.getStatus()));
resolve(new RollbackTransactionResponse());
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/tigris.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class Tigris {
if (error) {
reject(error);
} else {
resolve(new DeleteCacheResponse(response.getStatus(), response.getMessage()));
resolve(new DeleteCacheResponse(response.getMessage()));
}
}
);
Expand Down
Loading

0 comments on commit 4050d6a

Please sign in to comment.