@dxos/echo-query 0.9.1-main.c7dcc2e112 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,10 +11,10 @@ var assertArgument = (condition, argumentName, message) => {
11
11
  //#region src/query-lite/query-lite.ts
12
12
  var OrderClass = class OrderClass {
13
13
  static "variance" = {};
14
- static is(value) {
14
+ static "is"(value) {
15
15
  return typeof value === "object" && value !== null && "~Order" in value;
16
16
  }
17
- constructor(ast) {
17
+ "constructor"(ast) {
18
18
  this.ast = ast;
19
19
  }
20
20
  "~Order" = OrderClass.variance;
@@ -43,22 +43,66 @@ let Order1;
43
43
  });
44
44
  })(Order1 || (Order1 = {}));
45
45
  const Order2 = Order1;
46
+ const _filterMatchValueLocal = (filter, value) => {
47
+ switch (filter.type) {
48
+ case "compare": switch (filter.operator) {
49
+ case "eq": return value === filter.value;
50
+ case "neq": return value !== filter.value;
51
+ case "gt": return value > filter.value;
52
+ case "gte": return value >= filter.value;
53
+ case "lt": return value < filter.value;
54
+ case "lte": return value <= filter.value;
55
+ default: return false;
56
+ }
57
+ case "object":
58
+ if (typeof value !== "object" || value === null) return false;
59
+ if (filter.props) {
60
+ for (const [key, vf] of Object.entries(filter.props)) if (!_filterMatchValueLocal(vf, value[key])) return false;
61
+ }
62
+ return true;
63
+ case "in": return filter.values.includes(value);
64
+ case "range": return value >= filter.from && value <= filter.to;
65
+ case "not": return !_filterMatchValueLocal(filter.filter, value);
66
+ case "and": return filter.filters.every((f) => _filterMatchValueLocal(f, value));
67
+ case "or": return filter.filters.some((f) => _filterMatchValueLocal(f, value));
68
+ default: return false;
69
+ }
70
+ };
71
+ const _filterMatchEntityLocal = (filter, entity) => {
72
+ switch (filter.type) {
73
+ case "object":
74
+ if (filter.typename !== null) {
75
+ const typeURI = entity?.["@type"] ?? entity?.system?.type;
76
+ if (!typeURI || typeURI !== filter.typename) return false;
77
+ }
78
+ if (filter.id && filter.id.length > 0 && !filter.id.includes(entity?.id)) return false;
79
+ if (filter.props) for (const [key, vf] of Object.entries(filter.props)) {
80
+ if (key.startsWith("@")) continue;
81
+ if (!_filterMatchValueLocal(vf, entity?.[key])) return false;
82
+ }
83
+ return true;
84
+ case "not": return !_filterMatchEntityLocal(filter.filter, entity);
85
+ case "and": return filter.filters.every((f) => _filterMatchEntityLocal(f, entity));
86
+ case "or": return filter.filters.some((f) => _filterMatchEntityLocal(f, entity));
87
+ default: throw new Error(`Filter type '${filter.type}' is not supported in toPredicate.`);
88
+ }
89
+ };
46
90
  var FilterClass = class FilterClass {
47
91
  static "variance" = {};
48
- static is(value) {
92
+ static "is"(value) {
49
93
  return typeof value === "object" && value !== null && "~Filter" in value;
50
94
  }
51
- static fromAst(ast) {
95
+ static "fromAst"(ast) {
52
96
  return new FilterClass(ast);
53
97
  }
54
- static everything() {
98
+ static "everything"() {
55
99
  return new FilterClass({
56
100
  type: "object",
57
101
  typename: null,
58
102
  props: {}
59
103
  });
60
104
  }
61
- static nothing() {
105
+ static "nothing"() {
62
106
  return new FilterClass({
63
107
  type: "not",
64
108
  filter: {
@@ -68,14 +112,14 @@ var FilterClass = class FilterClass {
68
112
  }
69
113
  });
70
114
  }
71
- static relation() {
115
+ static "relation"() {
72
116
  return new FilterClass({
73
117
  type: "object",
74
118
  typename: null,
75
119
  props: {}
76
120
  });
77
121
  }
78
- static id(...ids) {
122
+ static "id"(...ids) {
79
123
  if (ids.length === 0) return FilterClass.nothing();
80
124
  return new FilterClass({
81
125
  type: "object",
@@ -84,7 +128,7 @@ var FilterClass = class FilterClass {
84
128
  props: {}
85
129
  });
86
130
  }
87
- static type(schema, props) {
131
+ static "type"(schema, props) {
88
132
  if (typeof schema !== "string") throw new TypeError("expected typename as the first paramter");
89
133
  return new FilterClass({
90
134
  type: "object",
@@ -92,27 +136,27 @@ var FilterClass = class FilterClass {
92
136
  ...propsFilterToAst(props ?? {})
93
137
  });
94
138
  }
95
- static typename(typename) {
139
+ static "typename"(typename) {
96
140
  return new FilterClass({
97
141
  type: "object",
98
142
  typename: makeTypeDxn(typename),
99
143
  props: {}
100
144
  });
101
145
  }
102
- static typeURI(uri) {
146
+ static "typeURI"(uri) {
103
147
  return new FilterClass({
104
148
  type: "object",
105
149
  typename: uri,
106
150
  props: {}
107
151
  });
108
152
  }
109
- static tag(tag) {
153
+ static "tag"(tag) {
110
154
  return new FilterClass({
111
155
  type: "tag",
112
156
  tag
113
157
  });
114
158
  }
115
- static key(key, options) {
159
+ static "key"(key, options) {
116
160
  return new FilterClass({
117
161
  type: "object",
118
162
  typename: null,
@@ -121,21 +165,21 @@ var FilterClass = class FilterClass {
121
165
  metaVersion: options?.version
122
166
  });
123
167
  }
124
- static props(props) {
168
+ static "props"(props) {
125
169
  return new FilterClass({
126
170
  type: "object",
127
171
  typename: null,
128
172
  ...propsFilterToAst(props)
129
173
  });
130
174
  }
131
- static text(text, options) {
175
+ static "text"(text, options) {
132
176
  return new FilterClass({
133
177
  type: "text-search",
134
178
  text,
135
179
  searchKind: options?.type
136
180
  });
137
181
  }
138
- static foreignKeys(schema, keys) {
182
+ static "foreignKeys"(schema, keys) {
139
183
  assertArgument(typeof schema === "string", "schema");
140
184
  assertArgument(!schema.startsWith("dxn:"), "schema");
141
185
  return new FilterClass({
@@ -145,7 +189,7 @@ var FilterClass = class FilterClass {
145
189
  foreignKeys: keys
146
190
  });
147
191
  }
148
- static eq(value) {
192
+ static "eq"(value) {
149
193
  if (!isRef(value) && typeof value === "object" && value !== null) throw new TypeError("Cannot use object as a value for eq filter");
150
194
  return new FilterClass({
151
195
  type: "compare",
@@ -153,67 +197,67 @@ var FilterClass = class FilterClass {
153
197
  value: isRef(value) ? value.noInline().encode() : value
154
198
  });
155
199
  }
156
- static neq(value) {
200
+ static "neq"(value) {
157
201
  return new FilterClass({
158
202
  type: "compare",
159
203
  operator: "neq",
160
204
  value
161
205
  });
162
206
  }
163
- static gt(value) {
207
+ static "gt"(value) {
164
208
  return new FilterClass({
165
209
  type: "compare",
166
210
  operator: "gt",
167
211
  value
168
212
  });
169
213
  }
170
- static gte(value) {
214
+ static "gte"(value) {
171
215
  return new FilterClass({
172
216
  type: "compare",
173
217
  operator: "gte",
174
218
  value
175
219
  });
176
220
  }
177
- static lt(value) {
221
+ static "lt"(value) {
178
222
  return new FilterClass({
179
223
  type: "compare",
180
224
  operator: "lt",
181
225
  value
182
226
  });
183
227
  }
184
- static lte(value) {
228
+ static "lte"(value) {
185
229
  return new FilterClass({
186
230
  type: "compare",
187
231
  operator: "lte",
188
232
  value
189
233
  });
190
234
  }
191
- static in(...values) {
235
+ static "in"(...values) {
192
236
  return new FilterClass({
193
237
  type: "in",
194
238
  values
195
239
  });
196
240
  }
197
- static contains(value) {
241
+ static "contains"(value) {
198
242
  return new FilterClass({
199
243
  type: "contains",
200
244
  value
201
245
  });
202
246
  }
203
- static between(from, to) {
247
+ static "between"(from, to) {
204
248
  return new FilterClass({
205
249
  type: "range",
206
250
  from,
207
251
  to
208
252
  });
209
253
  }
210
- static updated(range) {
254
+ static "updated"(range) {
211
255
  return FilterClass._timeRangeFilter("updatedAt", range);
212
256
  }
213
- static created(range) {
257
+ static "created"(range) {
214
258
  return FilterClass._timeRangeFilter("createdAt", range);
215
259
  }
216
- static childOf(parents, options) {
260
+ static "childOf"(parents, options) {
217
261
  return new FilterClass({
218
262
  type: "child-of",
219
263
  parents: (Array.isArray(parents) ? parents : [parents]).map((item) => {
@@ -223,7 +267,7 @@ var FilterClass = class FilterClass {
223
267
  transitive: options?.transitive ?? true
224
268
  });
225
269
  }
226
- static _timeRangeFilter(field, range) {
270
+ static "_timeRangeFilter"(field, range) {
227
271
  const toMs = (d) => typeof d === "number" ? d : d.getTime();
228
272
  const filters = [];
229
273
  if (range.after != null) filters.push(new FilterClass({
@@ -241,29 +285,34 @@ var FilterClass = class FilterClass {
241
285
  if (filters.length === 0) return FilterClass.everything();
242
286
  return filters.length === 1 ? filters[0] : FilterClass.and(...filters);
243
287
  }
244
- static not(filter) {
288
+ static "not"(filter) {
245
289
  return new FilterClass({
246
290
  type: "not",
247
291
  filter: filter.ast
248
292
  });
249
293
  }
250
- static and(...filters) {
294
+ static "and"(...filters) {
251
295
  return new FilterClass({
252
296
  type: "and",
253
297
  filters: filters.map((f) => f.ast)
254
298
  });
255
299
  }
256
- static or(...filters) {
300
+ static "or"(...filters) {
257
301
  return new FilterClass({
258
302
  type: "or",
259
303
  filters: filters.map((f) => f.ast)
260
304
  });
261
305
  }
262
306
  /** Returns a human-readable string representation of a Filter AST. */
263
- static pretty(filter) {
307
+ static "pretty"(filter) {
264
308
  return prettyFilter(filter.ast);
265
309
  }
266
- constructor(ast) {
310
+ /** Create a predicate from a filter. */
311
+ static "toPredicate" = ((entityOrFilter, filter) => {
312
+ if (filter === void 0) return (entity) => _filterMatchEntityLocal(entityOrFilter.ast, entity);
313
+ return _filterMatchEntityLocal(filter.ast, entityOrFilter);
314
+ });
315
+ "constructor"(ast) {
267
316
  this.ast = ast;
268
317
  }
269
318
  "~Filter" = FilterClass.variance;
@@ -292,19 +341,19 @@ const processPredicate = (predicate) => {
292
341
  };
293
342
  var QueryClass = class QueryClass {
294
343
  static "variance" = {};
295
- static is(value) {
344
+ static "is"(value) {
296
345
  return typeof value === "object" && value !== null && "~Query" in value;
297
346
  }
298
- static fromAst(ast) {
347
+ static "fromAst"(ast) {
299
348
  return new QueryClass(ast);
300
349
  }
301
- static select(filter) {
350
+ static "select"(filter) {
302
351
  return new QueryClass({
303
352
  type: "select",
304
353
  filter: filter.ast
305
354
  });
306
355
  }
307
- select(filter) {
356
+ "select"(filter) {
308
357
  if (FilterClass.is(filter)) return new QueryClass({
309
358
  type: "filter",
310
359
  selection: this.ast,
@@ -316,34 +365,34 @@ var QueryClass = class QueryClass {
316
365
  filter: FilterClass.props(filter).ast
317
366
  });
318
367
  }
319
- static type(schema, predicates) {
368
+ static "type"(schema, predicates) {
320
369
  if (typeof schema !== "string") throw new TypeError("expected typename as the first paramter");
321
370
  return new QueryClass({
322
371
  type: "select",
323
372
  filter: FilterClass.type(schema, predicates).ast
324
373
  });
325
374
  }
326
- static all(...queries) {
375
+ static "all"(...queries) {
327
376
  if (queries.length === 0) throw new TypeError("Query.all combines results of multiple queries, to query all objects use Query.select(Filter.everything())");
328
377
  return new QueryClass({
329
378
  type: "union",
330
379
  queries: queries.map((q) => q.ast)
331
380
  });
332
381
  }
333
- static without(source, exclude) {
382
+ static "without"(source, exclude) {
334
383
  return new QueryClass({
335
384
  type: "set-difference",
336
385
  source: source.ast,
337
386
  exclude: exclude.ast
338
387
  });
339
388
  }
340
- static from(...args) {
389
+ static "from"(...args) {
341
390
  return new QueryClass({
342
391
  type: "select",
343
392
  filter: FilterClass.everything().ast
344
393
  }).from(...args);
345
394
  }
346
- from(...args) {
395
+ "from"(...args) {
347
396
  if (args.length > 1 && args.every((arg$1) => _isScopeLike(arg$1))) return new QueryClass({
348
397
  type: "from",
349
398
  query: this.ast,
@@ -372,21 +421,21 @@ var QueryClass = class QueryClass {
372
421
  throw new TypeError("Database and Feed objects are not supported in query-lite sandbox");
373
422
  }
374
423
  /** Returns a human-readable string representation of a Query AST. */
375
- static pretty(query) {
424
+ static "pretty"(query) {
376
425
  return prettyQuery(query.ast);
377
426
  }
378
- constructor(ast) {
427
+ "constructor"(ast) {
379
428
  this.ast = ast;
380
429
  }
381
430
  "~Query" = QueryClass.variance;
382
- reference(key) {
431
+ "reference"(key) {
383
432
  return new QueryClass({
384
433
  type: "reference-traversal",
385
434
  anchor: this.ast,
386
435
  property: key
387
436
  });
388
437
  }
389
- referencedBy(target, key) {
438
+ "referencedBy"(target, key) {
390
439
  if (target !== void 0 && typeof target !== "string") throw new TypeError("referencedBy requires a typename string in query-lite");
391
440
  const typename = target !== void 0 ? makeTypeDxn(target) : null;
392
441
  return new QueryClass({
@@ -396,7 +445,7 @@ var QueryClass = class QueryClass {
396
445
  typename
397
446
  });
398
447
  }
399
- sourceOf(relation, predicates) {
448
+ "sourceOf"(relation, predicates) {
400
449
  return new QueryClass({
401
450
  type: "relation",
402
451
  anchor: this.ast,
@@ -404,7 +453,7 @@ var QueryClass = class QueryClass {
404
453
  filter: relation === void 0 ? void 0 : typeof relation === "string" ? FilterClass.type(relation, predicates).ast : FilterClass.type(relation, predicates).ast
405
454
  });
406
455
  }
407
- targetOf(relation, predicates) {
456
+ "targetOf"(relation, predicates) {
408
457
  return new QueryClass({
409
458
  type: "relation",
410
459
  anchor: this.ast,
@@ -412,56 +461,56 @@ var QueryClass = class QueryClass {
412
461
  filter: relation === void 0 ? void 0 : typeof relation === "string" ? FilterClass.type(relation, predicates).ast : FilterClass.type(relation, predicates).ast
413
462
  });
414
463
  }
415
- source() {
464
+ "source"() {
416
465
  return new QueryClass({
417
466
  type: "relation-traversal",
418
467
  anchor: this.ast,
419
468
  direction: "source"
420
469
  });
421
470
  }
422
- target() {
471
+ "target"() {
423
472
  return new QueryClass({
424
473
  type: "relation-traversal",
425
474
  anchor: this.ast,
426
475
  direction: "target"
427
476
  });
428
477
  }
429
- parent() {
478
+ "parent"() {
430
479
  return new QueryClass({
431
480
  type: "hierarchy-traversal",
432
481
  anchor: this.ast,
433
482
  direction: "to-parent"
434
483
  });
435
484
  }
436
- children() {
485
+ "children"() {
437
486
  return new QueryClass({
438
487
  type: "hierarchy-traversal",
439
488
  anchor: this.ast,
440
489
  direction: "to-children"
441
490
  });
442
491
  }
443
- orderBy(...order) {
492
+ "orderBy"(...order) {
444
493
  return new QueryClass({
445
494
  type: "order",
446
495
  query: this.ast,
447
496
  order: order.map((o) => o.ast)
448
497
  });
449
498
  }
450
- limit(limit) {
499
+ "limit"(limit) {
451
500
  return new QueryClass({
452
501
  type: "limit",
453
502
  query: this.ast,
454
503
  limit
455
504
  });
456
505
  }
457
- options(options) {
506
+ "options"(options) {
458
507
  return new QueryClass({
459
508
  type: "options",
460
509
  query: this.ast,
461
510
  options
462
511
  });
463
512
  }
464
- debugLabel(label) {
513
+ "debugLabel"(label) {
465
514
  if (this.ast.type === "options") return new QueryClass({
466
515
  type: "options",
467
516
  query: this.ast.query,