Skip to content

Commit 0fe1947

Browse files
feat: and query operator implementation
1 parent 97cbb6f commit 0fe1947

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

src/lib/query.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ export class Query extends BaseQuery {
201201
return this;
202202
}
203203

204+
/**
205+
* @method notExists
206+
* @memberof Query
207+
* @description Returns the raw (JSON) query based on the filters applied on Query object.
208+
* @example
209+
* import contentstack from '@contentstack/delivery-sdk'
210+
*
211+
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
212+
* const query = stack.contentType("contentTypeUid").entry().query();
213+
* const result = notExists('fieldUid').find()
214+
*
215+
* @returns {Query}
216+
*/
217+
notExists(key: string): Query {
218+
this._parameters[key] = { '$exists': false };
219+
return this;
220+
}
221+
204222
/**
205223
* @method or
206224
* @memberof Query
@@ -215,19 +233,35 @@ export class Query extends BaseQuery {
215233
*
216234
* @returns {Query}
217235
*/
218-
notExists(key: string): Query {
219-
this._parameters[key] = { '$exists': false };
236+
or(...queries: Query[]): Query {
237+
const paramsList: BaseQueryParameters[] = [];
238+
for (const queryItem of queries) {
239+
paramsList.push(queryItem._parameters);
240+
}
241+
this._parameters.$or = paramsList;
220242
return this;
221243
}
222244

223-
or(...queries: Query[]): Query {
224-
const combinedQuery: any = { $or: [] };
225-
for (const query of queries) {
226-
combinedQuery.$or.push(query._parameters);
245+
/**
246+
* @method and
247+
* @memberof Query
248+
* @description Returns the raw (JSON) query based on the filters applied on Query object.
249+
* @example
250+
* import contentstack from '@contentstack/delivery-sdk'
251+
*
252+
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
253+
* const query1 = await contentType.Entry().query().containedIn('fieldUID', ['value']);
254+
* const query2 = await contentType.Entry().query().where('fieldUID', QueryOperation.EQUALS, 'value2');
255+
* const query = await contentType.Entry().query().and(query1, query2).find();
256+
*
257+
* @returns {Query}
258+
*/
259+
and(...queries: Query[]): Query {
260+
const paramsList: BaseQueryParameters[] = [];
261+
for (const queryItem of queries) {
262+
paramsList.push(queryItem._parameters);
227263
}
228-
const newQuery: Query = Object.create(this);
229-
newQuery._parameters = combinedQuery;
230-
231-
return newQuery;
264+
this._parameters.$and = paramsList;
265+
return this;
232266
}
233267
}

0 commit comments

Comments
 (0)