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; + } } diff --git a/test/api/contenttype.spec.ts b/test/api/contenttype.spec.ts index 8409b2fb..caaa2680 100644 --- a/test/api/contenttype.spec.ts +++ b/test/api/contenttype.spec.ts @@ -46,6 +46,17 @@ 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 () => { + 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..0a4b74a6 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': {'$exists': false}}); }); }); \ No newline at end of file