@dxos/echo-query 0.8.4-main.c85a9c8dae → 0.8.4-main.cb12b3f963

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.
@@ -3,12 +3,12 @@
3
3
  //
4
4
 
5
5
  import { type Tree } from '@lezer/common';
6
- import { describe, it } from 'vitest';
6
+ import { describe, it, test } from 'vitest';
7
7
 
8
8
  import { Filter, Tag } from '@dxos/echo';
9
9
 
10
10
  import { QueryDSL } from './gen';
11
- import { type BuildResult, QueryBuilder } from './query-builder';
11
+ import { type BuildResult, QueryBuilder, normalizeInput } from './query-builder';
12
12
 
13
13
  // TODO(burdon): Ref/Relation traversal.
14
14
 
@@ -51,10 +51,10 @@ describe('query', () => {
51
51
  },
52
52
  // Type
53
53
  {
54
- input: 'type:dxos.org/type/Person',
54
+ input: 'type:org.dxos.type.person',
55
55
  expected: [
56
56
  'Query',
57
- // type:dxos.org/type/Person
57
+ // type:org.dxos.type.person
58
58
  'Filter',
59
59
  'TypeFilter',
60
60
  'TypeKeyword',
@@ -111,10 +111,10 @@ describe('query', () => {
111
111
  ],
112
112
  },
113
113
  {
114
- input: 'type:dxos.org/type/Person OR type:dxos.org/type/Organization',
114
+ input: 'type:org.dxos.type.person OR type:org.dxos.type.organization',
115
115
  expected: [
116
116
  'Query',
117
- // type:dxos.org/type/Person
117
+ // type:org.dxos.type.person
118
118
  'Filter',
119
119
  'TypeFilter',
120
120
  'TypeKeyword',
@@ -122,7 +122,7 @@ describe('query', () => {
122
122
  'Identifier',
123
123
  // OR
124
124
  'Or',
125
- // type:dxos.org/type/Organization
125
+ // type:org.dxos.type.organization
126
126
  'Filter',
127
127
  'TypeFilter',
128
128
  'TypeKeyword',
@@ -131,11 +131,11 @@ describe('query', () => {
131
131
  ],
132
132
  },
133
133
  {
134
- input: '(type:dxos.org/type/Person OR type:dxos.org/type/Organization) AND { name: "DXOS" }',
134
+ input: '(type:org.dxos.type.person OR type:org.dxos.type.organization) AND { name: "DXOS" }',
135
135
  expected: [
136
136
  'Query',
137
137
  '(',
138
- // type:dxos.org/type/Person
138
+ // type:org.dxos.type.person
139
139
  'Filter',
140
140
  'TypeFilter',
141
141
  'TypeKeyword',
@@ -143,7 +143,7 @@ describe('query', () => {
143
143
  'Identifier',
144
144
  // OR
145
145
  'Or',
146
- // type:dxos.org/type/Organization
146
+ // type:org.dxos.type.organization
147
147
  'Filter',
148
148
  'TypeFilter',
149
149
  'TypeKeyword',
@@ -164,10 +164,10 @@ describe('query', () => {
164
164
  ],
165
165
  },
166
166
  {
167
- input: 'type:dxos.org/type/Person -> type:dxos.org/type/Organization',
167
+ input: 'type:org.dxos.type.person -> type:org.dxos.type.organization',
168
168
  expected: [
169
169
  'Query',
170
- // type:dxos.org/type/Person
170
+ // type:org.dxos.type.person
171
171
  'Filter',
172
172
  'TypeFilter',
173
173
  'TypeKeyword',
@@ -175,7 +175,7 @@ describe('query', () => {
175
175
  'Identifier',
176
176
  'Relation',
177
177
  'ArrowRight',
178
- // type:dxos.org/type/Organization
178
+ // type:org.dxos.type.organization
179
179
  'Filter',
180
180
  'TypeFilter',
181
181
  'TypeKeyword',
@@ -184,10 +184,10 @@ describe('query', () => {
184
184
  ],
185
185
  },
186
186
  {
187
- input: 'type:dxos.org/type/Organization <- type:dxos.org/type/Person',
187
+ input: 'type:org.dxos.type.organization <- type:org.dxos.type.person',
188
188
  expected: [
189
189
  'Query',
190
- // type:dxos.org/type/Organization
190
+ // type:org.dxos.type.organization
191
191
  'Filter',
192
192
  'TypeFilter',
193
193
  'TypeKeyword',
@@ -195,7 +195,7 @@ describe('query', () => {
195
195
  'Identifier',
196
196
  'Relation',
197
197
  'ArrowLeft',
198
- // type:dxos.org/type/Person
198
+ // type:org.dxos.type.person
199
199
  'Filter',
200
200
  'TypeFilter',
201
201
  'TypeKeyword',
@@ -206,7 +206,7 @@ describe('query', () => {
206
206
  {
207
207
  // Persons for Organizations with name "DXOS"
208
208
  // TODO(burdon): Filter relations.
209
- input: '((type:dxos.org/type/Organization AND { name: "DXOS" }) -> type:dxos.org/type/Person)',
209
+ input: '((type:org.dxos.type.organization AND { name: "DXOS" }) -> type:org.dxos.type.person)',
210
210
  expected: [
211
211
  'Query',
212
212
  '(',
@@ -238,7 +238,7 @@ describe('query', () => {
238
238
  ],
239
239
  },
240
240
  {
241
- input: 'type:dxos.org/type/Person and { name: "DXOS" }',
241
+ input: 'type:org.dxos.type.person and { name: "DXOS" }',
242
242
  expected: [
243
243
  'Query',
244
244
  'Filter',
@@ -259,7 +259,7 @@ describe('query', () => {
259
259
  ],
260
260
  },
261
261
  {
262
- input: 'x = ( type: dxos.org/type/Person )',
262
+ input: 'x = ( type: org.dxos.type.person )',
263
263
  expected: [
264
264
  'Query',
265
265
  'Assignment',
@@ -305,9 +305,9 @@ describe('query', () => {
305
305
  const tests: Test[] = [
306
306
  // Types
307
307
  {
308
- input: 'type:dxos.org/type/Person',
308
+ input: 'type:org.dxos.type.person',
309
309
  expected: {
310
- filter: Filter.typename('dxos.org/type/Person'),
310
+ filter: Filter.typename('org.dxos.type.person'),
311
311
  },
312
312
  },
313
313
  // Tags
@@ -357,32 +357,32 @@ describe('query', () => {
357
357
  },
358
358
  },
359
359
  {
360
- input: 'type:dxos.org/type/Person OR type:dxos.org/type/Organization',
360
+ input: 'type:org.dxos.type.person OR type:org.dxos.type.organization',
361
361
  expected: {
362
- filter: Filter.or(Filter.typename('dxos.org/type/Person'), Filter.typename('dxos.org/type/Organization')),
362
+ filter: Filter.or(Filter.typename('org.dxos.type.person'), Filter.typename('org.dxos.type.organization')),
363
363
  },
364
364
  },
365
365
  {
366
- input: '(type:dxos.org/type/Person OR type:dxos.org/type/Organization) AND { name: "DXOS" }',
366
+ input: '(type:org.dxos.type.person OR type:org.dxos.type.organization) AND { name: "DXOS" }',
367
367
  expected: {
368
368
  filter: Filter.and(
369
- Filter.or(Filter.typename('dxos.org/type/Person'), Filter.typename('dxos.org/type/Organization')),
369
+ Filter.or(Filter.typename('org.dxos.type.person'), Filter.typename('org.dxos.type.organization')),
370
370
  Filter.props({ name: 'DXOS' }),
371
371
  ),
372
372
  },
373
373
  },
374
374
  {
375
- input: 'type:dxos.org/type/Person and { name: "DXOS" }',
375
+ input: 'type:org.dxos.type.person and { name: "DXOS" }',
376
376
  expected: {
377
- filter: Filter.and(Filter.typename('dxos.org/type/Person'), Filter.props({ name: 'DXOS' })),
377
+ filter: Filter.and(Filter.typename('org.dxos.type.person'), Filter.props({ name: 'DXOS' })),
378
378
  },
379
379
  },
380
380
  // Assignment
381
381
  {
382
- input: 'x = ( type:dxos.org/type/Person )',
382
+ input: 'x = ( type:org.dxos.type.person )',
383
383
  expected: {
384
384
  name: 'x',
385
- filter: Filter.typename('dxos.org/type/Person'),
385
+ filter: Filter.typename('org.dxos.type.person'),
386
386
  },
387
387
  },
388
388
  {
@@ -401,9 +401,9 @@ describe('query', () => {
401
401
  //
402
402
  // {
403
403
  // input: '',
404
- // expected: Query.select(Filter.typename('dxos.org/type/Person', { jobTitle: 'investor' }))
404
+ // expected: Query.select(Filter.typename('org.dxos.type.person', { jobTitle: 'investor' }))
405
405
  // .reference('organization')
406
- // .targetOf(Relation.of('dxos.org/relation/HasSubject')) // TODO(burdon): Invert?
406
+ // .targetOf(Relation.of('org.dxos.relation.hasSubject')) // TODO(burdon): Invert?
407
407
  // .source(),
408
408
  // },
409
409
  ];
@@ -413,4 +413,117 @@ describe('query', () => {
413
413
  expect(result, JSON.stringify({ input, result, expected }, null, 2)).toEqual(expected);
414
414
  });
415
415
  });
416
+
417
+ test('normalizeInput', ({ expect }) => {
418
+ type Test = { input: string; expected: string };
419
+ const tests: Test[] = [
420
+ { input: 'foo', expected: '"foo"' },
421
+ { input: 'foo bar', expected: '"foo" "bar"' },
422
+ { input: 'foo bar', expected: '"foo" "bar"' },
423
+ { input: '"already" bare', expected: '"already" "bare"' },
424
+ { input: 'from:rich@dxos.org', expected: 'from:"rich@dxos.org"' },
425
+ { input: 'from:rich@dxos.org urgent', expected: 'from:"rich@dxos.org" "urgent"' },
426
+ { input: 'name:DXOS', expected: 'name:"DXOS"' },
427
+ { input: 'count:42', expected: 'count:42' },
428
+ { input: 'active:true', expected: 'active:true' },
429
+ { input: 'value:null', expected: 'value:null' },
430
+ { input: 'name:"DXOS"', expected: 'name:"DXOS"' },
431
+ { input: 'type:org.dxos.type.person', expected: 'type:org.dxos.type.person' },
432
+ { input: '#tag', expected: '#tag' },
433
+ { input: '#tag foo', expected: '#tag "foo"' },
434
+ { input: 'foo AND bar', expected: '"foo" AND "bar"' },
435
+ { input: 'foo OR bar', expected: '"foo" OR "bar"' },
436
+ { input: 'NOT foo', expected: 'NOT "foo"' },
437
+ { input: '!foo', expected: '!"foo"' },
438
+ { input: '(foo bar)', expected: '("foo" "bar")' },
439
+ { input: 'x = ( foo )', expected: 'x = ( "foo" )' },
440
+ { input: '{ name: "DXOS" }', expected: '{ name: "DXOS" }' },
441
+ // Apostrophes inside barewords don't open a quoted string.
442
+ { input: "don't", expected: '"don\'t"' },
443
+ { input: "O'Connor", expected: '"O\'Connor"' },
444
+ { input: "don't worry", expected: '"don\'t" "worry"' },
445
+ // Unmatched closing brace/bracket — passed through, no infinite loop.
446
+ { input: 'foo}', expected: '"foo"}' },
447
+ { input: 'foo]bar', expected: '"foo"]"bar"' },
448
+ // Genuine single-quoted string still works.
449
+ { input: "'foo bar'", expected: "'foo bar'" },
450
+ // URLs are searched as text rather than auto-promoted to property filters.
451
+ { input: 'https://dxos.org', expected: '"https://dxos.org"' },
452
+ { input: 'http://example.com/foo', expected: '"http://example.com/foo"' },
453
+ { input: 'mailto:rich@dxos.org', expected: '"mailto:rich@dxos.org"' },
454
+ // Escapes round-trip: backslashes are escaped in the literal body.
455
+ { input: 'foo\\bar', expected: '"foo\\\\bar"' },
456
+ ];
457
+
458
+ for (const { input, expected } of tests) {
459
+ expect(normalizeInput(input), input).toEqual(expected);
460
+ }
461
+ });
462
+
463
+ test('build with property and text fragments', ({ expect }) => {
464
+ const queryBuilder = new QueryBuilder({
465
+ tag_1: Tag.make({ label: 'foo' }),
466
+ });
467
+
468
+ type Test = { input: string; expected: BuildResult };
469
+ const tests: Test[] = [
470
+ // Property filter from `key:value` text input.
471
+ {
472
+ input: 'from:rich@dxos.org',
473
+ expected: { filter: Filter.props({ from: 'rich@dxos.org' }) },
474
+ },
475
+ {
476
+ input: 'name:DXOS',
477
+ expected: { filter: Filter.props({ name: 'DXOS' }) },
478
+ },
479
+ // Bare text fragment becomes text search.
480
+ {
481
+ input: 'urgent',
482
+ expected: { filter: Filter.text('urgent') },
483
+ },
484
+ // Multiple bare fragments are AND-joined.
485
+ {
486
+ input: 'urgent review',
487
+ expected: { filter: Filter.and(Filter.text('urgent'), Filter.text('review')) },
488
+ },
489
+ // Mixed property + text fragment.
490
+ {
491
+ input: 'from:rich@dxos.org urgent',
492
+ expected: {
493
+ filter: Filter.and(Filter.props({ from: 'rich@dxos.org' }), Filter.text('urgent')),
494
+ },
495
+ },
496
+ // Tag + text fragment.
497
+ {
498
+ input: '#foo bar',
499
+ expected: { filter: Filter.and(Filter.tag('tag_1'), Filter.text('bar')) },
500
+ },
501
+ // Three fragments AND-joined.
502
+ {
503
+ input: 'a b c',
504
+ expected: {
505
+ filter: Filter.and(Filter.text('a'), Filter.text('b'), Filter.text('c')),
506
+ },
507
+ },
508
+ // URLs are text-searched, not promoted to property filters.
509
+ {
510
+ input: 'https://dxos.org',
511
+ expected: { filter: Filter.text('https://dxos.org') },
512
+ },
513
+ {
514
+ input: 'mailto:rich@dxos.org',
515
+ expected: { filter: Filter.text('mailto:rich@dxos.org') },
516
+ },
517
+ // Escapes decode back to the original input.
518
+ {
519
+ input: 'foo\\bar',
520
+ expected: { filter: Filter.text('foo\\bar') },
521
+ },
522
+ ];
523
+
524
+ tests.forEach(({ input, expected }) => {
525
+ const result = queryBuilder.build(input);
526
+ expect(result, JSON.stringify({ input, result, expected }, null, 2)).toEqual(expected);
527
+ });
528
+ });
416
529
  });
@@ -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
+ };