@dxos/echo-query 0.8.4-main.a4bbb77 → 0.8.4-main.abd8ff62ef

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 (54) hide show
  1. package/README.md +1 -1
  2. package/dist/lib/neutral/index.mjs +917 -0
  3. package/dist/lib/neutral/index.mjs.map +7 -0
  4. package/dist/lib/neutral/meta.json +1 -0
  5. package/dist/query-lite/index.d.ts +9975 -0
  6. package/dist/query-lite/index.d.ts.map +1 -0
  7. package/dist/query-lite/index.js +548 -0
  8. package/dist/query-lite/index.js.map +1 -0
  9. package/dist/types/src/index.d.ts +1 -0
  10. package/dist/types/src/index.d.ts.map +1 -1
  11. package/dist/types/src/parser/gen/index.d.ts.map +1 -1
  12. package/dist/types/src/parser/gen/query.terms.d.ts +1 -1
  13. package/dist/types/src/parser/gen/query.terms.d.ts.map +1 -1
  14. package/dist/types/src/parser/query-builder.d.ts +21 -5
  15. package/dist/types/src/parser/query-builder.d.ts.map +1 -1
  16. package/dist/types/src/query-lite/index.d.ts +2 -0
  17. package/dist/types/src/query-lite/index.d.ts.map +1 -0
  18. package/dist/types/src/query-lite/query-lite.d.ts +8 -0
  19. package/dist/types/src/query-lite/query-lite.d.ts.map +1 -0
  20. package/dist/types/src/sandbox/index.d.ts +2 -0
  21. package/dist/types/src/sandbox/index.d.ts.map +1 -0
  22. package/dist/types/src/sandbox/query-sandbox.d.ts +21 -0
  23. package/dist/types/src/sandbox/query-sandbox.d.ts.map +1 -0
  24. package/dist/types/src/sandbox/query-sandbox.test.d.ts +2 -0
  25. package/dist/types/src/sandbox/query-sandbox.test.d.ts.map +1 -0
  26. package/dist/types/src/sandbox/quickjs.d.ts +8 -0
  27. package/dist/types/src/sandbox/quickjs.d.ts.map +1 -0
  28. package/dist/types/src/sandbox/quickjs.test.d.ts +2 -0
  29. package/dist/types/src/sandbox/quickjs.test.d.ts.map +1 -0
  30. package/dist/types/tsconfig.tsbuildinfo +1 -1
  31. package/package.json +25 -14
  32. package/src/env.d.ts +8 -0
  33. package/src/index.ts +1 -0
  34. package/src/parser/gen/query.terms.ts +24 -23
  35. package/src/parser/gen/query.ts +8 -8
  36. package/src/parser/query-builder.ts +359 -30
  37. package/src/parser/query.grammar +8 -2
  38. package/src/parser/query.test.ts +212 -43
  39. package/src/query-lite/index.ts +5 -0
  40. package/src/query-lite/query-lite.ts +744 -0
  41. package/src/sandbox/index.ts +5 -0
  42. package/src/sandbox/query-sandbox.test.ts +53 -0
  43. package/src/sandbox/query-sandbox.ts +72 -0
  44. package/src/sandbox/quickjs.test.ts +67 -0
  45. package/src/sandbox/quickjs.ts +33 -0
  46. package/dist/lib/browser/index.mjs +0 -505
  47. package/dist/lib/browser/index.mjs.map +0 -7
  48. package/dist/lib/browser/meta.json +0 -1
  49. package/dist/lib/node-esm/index.mjs +0 -505
  50. package/dist/lib/node-esm/index.mjs.map +0 -7
  51. package/dist/lib/node-esm/meta.json +0 -1
  52. package/dist/types/src/search.test.d.ts +0 -2
  53. package/dist/types/src/search.test.d.ts.map +0 -1
  54. package/src/search.test.ts +0 -49
