@dxos/echo-query 0.8.4-main.d05673bc65 → 0.8.4-main.dfabb4ec29

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.
@@ -221,7 +221,7 @@ class FilterClass implements Filter$.Any {
221
221
  });
222
222
  }
223
223
 
224
- static in<T>(...values: T[]): Filter$.Filter<T | undefined> {
224
+ static in<T>(...values: T[]): Filter$.Filter<T> {
225
225
  return new FilterClass({
226
226
  type: 'in',
227
227
  values,
@@ -235,7 +235,7 @@ class FilterClass implements Filter$.Any {
235
235
  });
236
236
  }
237
237
 
238
- static between<T>(from: T, to: T): Filter$.Filter<unknown> {
238
+ static between<T>(from: T, to: T): Filter$.Filter<T> {
239
239
  return new FilterClass({
240
240
  type: 'range',
241
241
  from,
@@ -243,6 +243,47 @@ class FilterClass implements Filter$.Any {
243
243
  });
244
244
  }
245
245
 
246
+ static updated(range: { after?: Date | number; before?: Date | number }): Filter$.Any {
247
+ return FilterClass._timeRangeFilter('updatedAt', range);
248
+ }
249
+
250
+ static created(range: { after?: Date | number; before?: Date | number }): Filter$.Any {
251
+ return FilterClass._timeRangeFilter('createdAt', range);
252
+ }
253
+
254
+ static childOf(parents: unknown | DXN | (unknown | DXN)[], options?: { transitive?: boolean }): Filter$.Any {
255
+ const items = Array.isArray(parents) ? parents : [parents];
256
+ const dxns = items.map((item) => {
257
+ if (isDxnLike(item)) {
258
+ return item.toString();
259
+ }
260
+ throw new TypeError('childOf requires DXN values in query-lite');
261
+ });
262
+ return new FilterClass({
263
+ type: 'child-of',
264
+ parents: dxns,
265
+ transitive: options?.transitive ?? true,
266
+ });
267
+ }
268
+
269
+ private static _timeRangeFilter(
270
+ field: 'updatedAt' | 'createdAt',
271
+ range: { after?: Date | number; before?: Date | number },
272
+ ): Filter$.Any {
273
+ const toMs = (d: Date | number) => (typeof d === 'number' ? d : d.getTime());
274
+ const filters: Filter$.Any[] = [];
275
+ if (range.after != null) {
276
+ filters.push(new FilterClass({ type: 'timestamp', field, operator: 'gte', value: toMs(range.after) }));
277
+ }
278
+ if (range.before != null) {
279
+ filters.push(new FilterClass({ type: 'timestamp', field, operator: 'lte', value: toMs(range.before) }));
280
+ }
281
+ if (filters.length === 0) {
282
+ return FilterClass.everything();
283
+ }
284
+ return filters.length === 1 ? filters[0] : FilterClass.and(...filters);
285
+ }
286
+
246
287
  static not<F extends Filter$.Any>(filter: F): Filter$.Filter<Filter$.Type<F>> {
247
288
  return new FilterClass({
248
289
  type: 'not',
@@ -268,6 +309,11 @@ class FilterClass implements Filter$.Any {
268
309
  });
269
310
  }
270
311
 
312
+ /** Returns a human-readable string representation of a Filter AST. */
313
+ static pretty(filter: Filter$.Any): string {
314
+ return prettyFilter(filter.ast);
315
+ }
316
+
271
317
  private constructor(public readonly ast: QueryAST.Filter) {}
272
318
 
273
319
  '~Filter' = FilterClass.variance;
@@ -345,6 +391,22 @@ class QueryClass implements Query$.Any {
345
391
  });
346
392
  }
347
393
 
394
+ select(filter: Filter$.Any | Filter$.Props<any>): Query$.Any {
395
+ if (FilterClass.is(filter)) {
396
+ return new QueryClass({
397
+ type: 'filter',
398
+ selection: this.ast,
399
+ filter: filter.ast,
400
+ });
401
+ } else {
402
+ return new QueryClass({
403
+ type: 'filter',
404
+ selection: this.ast,
405
+ filter: FilterClass.props(filter).ast,
406
+ });
407
+ }
408
+ }
409
+
348
410
  static type(schema: Schema.Schema.All | string, predicates?: Filter$.Props<unknown>): Query$.Any {
349
411
  return new QueryClass({
350
412
  type: 'select',
@@ -381,26 +443,40 @@ class QueryClass implements Query$.Any {
381
443
  return wrapper.from(source, options);
382
444
  }
383
445
 
384
- constructor(public readonly ast: QueryAST.Query) {}
385
-
386
- '~Query' = QueryClass.variance;
387
-
388
- select(filter: Filter$.Any | Filter$.Props<any>): Query$.Any {
389
- if (FilterClass.is(filter)) {
446
+ from(arg: any, options?: { includeFeeds?: boolean }): Query$.Any {
447
+ if (arg === 'all-accessible-spaces') {
390
448
  return new QueryClass({
391
- type: 'filter',
392
- selection: this.ast,
393
- filter: filter.ast,
449
+ type: 'from',
450
+ query: this.ast,
451
+ from: {
452
+ _tag: 'scope',
453
+ scope: {
454
+ ...(options?.includeFeeds ? { allQueuesFromSpaces: true } : {}),
455
+ },
456
+ },
394
457
  });
395
- } else {
458
+ }
459
+
460
+ if (_isScopeLike(arg)) {
396
461
  return new QueryClass({
397
- type: 'filter',
398
- selection: this.ast,
399
- filter: FilterClass.props(filter).ast,
462
+ type: 'from',
463
+ query: this.ast,
464
+ from: { _tag: 'scope', scope: arg },
400
465
  });
401
466
  }
467
+
468
+ throw new TypeError('Database and Feed objects are not supported in query-lite sandbox');
469
+ }
470
+
471
+ /** Returns a human-readable string representation of a Query AST. */
472
+ static pretty(query: Query$.Any): string {
473
+ return prettyQuery(query.ast);
402
474
  }
403
475
 
476
+ constructor(public readonly ast: QueryAST.Query) {}
477
+
478
+ '~Query' = QueryClass.variance;
479
+
404
480
  reference(key: string): Query$.Any {
405
481
  return new QueryClass({
406
482
  type: 'reference-traversal',
@@ -490,36 +566,26 @@ class QueryClass implements Query$.Any {
490
566
  });
491
567
  }
492
568
 
493
- from(arg: any, options?: { includeFeeds?: boolean }): Query$.Any {
494
- if (arg === 'all-accessible-spaces') {
495
- return new QueryClass({
496
- type: 'from',
497
- query: this.ast,
498
- from: {
499
- _tag: 'scope',
500
- scope: {
501
- ...(options?.includeFeeds ? { allQueuesFromSpaces: true } : {}),
502
- },
503
- },
504
- });
505
- }
569
+ options(options: QueryAST.QueryOptions): Query$.Any {
570
+ return new QueryClass({
571
+ type: 'options',
572
+ query: this.ast,
573
+ options,
574
+ });
575
+ }
506
576
 
507
- if (_isScopeLike(arg)) {
577
+ debugLabel(label: string): Query$.Any {
578
+ if (this.ast.type === 'options') {
508
579
  return new QueryClass({
509
- type: 'from',
510
- query: this.ast,
511
- from: { _tag: 'scope', scope: arg },
580
+ type: 'options',
581
+ query: this.ast.query,
582
+ options: { ...this.ast.options, debugLabel: label },
512
583
  });
513
584
  }
514
-
515
- throw new TypeError('Database and Feed objects are not supported in query-lite sandbox');
516
- }
517
-
518
- options(options: QueryAST.QueryOptions): Query$.Any {
519
585
  return new QueryClass({
520
586
  type: 'options',
521
587
  query: this.ast,
522
- options,
588
+ options: { debugLabel: label },
523
589
  });
524
590
  }
525
591
  }
@@ -538,6 +604,16 @@ const makeTypeDxn = (typename: string) => {
538
604
  return `dxn:type:${typename}`;
539
605
  };
540
606
 
607
+ const isDxnLike = (value: unknown): value is DXN => {
608
+ return (
609
+ typeof value === 'object' &&
610
+ value !== null &&
611
+ 'toString' in value &&
612
+ typeof value.toString === 'function' &&
613
+ value.toString().startsWith('dxn:')
614
+ );
615
+ };
616
+
541
617
  const SCOPE_KEYS = new Set(['spaceIds', 'queues', 'allQueuesFromSpaces']);
542
618
 
543
619
  const _isScopeLike = (value: unknown): value is QueryAST.Scope => {
@@ -546,3 +622,123 @@ const _isScopeLike = (value: unknown): value is QueryAST.Scope => {
546
622
  }
547
623
  return Object.keys(value).every((key) => SCOPE_KEYS.has(key));
548
624
  };
625
+
626
+ const prettyFilter = (filter: QueryAST.Filter): string => {
627
+ switch (filter.type) {
628
+ case 'object': {
629
+ const parts: string[] = [];
630
+ if (filter.typename !== null) {
631
+ parts.push(JSON.stringify(filter.typename));
632
+ }
633
+ const propEntries = Object.entries(filter.props);
634
+ if (propEntries.length > 0) {
635
+ const propsStr = propEntries.map(([k, v]) => `${k}: ${prettyFilter(v)}`).join(', ');
636
+ parts.push(`{ ${propsStr} }`);
637
+ }
638
+ if (filter.id !== undefined) {
639
+ parts.push(`id: [${filter.id.join(', ')}]`);
640
+ }
641
+ return parts.length > 0 ? `Filter.type(${parts.join(', ')})` : 'Filter.everything()';
642
+ }
643
+ case 'compare':
644
+ return `Filter.${filter.operator}(${JSON.stringify(filter.value)})`;
645
+ case 'in':
646
+ return `Filter.in(${filter.values.map((v) => JSON.stringify(v)).join(', ')})`;
647
+ case 'contains':
648
+ return `Filter.contains(${JSON.stringify(filter.value)})`;
649
+ case 'range':
650
+ return `Filter.between(${JSON.stringify(filter.from)}, ${JSON.stringify(filter.to)})`;
651
+ case 'text-search':
652
+ return `Filter.text(${JSON.stringify(filter.text)})`;
653
+ case 'tag':
654
+ return `Filter.tag(${JSON.stringify(filter.tag)})`;
655
+ case 'child-of':
656
+ return `Filter.childOf([${filter.parents.map((parent) => JSON.stringify(parent)).join(', ')}], { transitive: ${filter.transitive} })`;
657
+ case 'timestamp':
658
+ return `Filter.${filter.field}.${filter.operator}(${filter.value})`;
659
+ case 'not':
660
+ return `Filter.not(${prettyFilter(filter.filter)})`;
661
+ case 'and':
662
+ return `Filter.and(${filter.filters.map(prettyFilter).join(', ')})`;
663
+ case 'or':
664
+ return `Filter.or(${filter.filters.map(prettyFilter).join(', ')})`;
665
+ }
666
+ };
667
+
668
+ const prettyQuery = (query: QueryAST.Query): string => {
669
+ switch (query.type) {
670
+ case 'select':
671
+ return `Query.select(${prettyFilter(query.filter)})`;
672
+ case 'filter':
673
+ return `${prettyQuery(query.selection)}.select(${prettyFilter(query.filter)})`;
674
+ case 'reference-traversal':
675
+ return `${prettyQuery(query.anchor)}.reference(${JSON.stringify(query.property)})`;
676
+ case 'incoming-references': {
677
+ const args: string[] = [];
678
+ if (query.typename !== null) {
679
+ args.push(JSON.stringify(query.typename));
680
+ }
681
+ if (query.property !== null) {
682
+ args.push(JSON.stringify(query.property));
683
+ }
684
+ return `${prettyQuery(query.anchor)}.referencedBy(${args.join(', ')})`;
685
+ }
686
+ case 'relation': {
687
+ const method =
688
+ query.direction === 'outgoing' ? 'sourceOf' : query.direction === 'incoming' ? 'targetOf' : 'relationOf';
689
+ const filterStr = query.filter !== undefined ? prettyFilter(query.filter) : '';
690
+ return `${prettyQuery(query.anchor)}.${method}(${filterStr})`;
691
+ }
692
+ case 'relation-traversal':
693
+ return `${prettyQuery(query.anchor)}.${query.direction}()`;
694
+ case 'hierarchy-traversal':
695
+ return query.direction === 'to-parent'
696
+ ? `${prettyQuery(query.anchor)}.parent()`
697
+ : `${prettyQuery(query.anchor)}.children()`;
698
+ case 'union':
699
+ return `Query.all(${query.queries.map(prettyQuery).join(', ')})`;
700
+ case 'set-difference':
701
+ return `Query.without(${prettyQuery(query.source)}, ${prettyQuery(query.exclude)})`;
702
+ case 'order': {
703
+ const orders = query.order.map((o) => {
704
+ if (o.kind === 'natural') {
705
+ return 'Order.natural';
706
+ }
707
+ if (o.kind === 'rank') {
708
+ return `Order.rank(${JSON.stringify(o.direction)})`;
709
+ }
710
+ return `Order.property(${JSON.stringify(o.property)}, ${JSON.stringify(o.direction)})`;
711
+ });
712
+ return `${prettyQuery(query.query)}.orderBy(${orders.join(', ')})`;
713
+ }
714
+ case 'options': {
715
+ const parts: string[] = [];
716
+ if (query.options.deleted !== undefined) {
717
+ parts.push(`deleted: ${JSON.stringify(query.options.deleted)}`);
718
+ }
719
+ if (query.options.debugLabel !== undefined) {
720
+ parts.push(`debugLabel: ${JSON.stringify(query.options.debugLabel)}`);
721
+ }
722
+ return `${prettyQuery(query.query)}.options({ ${parts.join(', ')} })`;
723
+ }
724
+ case 'from': {
725
+ if (query.from._tag === 'scope') {
726
+ const scope = query.from.scope;
727
+ const parts: string[] = [];
728
+ if (scope.spaceIds !== undefined) {
729
+ parts.push(`spaceIds: [${scope.spaceIds.join(', ')}]`);
730
+ }
731
+ if (scope.queues !== undefined) {
732
+ parts.push(`queues: [${scope.queues.join(', ')}]`);
733
+ }
734
+ if (scope.allQueuesFromSpaces !== undefined) {
735
+ parts.push(`allQueuesFromSpaces: ${scope.allQueuesFromSpaces}`);
736
+ }
737
+ return `${prettyQuery(query.query)}.from({ ${parts.join(', ')} })`;
738
+ }
739
+ return `${prettyQuery(query.query)}.from(${prettyQuery(query.from.query)})`;
740
+ }
741
+ case 'limit':
742
+ return `${prettyQuery(query.query)}.limit(${query.limit})`;
743
+ }
744
+ };
@@ -39,14 +39,14 @@ describe('QuerySandbox', () => {
39
39
  const ast = sandbox.eval(trim`
40
40
  Query.select(Filter.type('org.dxos.type.person', { jobTitle: 'investor' }))
41
41
  .reference('organization')
42
- .targetOf('org.dxos.relation.has-subject')
42
+ .targetOf('org.dxos.relation.hasSubject')
43
43
  .source()
44
44
  `);
45
45
 
46
46
  expect(ast).toEqual(
47
47
  Query.select(Filter.type('org.dxos.type.person', { jobTitle: 'investor' }))
48
48
  .reference('organization')
49
- .targetOf('org.dxos.relation.has-subject')
49
+ .targetOf('org.dxos.relation.hasSubject')
50
50
  .source().ast,
51
51
  );
52
52
  });