@dxos/echo-query 0.8.4-main.ead640a → 0.8.4-main.f466a3d56e

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.
Files changed (42) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +1 -1
  3. package/dist/lib/neutral/index.mjs +917 -0
  4. package/dist/lib/neutral/index.mjs.map +7 -0
  5. package/dist/lib/neutral/meta.json +1 -0
  6. package/dist/query-lite/index.d.ts +10022 -0
  7. package/dist/query-lite/index.d.ts.map +1 -0
  8. package/dist/query-lite/index.js +545 -375
  9. package/dist/query-lite/index.js.map +1 -0
  10. package/dist/types/src/index.d.ts +1 -0
  11. package/dist/types/src/index.d.ts.map +1 -1
  12. package/dist/types/src/parser/gen/index.d.ts.map +1 -1
  13. package/dist/types/src/parser/gen/query.terms.d.ts +1 -1
  14. package/dist/types/src/parser/gen/query.terms.d.ts.map +1 -1
  15. package/dist/types/src/parser/query-builder.d.ts +18 -3
  16. package/dist/types/src/parser/query-builder.d.ts.map +1 -1
  17. package/dist/types/src/query-lite/query-lite.d.ts +4 -4
  18. package/dist/types/src/query-lite/query-lite.d.ts.map +1 -1
  19. package/dist/types/src/sandbox/index.d.ts +2 -0
  20. package/dist/types/src/sandbox/index.d.ts.map +1 -0
  21. package/dist/types/src/sandbox/query-sandbox.d.ts +1 -1
  22. package/dist/types/src/sandbox/query-sandbox.d.ts.map +1 -1
  23. package/dist/types/src/sandbox/quickjs.d.ts.map +1 -1
  24. package/dist/types/tsconfig.tsbuildinfo +1 -1
  25. package/package.json +22 -20
  26. package/src/index.ts +1 -0
  27. package/src/parser/gen/query.terms.ts +24 -23
  28. package/src/parser/gen/query.ts +8 -8
  29. package/src/parser/query-builder.ts +333 -20
  30. package/src/parser/query.grammar +8 -2
  31. package/src/parser/query.test.ts +207 -41
  32. package/src/query-lite/query-lite.ts +360 -73
  33. package/src/sandbox/index.ts +5 -0
  34. package/src/sandbox/query-sandbox.test.ts +15 -14
  35. package/src/sandbox/query-sandbox.ts +1 -1
  36. package/src/sandbox/quickjs.ts +1 -2
  37. package/dist/lib/browser/index.mjs +0 -530
  38. package/dist/lib/browser/index.mjs.map +0 -7
  39. package/dist/lib/browser/meta.json +0 -1
  40. package/dist/lib/node-esm/index.mjs +0 -530
  41. package/dist/lib/node-esm/index.mjs.map +0 -7
  42. package/dist/lib/node-esm/meta.json +0 -1
@@ -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 { 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',
@@ -258,6 +258,22 @@ describe('query', () => {
258
258
  '}',
259
259
  ],
260
260
  },
261
+ {
262
+ input: 'x = ( type: org.dxos.type.person )',
263
+ expected: [
264
+ 'Query',
265
+ 'Assignment',
266
+ 'Identifier',
267
+ '=',
268
+ '(',
269
+ 'Filter',
270
+ 'TypeFilter',
271
+ 'TypeKeyword',
272
+ ':',
273
+ 'Identifier',
274
+ ')',
275
+ ],
276
+ },
261
277
  ];
262
278
 
