diff --git a/src/__tests__/tigris.rpc.spec.ts b/src/__tests__/tigris.rpc.spec.ts index fe77681..e1e543e 100644 --- a/src/__tests__/tigris.rpc.spec.ts +++ b/src/__tests__/tigris.rpc.spec.ts @@ -8,6 +8,7 @@ import { TigrisCollectionType, TigrisDataTypes, TigrisSchema, + UpdateFields, UpdateQueryOptions, } from "../types"; import { Tigris, TigrisClientConfig } from "../tigris"; @@ -355,7 +356,7 @@ describe("rpc tests", () => { const expectedFilter = { id: 1 }; const expectedCollation: Collation = { case: Case.CaseInsensitive }; - const expectedUpdateFields = { title: "one" }; + const expectedUpdateFields: UpdateFields = { title: "one", $push: { tags: "fiction" } }; const options = new UpdateQueryOptions(5, expectedCollation); const updatePromise = collection.updateOne({ diff --git a/src/__tests__/tigris.search.spec.ts b/src/__tests__/tigris.search.spec.ts index 632f696..b6f28e0 100644 --- a/src/__tests__/tigris.search.spec.ts +++ b/src/__tests__/tigris.search.spec.ts @@ -71,18 +71,6 @@ describe("Search Indexing", () => { }); }); - describe("getIndex", () => { - it("succeeds if index exists", async () => { - const getIndexPromise = tigris.getIndex(SearchServiceFixtures.Success); - return expect(getIndexPromise).resolves.toBeInstanceOf(SearchIndex); - }); - it("fails if index does not exist", async () => { - await expect(tigris.getIndex(SearchServiceFixtures.DoesNotExist)).rejects.toThrow( - "search index not found" - ); - }); - }); - describe("deleteIndex", () => { it("succeeds if index exists", async () => { const deleteResp = await tigris.deleteIndex(SearchServiceFixtures.Success); diff --git a/src/__tests__/tigris.updatefields.spec.ts b/src/__tests__/tigris.updatefields.spec.ts index 2a17192..75e3342 100644 --- a/src/__tests__/tigris.updatefields.spec.ts +++ b/src/__tests__/tigris.updatefields.spec.ts @@ -63,6 +63,27 @@ describe("updateFields tests", () => { }, expected: '{"$set":{"categories":["tales","stories"]}}', }, + { + name: "push an element to an array", + input: { + $push: { + categories: "fiction", + }, + }, + expected: '{"$push":{"categories":"fiction"}}', + }, + { + name: "push an object element to an array", + input: { + $push: { + authors: { + firstName: "James", + lastName: "Bond", + }, + }, + }, + expected: '{"$push":{"authors":{"firstName":"James","lastName":"Bond"}}}', + }, ]; it.each(testCases)("Serializing '$name' to string", (fixture) => { @@ -70,6 +91,14 @@ describe("updateFields tests", () => { }); }); +class Author { + @Field() + firstName: string; + + @Field() + lastName: string; +} + class Publisher { @Field() totalPublished: number; @@ -103,4 +132,7 @@ class Books { @Field() publisher: Publisher; + + @Field(TigrisDataTypes.ARRAY, { elements: Author }) + authors: Author[]; } diff --git a/src/search/search.ts b/src/search/search.ts index 854c541..881c51d 100644 --- a/src/search/search.ts +++ b/src/search/search.ts @@ -6,7 +6,6 @@ import { Utility } from "../utility"; import { CreateOrUpdateIndexRequest as ProtoCreateIndexRequest, DeleteIndexRequest as ProtoDeleteIndexRequest, - GetIndexRequest as ProtoGetIndexRequest, ListIndexesRequest as ProtoListIndexesRequest, } from "../proto/server/v1/search_pb"; import { DecoratedSchemaProcessor } from "../schema/decorated-schema-processor"; @@ -99,19 +98,9 @@ export class Search { }); } + // TODO: this doesn't have to be promise but would be a breaking change for existing users public getIndex(name: string): Promise> { - const getIndexRequest = new ProtoGetIndexRequest().setProject(this.projectName).setName(name); - return new Promise>((resolve, reject) => { - this.client.getIndex(getIndexRequest, (error, response) => { - if (error) { - reject(error); - return; - } - if (response.hasIndex()) { - resolve(new SearchIndex(this.client, name, this.config)); - } - }); - }); + return Promise.resolve(new SearchIndex(this.client, name, this.config)); } public deleteIndex(name: string): Promise { diff --git a/src/types.ts b/src/types.ts index 52432e2..cbdd810 100644 --- a/src/types.ts +++ b/src/types.ts @@ -511,6 +511,7 @@ export type UpdateFields = $decrement?: DocumentFields; $multiply?: DocumentFields; $divide?: DocumentFields; + $push?: DocumentFields; } | DocumentFields; diff --git a/src/utility.ts b/src/utility.ts index 6e44675..a31b558 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -105,6 +105,7 @@ export const Utility = { case "$increment": case "$decrement": case "$multiply": + case "$push": updateBuilder[key] = value; break; default: