Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 11 additions & 0 deletions test/api/contenttype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TEntry>()
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);
Expand Down
8 changes: 6 additions & 2 deletions test/unit/contenttype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}});
});
});