@@ -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
- import { Filter } from '@dxos/echo';
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) {
@@ -279,75 +295,115 @@ describe('query', () => {
279
295
  });
280
296
 
281
297
  it('build', ({ expect }) => {
282
- const queryBuilder = new QueryBuilder();
298
+ const queryBuilder = new QueryBuilder({
299
+ tag_1: Tag.make({ label: 'foo' }),
300
+ tag_2: Tag.make({ label: 'bar' }),
301
+ });
283
302
 
284
303
  // TODO(burdon): Test "not"
285
- type Test = { input: string; expected: Filter.Any };
304
+ type Test = { input: string; expected: BuildResult };
286
305
  const tests: Test[] = [
287
306
  // Types
288
307
  {
289
- input: 'type:dxos.org/type/Person',
290
- 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
+ },
291
312
  },
292
313
  // Tags
293
314
  {
294
315
  input: '#foo',
295
- expected: Filter.tag('foo'),
316
+ expected: {
317
+ filter: Filter.tag('tag_1'),
318
+ },
296
319
  },
297
320
  {
298
321
  input: '#foo AND #bar',
299
- expected: Filter.and(Filter.tag('foo'), Filter.tag('bar')),
322
+ expected: {
323
+ filter: Filter.and(Filter.tag('tag_1'), Filter.tag('tag_2')),
324
+ },
300
325
  },
301
326
  {
302
327
  input: '#foo #bar',
303
- expected: Filter.and(Filter.tag('foo'), Filter.tag('bar')),
328
+ expected: {
329
+ filter: Filter.and(Filter.tag('tag_1'), Filter.tag('tag_2')),
330
+ },
304
331
  },
305
332
  // Text
306
333
  {
307
334
  input: '"test"',
308
- expected: Filter.text('test'),
335
+ expected: {
336
+ filter: Filter.text('test'),
337
+ },
309
338
  },
310
339
  // Mixed
311
340
  {
312
341
  input: '#foo "test"',
313
- expected: Filter.and(Filter.tag('foo'), Filter.text('test')),
342
+ expected: {
343
+ filter: Filter.and(Filter.tag('tag_1'), Filter.text('test')),
344
+ },
314
345
  },
315
346
  // Props
316
347
  {
317
348
  input: '{ name: "DXOS" }',
318
- expected: Filter.props({ name: 'DXOS' }),
349
+ expected: {
350
+ filter: Filter.props({ name: 'DXOS' }),
351
+ },
319
352
  },
320
353
  {
321
354
  input: '{ value: 100 }',
322
- expected: Filter.props({ value: 100 }),
355
+ expected: {
356
+ filter: Filter.props({ value: 100 }),
357
+ },
323
358
  },
324
359
  {
325
- input: 'type:dxos.org/type/Person OR type:dxos.org/type/Organization',
326
- expected: Filter.or(Filter.typename('dxos.org/type/Person'), Filter.typename('dxos.org/type/Organization')),
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
+ },
327
364
  },
328
365
  {
329
- input: '(type:dxos.org/type/Person OR type:dxos.org/type/Organization) AND { name: "DXOS" }',
330
- expected: Filter.and(
331
- Filter.or(Filter.typename('dxos.org/type/Person'), Filter.typename('dxos.org/type/Organization')),
332
- Filter.props({ name: 'DXOS' }),
333
- ),
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
+ },
334
373
  },
335
374
  {
336
- input: 'type:dxos.org/type/Person and { name: "DXOS" }',
337
- expected: Filter.and(Filter.typename('dxos.org/type/Person'), Filter.props({ name: 'DXOS' })),
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
+ },
379
+ },
380
+ // Assignment
381
+ {
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
+ },
338
394
  },
339
395
  // TODO(burdon): Convert Query/Filter expr to AST.
340
396
  // TODO(burdon): Person -> Organization (many-to-many relation).
341
397
  // Get Research Note objects for Organization objects for Person objects with jobTitle.
342
398
  //
343
- // Cypher: MATCH (p:Person)-[:WorksAt]->(o:Organization)<-[:ResearchOn]-(r:ResearchNote) WHERE p.jotTitle IS NOT NULL
344
- // ((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
345
401
  //
346
402
  // {
347
403
  // input: '',
348
- // expected: Query.select(Filter.typename('dxos.org/type/Person', { jobTitle: 'investor' }))
404
+ // expected: Query.select(Filter.typename('org.dxos.type.person', { jobTitle: 'investor' }))
349
405
  // .reference('organization')
350
- // .targetOf(Relation.of('dxos.org/relation/ResearchOn')) // TODO(burdon): Invert?
406
+ // .targetOf(Relation.of('org.dxos.relation.hasSubject')) // TODO(burdon): Invert?
351
407
  // .source(),
352
408
  // },
353
409
  ];
@@ -357,4 +413,117 @@ describe('query', () => {
357
413
  expect(result, JSON.stringify({ input, result, expected }, null, 2)).toEqual(expected);
358
414
  });
359
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
+ });
360
529
  });
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * from './query-lite';