MCPcopy
hub / github.com/loopbackio/loopback-next / WhereBuilder

Class WhereBuilder

packages/filter/src/query.ts:281–524  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

279 * ```
280 */
281export class WhereBuilder<MT extends object = AnyObject> {
282 where: Where<MT>;
283
284 constructor(w?: Where<MT>) {
285 this.where = w ?? {};
286 }
287
288 private add(w: Where<MT>): this {
289 for (const k of Object.keys(w)) {
290 if (k in this.where) {
291 // Found conflicting keys, create an `and` operator to join the existing
292 // conditions with the new one
293 const where = {and: [this.where, w]};
294 this.where = where;
295 return this;
296 }
297 }
298 // Merge the where items
299 this.where = Object.assign(this.where, w);
300 return this;
301 }
302
303 /**
304 * @deprecated
305 * Starting from TypeScript 3.2, we don't have to cast any more. This method
306 * should be considered as `deprecated`.
307 *
308 * Cast an `and`, `or`, or condition clause to Where
309 * @param clause - And/Or/Condition clause
310 */
311 cast(clause: AndClause<MT> | OrClause<MT> | Condition<MT>): Where<MT> {
312 return clause;
313 }
314
315 /**
316 * Add an `and` clause.
317 * @param w - One or more where objects
318 */
319 and(...w: (Where<MT> | Where<MT>[])[]): this {
320 let clauses: Where<MT>[] = [];
321 w.forEach(where => {
322 clauses = clauses.concat(Array.isArray(where) ? where : [where]);
323 });
324 return this.add({and: clauses});
325 }
326
327 /**
328 * Add an `or` clause.
329 * @param w - One or more where objects
330 */
331 or(...w: (Where<MT> | Where<MT>[])[]): this {
332 let clauses: Where<MT>[] = [];
333 w.forEach(where => {
334 clauses = clauses.concat(Array.isArray(where) ? where : [where]);
335 });
336 return this.add({or: clauses});
337 }
338

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected