Skip to content

Commit

Permalink
Merge pull request #333 from tigrisdata/main
Browse files Browse the repository at this point in the history
beta release
  • Loading branch information
adilansari authored May 2, 2023
2 parents a803776 + 15b5b82 commit 8a1e391
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pre-release.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: release-test
on:
pull_request_target:
pull_request:
types:
- "opened"
- "reopened"
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/ts-ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
name: ts-ci

on: [push]
on:
pull_request:
types: [opened, synchronize, reopened]
branches:
- main
push:
branches:
- main

jobs:
build:
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ On every `git commit` we check the code quality using prettier and eslint.
# License

This software is licensed under the [Apache 2.0](LICENSE).

# Contributors

Thanks to all the people who contributed!

<a href="https://github.com/tigrisdata/tigris-client-ts/graphs/contributors">
<img src="https://contrib.rocks/image?repo=tigrisdata/tigris-client-ts" />
</a>
2 changes: 1 addition & 1 deletion api/proto
Submodule proto updated from 2459ca to f346cf
25 changes: 24 additions & 1 deletion src/__tests__/test-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TigrisService } from "../proto/server/v1/api_grpc_pb";
import * as grpc from "@grpc/grpc-js";
import { sendUnaryData, ServerUnaryCall, ServerWritableStream } from "@grpc/grpc-js";
import { sendUnaryData, Server, ServerUnaryCall, ServerWritableStream } from "@grpc/grpc-js";
import { v4 as uuidv4 } from "uuid";
import {
BeginTransactionRequest,
Expand All @@ -11,6 +11,8 @@ import {
CollectionMetadata,
CommitTransactionRequest,
CommitTransactionResponse,
CountRequest,
CountResponse,
CreateBranchRequest,
CreateBranchResponse,
CreateOrUpdateCollectionRequest,
Expand Down Expand Up @@ -62,6 +64,8 @@ import * as google_protobuf_timestamp_pb from "google-protobuf/google/protobuf/t
import { Utility } from "../utility";
import { Status } from "@grpc/grpc-js/build/src/constants";
import assert from "assert";
import { Tigris } from "../tigris";
import { Collection } from "../collection";

export class TestTigrisService {
public static readonly ExpectedBranch = "unit-tests";
Expand Down Expand Up @@ -93,6 +97,8 @@ export class TestTigrisService {
],
]);

private myDatabase: any;

public static readonly ALERTS_B64_BY_ID: ReadonlyMap<number, string> = new Map([
// base64 of {"id":1,"text":"test"}
[1, "eyJpZCI6MSwidGV4dCI6InRlc3QifQ=="],
Expand Down Expand Up @@ -190,6 +196,10 @@ export class TestTigrisService {
call: ServerUnaryCall<ReadRequest, ExplainResponse>,
callback: sendUnaryData<ExplainResponse>
): void;
count(
call: ServerUnaryCall<CountRequest, CountResponse>,
callback: sendUnaryData<CountResponse>
): void;
} = {
createBranch(
call: ServerUnaryCall<CreateBranchRequest, CreateBranchResponse>,
Expand Down Expand Up @@ -317,6 +327,7 @@ export class TestTigrisService {
);
callback(undefined, reply);
},

/* eslint-disable @typescript-eslint/no-empty-function */
describeCollection(
call: ServerUnaryCall<DescribeCollectionRequest, DescribeCollectionResponse>,
Expand All @@ -337,6 +348,7 @@ export class TestTigrisService {

callback(undefined, reply);
},

describeDatabase(
call: ServerUnaryCall<DescribeDatabaseRequest, DescribeDatabaseResponse>,
callback: sendUnaryData<DescribeDatabaseResponse>
Expand Down Expand Up @@ -718,6 +730,17 @@ export class TestTigrisService {
reply.setReadType("secondary index");
callback(undefined, reply);
},

count(
call: ServerUnaryCall<CountRequest, CountResponse>,
callback: sendUnaryData<CountResponse>
): void {
assert(call.request.getBranch() === TestTigrisService.ExpectedBranch);

const reply: CountResponse = new CountResponse();
reply.setCount(3);
callback(undefined, reply);
},
};
}

Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/tigris.filters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,33 @@ describe("filters tests", () => {
expect(Utility.filterToString(tigrisFilter)).toBe('{"balance":{"$gte":10}}');
});

it("not Filter", () => {
const tigrisFilter: Filter<Student> = {
name: {
$not: "Jack",
},
};
expect(Utility.filterToString(tigrisFilter)).toBe('{"name":{"$not":"Jack"}}');
});

it("contains Filter(string)", () => {
const tigrisFilter: Filter<Student> = {
name: {
$contains: "Adam",
},
};
expect(Utility.filterToString(tigrisFilter)).toBe('{"name":{"$contains":"Adam"}}');
});

it("regex Filter", () => {
const tigrisFilter: Filter<Student> = {
name: {
$regex: "/andy/i",
},
};
expect(Utility.filterToString(tigrisFilter)).toBe('{"name":{"$regex":"/andy/i"}}');
});

it("logicalFilterTest1", () => {
const logicalFilter: Filter<IUser> = {
$or: [
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/tigris.rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,15 @@ describe("rpc tests", () => {
expect(explainResp.filter).toEqual(JSON.stringify({ author: "Marcel Proust" }));
});

it("count", async () => {
const tigris = new Tigris({ ...testConfig, projectName: "db3" });
const db = tigris.getDatabase();
const countResponse = await db.getCollection<IBook>("books").count({
author: "Marcel Proust",
});
expect(countResponse).toEqual(3);
});

it("describe collection", async () => {
const tigris = new Tigris({ ...testConfig, projectName: "db3" });

Expand Down
35 changes: 35 additions & 0 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
SearchResponse as ProtoSearchResponse,
UpdateRequest as ProtoUpdateRequest,
SearchRequest as ProtoSearchRequest,
CountRequest as ProtoCountRequest,
DescribeCollectionRequest as ProtoDescribeCollectionRequest,
} from "./proto/server/v1/api_pb";
import { Session } from "./session";
Expand Down Expand Up @@ -618,6 +619,40 @@ export class Collection<T extends TigrisCollectionType> implements ICollection {
});
}

/**
* Count the number of documents in a collection
* @returns - the number of documents in a collection
*
* @example
* ```
* const countPromise = db.getCollection<Book>(Book).count();
*
* countPromise
* .then(count: number) => console.log(count);
* .catch( // catch the error)
* .finally( // finally do something)
* ```
*/
count(filter?: Filter<T>): Promise<number> {
if (!filter) {
filter = {};
}
const countRequest = new ProtoCountRequest()
.setProject(this.db)
.setCollection(this.collectionName)
.setBranch(this.branch)
.setFilter(Utility.stringToUint8Array(Utility.filterToString(filter)));

return new Promise((resolve, reject) => {
this.grpcClient.count(countRequest, (err, response) => {
if (err) {
return reject(err);
}
resolve(response.getCount());
});
});
}

/**
* Read a single document from collection.
*
Expand Down
11 changes: 10 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,16 @@ type PathsForFilter<T, P extends string = ""> = {
: `${P}${K & string}`;
}[keyof T];

export type SelectorOperator = "$eq" | "$gt" | "$gte" | "$lt" | "$lte" | "$none";
export type SelectorOperator =
| "$eq"
| "$gt"
| "$gte"
| "$lt"
| "$lte"
| "$not"
| "$regex"
| "$contains"
| "$none";
export type LogicalOperator = "$or" | "$and";

export type SelectorFilter<T> = {
Expand Down

0 comments on commit 8a1e391

Please sign in to comment.