From 267e12d7ad2c9e0709bb575cd87cf669bbe2ef41 Mon Sep 17 00:00:00 2001 From: Ovais Tariq Date: Wed, 17 May 2023 17:14:40 -0700 Subject: [PATCH] fix: relax filter type checking (#375) --- src/__tests__/tigris.filters.spec.ts | 46 ++++++++++++++++++++++++++++ src/types.ts | 6 +++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/__tests__/tigris.filters.spec.ts b/src/__tests__/tigris.filters.spec.ts index 252ab94..45857df 100644 --- a/src/__tests__/tigris.filters.spec.ts +++ b/src/__tests__/tigris.filters.spec.ts @@ -46,6 +46,40 @@ describe("filters tests", () => { ); }); + it("filter with an array value on array field", () => { + const arrayFilter: Filter = { + tags: ["tag1"], + }; + expect(Utility.filterToString(arrayFilter)).toBe('{"tags":["tag1"]}'); + }); + + it("filter with a string value on array field", () => { + const arrayFilter: Filter = { + tags: "tag1", + }; + expect(Utility.filterToString(arrayFilter)).toBe('{"tags":"tag1"}'); + }); + + it("filter with an array value on array of objects", () => { + const arrayFilter: Filter = { + address: [ + { + city: "city1", + }, + ], + }; + expect(Utility.filterToString(arrayFilter)).toBe('{"address":[{"city":"city1"}]}'); + }); + + it("filter with an object value on array of objects", () => { + const arrayFilter: Filter = { + address: { + city: "city1", + }, + }; + expect(Utility.filterToString(arrayFilter)).toBe('{"address":{"city":"city1"}}'); + }); + it("simplerSelectorWithinLogicalFilterTest", () => { const filter1: Filter = { $and: [ @@ -285,6 +319,14 @@ export interface IUser extends TigrisCollectionType { balance: number; } +export class UserAddress { + street: string; + unit: string; + city: string; + state: string; + zipcode: number; +} + @TigrisCollection("user1") export class IUser1 { @PrimaryKey({ order: 1 }) @@ -299,6 +341,10 @@ export class IUser1 { createdAt: string; @Field() updatedAt: Date; + @Field({ elements: TigrisDataTypes.STRING }) + tags: string[]; + @Field({ elements: UserAddress }) + address: UserAddress[]; } export interface IUser2 extends TigrisCollectionType { diff --git a/src/types.ts b/src/types.ts index cbdd810..0ec06bd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -778,7 +778,11 @@ export type SelectorOperator = export type LogicalOperator = "$or" | "$and"; export type SelectorFilter = { - [K in DocumentPaths]?: PathType | { [P in SelectorOperator]?: PathType }; + [K in DocumentPaths]?: + | PathType + | { [P in SelectorOperator]?: PathType } + | FieldTypes + | { [P in SelectorOperator]?: FieldTypes }; }; export type LogicalFilter = {