Skip to content

Commit

Permalink
Merge pull request #370 from tigrisdata/main
Browse files Browse the repository at this point in the history
Merged by Reviewpad
  • Loading branch information
reviewpad[bot] authored May 16, 2023
2 parents bd1e30e + 1bee00b commit 339c469
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/__tests__/tigris.rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TigrisCollectionType,
TigrisDataTypes,
TigrisSchema,
UpdateFields,
UpdateQueryOptions,
} from "../types";
import { Tigris, TigrisClientConfig } from "../tigris";
Expand Down Expand Up @@ -355,7 +356,7 @@ describe("rpc tests", () => {

const expectedFilter = { id: 1 };
const expectedCollation: Collation = { case: Case.CaseInsensitive };
const expectedUpdateFields = { title: "one" };
const expectedUpdateFields: UpdateFields<IBook> = { title: "one", $push: { tags: "fiction" } };
const options = new UpdateQueryOptions(5, expectedCollation);

const updatePromise = collection.updateOne({
Expand Down
12 changes: 0 additions & 12 deletions src/__tests__/tigris.search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/tigris.updatefields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,42 @@ 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) => {
expect(Utility.updateFieldsString(fixture.input)).toBe(fixture.expected);
});
});

class Author {
@Field()
firstName: string;

@Field()
lastName: string;
}

class Publisher {
@Field()
totalPublished: number;
Expand Down Expand Up @@ -103,4 +132,7 @@ class Books {

@Field()
publisher: Publisher;

@Field(TigrisDataTypes.ARRAY, { elements: Author })
authors: Author[];
}
15 changes: 2 additions & 13 deletions src/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<T extends TigrisIndexType>(name: string): Promise<SearchIndex<T>> {
const getIndexRequest = new ProtoGetIndexRequest().setProject(this.projectName).setName(name);
return new Promise<SearchIndex<T>>((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<DeleteIndexResponse> {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ export type UpdateFields<T> =
$decrement?: DocumentFields<T, NumericType>;
$multiply?: DocumentFields<T, NumericType>;
$divide?: DocumentFields<T, NumericType>;
$push?: DocumentFields<T, FieldTypes | undefined>;
}
| DocumentFields<T, FieldTypes | undefined>;

Expand Down
1 change: 1 addition & 0 deletions src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const Utility = {
case "$increment":
case "$decrement":
case "$multiply":
case "$push":
updateBuilder[key] = value;
break;
default:
Expand Down

0 comments on commit 339c469

Please sign in to comment.