MCPcopy Create free account
hub / github.com/zenstackhq/zenstack / buildFilter

Function buildFilter

packages/orm/src/client/crud/dialects/base-dialect.ts:202–249  ·  view source on GitHub ↗
(model: string, modelAlias: string, where: boolean | object | undefined)

Source from the content-addressed store, hash-verified

200 }
201
202 buildFilter(model: string, modelAlias: string, where: boolean | object | undefined) {
203 if (where === true || where === undefined) {
204 return this.true();
205 }
206
207 if (where === false) {
208 return this.false();
209 }
210
211 let result = this.true();
212 const _where = flattenCompoundUniqueFilters(this.schema, model, where);
213
214 for (const [key, payload] of Object.entries(_where)) {
215 if (payload === undefined) {
216 continue;
217 }
218
219 if (key.startsWith('$')) {
220 continue;
221 }
222
223 if (this.isLogicalCombinator(key)) {
224 result = this.and(result, this.buildCompositeFilter(model, modelAlias, key, payload));
225 continue;
226 }
227
228 const fieldDef = requireField(this.schema, model, key);
229
230 if (fieldDef.relation) {
231 result = this.and(result, this.buildRelationFilter(model, modelAlias, key, fieldDef, payload));
232 } else {
233 // if the field is from a base model, build a reference from that model
234 const fieldRef = this.fieldRef(fieldDef.originModel ?? model, key, fieldDef.originModel ?? modelAlias);
235 if (fieldDef.array) {
236 result = this.and(result, this.buildArrayFilter(fieldRef, fieldDef, payload));
237 } else {
238 result = this.and(result, this.buildPrimitiveFilter(fieldRef, fieldDef, payload));
239 }
240 }
241 }
242
243 // call expression builder and combine the results
244 if ('$expr' in _where && typeof _where['$expr'] === 'function') {
245 result = this.and(result, _where['$expr'](this.eb));
246 }
247
248 return result;
249 }
250
251 private buildCursorFilter(
252 model: string,

Calls 3

requireFieldFunction · 0.90
fieldRefMethod · 0.80

Tested by

no test coverage detected