263
279
  for (const { input, expected } of tests) {
@@ -285,72 +301,109 @@ describe('query', () => {
285
301
  });
286
302
 
287
303
  // TODO(burdon): Test "not"
288
- type Test = { input: string; expected: Filter.Any };
304
+ type Test = { input: string; expected: BuildResult };
289
305
  const tests: Test[] = [
290
306
  // Types
291
307
  {
292
- input: 'type:dxos.org/type/Person',
293
- expected: Filter.typename('dxos.org/type/Person'),
308
+ input: 'type:org.dxos.type.person',
309
+ expected: {
310
+ filter: Filter.typename('org.dxos.type.person'),
311
+ },
294
312
  },
295
313
  // Tags
296
314
  {
297
315
  input: '#foo',
298
- expected: Filter.tag('tag_1'),
316
+ expected: {
317
+ filter: Filter.tag('tag_1'),
318
+ },
299
319
  },
300
320
  {
301
321
  input: '#foo AND #bar',
302
- expected: Filter.and(Filter.tag('tag_1'), Filter.tag('tag_2')),
322
+ expected: {
323
+ filter: Filter.and(Filter.tag('tag_1'), Filter.tag('tag_2')),
324
+ },
303
325
  },
304
326
  {
305
327
  input: '#foo #bar',
306
- expected: Filter.and(Filter.tag('tag_1'), Filter.tag('tag_2')),
328
+ expected: {
329
+ filter: Filter.and(Filter.tag('tag_1'), Filter.tag('tag_2')),
330
+ },
307
331
  },
308
332
  // Text
309
333
  {
310
334
  input: '"test"',
311
- expected: Filter.text('test'),
335
+ expected: {
336
+ filter: Filter.text('test'),
337
+ },
312
338
  },
313
339
  // Mixed
314
340
  {
315
341
  input: '#foo "test"',
316
- expected: Filter.and(Filter.tag('tag_1'), Filter.text('test')),
342
+ expected: {
343
+ filter: Filter.and(Filter.tag('tag_1'), Filter.text('test')),
344
+ },
317
345
  },
318
346
  // Props
319
347
  {
320
348
  input: '{ name: "DXOS" }',
321
- expected: Filter.props({ name: 'DXOS' }),
349
+ expected: {
350
+ filter: Filter.props({ name: 'DXOS' }),
351
+ },
322
352
  },
323
353
  {
324
354
  input: '{ value: 100 }',
325
- expected: Filter.props({ value: 100 }),
355
+ expected: {
356
+ filter: Filter.props({ value: 100 }),
357
+ },
358
+ },
359
+ {
360
+ input: 'type:org.dxos.type.person OR type:org.dxos.type.organization',
361
+ expected: {
362
+ filter: Filter.or(Filter.typename('org.dxos.type.person'), Filter.typename('org.dxos.type.organization')),
363
+ },
326
364
  },
327
365
  {
328
- input: 'type:dxos.org/type/Person OR type:dxos.org/type/Organization',
329
- expected: Filter.or(Filter.typename('dxos.org/type/Person'), Filter.typename('dxos.org/type/Organization')),
366
+ input: '(type:org.dxos.type.person OR type:org.dxos.type.organization) AND { name: "DXOS" }',
367
+ expected: {
368
+ filter: Filter.and(
369
+ Filter.or(Filter.typename('org.dxos.type.person'), Filter.typename('org.dxos.type.organization')),
370
+ Filter.props({ name: 'DXOS' }),
371
+ ),
372
+ },
330
373
  },
331
374
  {
332
- input: '(type:dxos.org/type/Person OR type:dxos.org/type/Organization) AND { name: "DXOS" }',
333
- expected: Filter.and(
334
- Filter.or(Filter.typename('dxos.org/type/Person'), Filter.typename('dxos.org/type/Organization')),
335
- Filter.props({ name: 'DXOS' }),
336
- ),
375
+ input: 'type:org.dxos.type.person and { name: "DXOS" }',
376
+ expected: {
377
+ filter: Filter.and(Filter.typename('org.dxos.type.person'), Filter.props({ name: 'DXOS' })),
378
+ },
337
379
  },
380
+ // Assignment
338
381
  {
339
- input: 'type:dxos.org/type/Person and { name: "DXOS" }',
340
- expected: Filter.and(Filter.typename('dxos.org/type/Person'), Filter.props({ name: 'DXOS' })),
382
+ input: 'x = ( type:org.dxos.type.person )',
383
+ expected: {
384
+ name: 'x',
385
+ filter: Filter.typename('org.dxos.type.person'),
386
+ },
387
+ },
388
+ {
389
+ input: 'x = ( #foo AND "bar" )',
390
+ expected: {
391
+ name: 'x',
392
+ filter: Filter.and(Filter.tag('tag_1'), Filter.text('bar')),
393
+ },
341
394
  },
342
395
  // TODO(burdon): Convert Query/Filter expr to AST.
343
396
  // TODO(burdon): Person -> Organization (many-to-many relation).
344
397
  // Get Research Note objects for Organization objects for Person objects with jobTitle.
345
398
  //
346
- // Cypher: MATCH (p:Person)-[:WorksAt]->(o:Organization)<-[:ResearchOn]-(r:ResearchNote) WHERE p.jotTitle IS NOT NULL
347
- // ((type:Person AND { jobTitle: "investor" }) -[:WorksAt]-> type:Organization) <-[:ResearchOn]- type:ResearchNote
399
+ // Cypher: MATCH (p:Person)-[:WorksAt]->(o:Organization)<-[:HasSubject]-(r:ResearchNote) WHERE p.jotTitle IS NOT NULL
400
+ // ((type:Person AND { jobTitle: "investor" }) -[:WorksAt]-> type:Organization) <-[:HasSubject]- type:ResearchNote
348
401
  //
349
402
  // {
350
403
  // input: '',
351
- // expected: Query.select(Filter.typename('dxos.org/type/Person', { jobTitle: 'investor' }))
404
+ // expected: Query.select(Filter.typename('org.dxos.type.person', { jobTitle: 'investor' }))
352
405
  // .reference('organization')
353
- // .targetOf(Relation.of('dxos.org/relation/ResearchOn')) // TODO(burdon): Invert?
406
+ // .targetOf(Relation.of('org.dxos.relation.hasSubject')) // TODO(burdon): Invert?
354
407
  // .source(),
355
408
  // },
356
409
  ];
@@ -360,4 +413,117 @@ describe('query', () => {
360
413
  expect(result, JSON.stringify({ input, result, expected }, null, 2)).toEqual(expected);
361
414
  });
362
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
+ });
363
529
  });