From e5ed42ae11d7fc5640a8adee6c2cb3fba060ddaa Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 5 Mar 2024 11:09:44 +0530 Subject: [PATCH 1/4] feat: notExists query operator implementation --- src/lib/query.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/lib/query.ts b/src/lib/query.ts index 39640a29..7547e453 100644 --- a/src/lib/query.ts +++ b/src/lib/query.ts @@ -200,4 +200,22 @@ export class Query extends BaseQuery { this._parameters[key] = { '$nin': value }; return this; } + + /** + * @method notExists + * @memberof Query + * @description Returns the raw (JSON) query based on the filters applied on Query object. + * @example + * import contentstack from '@contentstack/delivery-sdk' + * + * const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" }); + * const query = stack.contentType("contentTypeUid").Query(); + * const result = notExists('fieldUid').find() + * + * @returns {Query} + */ + notExists(key: string): Query { + this._parameters[key] = { '$exists': false }; + return this; + } } From 1524972e4a55bfe55902e0ec5509a0273a62b9a4 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 5 Mar 2024 11:10:17 +0530 Subject: [PATCH 2/4] test: unit and api tests for notExists operator --- test/api/contenttype.spec.ts | 13 +++++++++++++ test/unit/contenttype.spec.ts | 8 ++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/test/api/contenttype.spec.ts b/test/api/contenttype.spec.ts index 8409b2fb..4d30ad04 100644 --- a/test/api/contenttype.spec.ts +++ b/test/api/contenttype.spec.ts @@ -46,6 +46,19 @@ describe('ContentType Query API test cases', () => { expect(query.entries[0].created_at).toBeDefined(); } }); + + it('should get entries which does not match the fieldUid - notExists', async () => { + console.log("🚀 ~ makeContentType ~ stack:", await stack.ContentType().find()) + + const query = await makeContentType('contenttype_uid').Query().notExists('multi_line').find() + if (query.entries) { + expect(query.entries[0]._version).toBeDefined(); + expect(query.entries[0].title).toBeDefined(); + expect(query.entries[0].uid).toBeDefined(); + expect(query.entries[0].created_at).toBeDefined(); + expect((query.entries[0] as any).multi_line).not.toBeDefined() + } + }); }); function makeContentType(uid = ''): ContentType { const contentType = stack.ContentType(uid); diff --git a/test/unit/contenttype.spec.ts b/test/unit/contenttype.spec.ts index 89700f6f..7ed4a3f4 100644 --- a/test/unit/contenttype.spec.ts +++ b/test/unit/contenttype.spec.ts @@ -58,10 +58,14 @@ describe('ContentType Query class', () => { }); it('should get entries which matches the fieldUid and values', () => { const query = contentType.Query().containedIn('fieldUID', ['value']); - expect(query._queryParams).toStrictEqual({'fieldUID': {'$in': ['value']}}); + expect(query._parameters).toStrictEqual({'fieldUID': {'$in': ['value']}}); }); it('should get entries which does not match the fieldUid and values', () => { const query = contentType.Query().notContainedIn('fieldUID', ['value', 'value2']); - expect(query._queryParams).toStrictEqual({'fieldUID': {'$nin': ['value', 'value2']}}); + expect(query._parameters).toStrictEqual({'fieldUID': {'$nin': ['value', 'value2']}}); + }); + it('should get entries which does not match the fieldUid - notExists', () => { + const query = contentType.Query().notExists('fieldUID'); + expect(query._parameters).toStrictEqual({'fieldUID': {'$nin': ['value', 'value2']}}); }); }); \ No newline at end of file From 65306418e77b829914d4283f318418860719d9f1 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 5 Mar 2024 11:12:11 +0530 Subject: [PATCH 3/4] test: updated unit test assertion --- test/unit/contenttype.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/contenttype.spec.ts b/test/unit/contenttype.spec.ts index 7ed4a3f4..0a4b74a6 100644 --- a/test/unit/contenttype.spec.ts +++ b/test/unit/contenttype.spec.ts @@ -66,6 +66,6 @@ describe('ContentType Query class', () => { }); it('should get entries which does not match the fieldUid - notExists', () => { const query = contentType.Query().notExists('fieldUID'); - expect(query._parameters).toStrictEqual({'fieldUID': {'$nin': ['value', 'value2']}}); + expect(query._parameters).toStrictEqual({'fieldUID': {'$exists': false}}); }); }); \ No newline at end of file From a3d66404a9730bb2727ae4efbfe001df54d708de Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 5 Mar 2024 15:22:06 +0530 Subject: [PATCH 4/4] removed console log --- test/api/contenttype.spec.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/api/contenttype.spec.ts b/test/api/contenttype.spec.ts index 4d30ad04..caaa2680 100644 --- a/test/api/contenttype.spec.ts +++ b/test/api/contenttype.spec.ts @@ -48,8 +48,6 @@ describe('ContentType Query API test cases', () => { }); it('should get entries which does not match the fieldUid - notExists', async () => { - console.log("🚀 ~ makeContentType ~ stack:", await stack.ContentType().find()) - const query = await makeContentType('contenttype_uid').Query().notExists('multi_line').find() if (query.entries) { expect(query.entries[0]._version).toBeDefined();