@dxos/echo-query 0.8.4-main.84f28bd → 0.8.4-main.937b3ca
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.
- package/README.md +1 -1
- package/dist/lib/browser/index.mjs +717 -0
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +717 -0
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/query-lite/index.d.ts +8765 -0
- package/dist/query-lite/index.d.ts.map +1 -0
- package/dist/query-lite/index.js +367 -0
- package/dist/query-lite/index.js.map +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/parser/gen/index.d.ts +8 -0
- package/dist/types/src/parser/gen/index.d.ts.map +1 -0
- package/dist/types/src/parser/gen/query.d.ts +3 -0
- package/dist/types/src/parser/gen/query.d.ts.map +1 -0
- package/dist/types/src/parser/gen/query.terms.d.ts +2 -0
- package/dist/types/src/parser/gen/query.terms.d.ts.map +1 -0
- package/dist/types/src/parser/index.d.ts +3 -0
- package/dist/types/src/parser/index.d.ts.map +1 -0
- package/dist/types/src/parser/query-builder.d.ts +82 -0
- package/dist/types/src/parser/query-builder.d.ts.map +1 -0
- package/dist/types/src/parser/query.test.d.ts +2 -0
- package/dist/types/src/parser/query.test.d.ts.map +1 -0
- package/dist/types/src/query-lite/index.d.ts +2 -0
- package/dist/types/src/query-lite/index.d.ts.map +1 -0
- package/dist/types/src/query-lite/query-lite.d.ts +8 -0
- package/dist/types/src/query-lite/query-lite.d.ts.map +1 -0
- package/dist/types/src/sandbox/index.d.ts +2 -0
- package/dist/types/src/sandbox/index.d.ts.map +1 -0
- package/dist/types/src/sandbox/query-sandbox.d.ts +21 -0
- package/dist/types/src/sandbox/query-sandbox.d.ts.map +1 -0
- package/dist/types/src/sandbox/query-sandbox.test.d.ts +2 -0
- package/dist/types/src/sandbox/query-sandbox.test.d.ts.map +1 -0
- package/dist/types/src/sandbox/quickjs.d.ts +8 -0
- package/dist/types/src/sandbox/quickjs.d.ts.map +1 -0
- package/dist/types/src/sandbox/quickjs.test.d.ts +2 -0
- package/dist/types/src/sandbox/quickjs.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +29 -8
- package/src/env.d.ts +8 -0
- package/src/index.ts +3 -0
- package/src/parser/gen/index.ts +13 -0
- package/src/parser/gen/query.terms.ts +27 -0
- package/src/parser/gen/query.ts +18 -0
- package/src/parser/index.ts +6 -0
- package/src/parser/query-builder.ts +539 -0
- package/src/parser/query.grammar +130 -0
- package/src/parser/query.test.ts +416 -0
- package/src/query-lite/index.ts +5 -0
- package/src/query-lite/query-lite.ts +489 -0
- package/src/sandbox/index.ts +5 -0
- package/src/sandbox/query-sandbox.test.ts +53 -0
- package/src/sandbox/query-sandbox.ts +72 -0
- package/src/sandbox/quickjs.test.ts +67 -0
- package/src/sandbox/quickjs.ts +33 -0
- package/dist/types/src/search.test.d.ts +0 -2
- package/dist/types/src/search.test.d.ts.map +0 -1
- package/src/search.test.ts +0 -49
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Schema from 'effect/Schema';
|
|
6
|
+
|
|
7
|
+
import type { Filter as Filter$, Order as Order$, Query as Query$, Ref } from '@dxos/echo';
|
|
8
|
+
import type { ForeignKey, QueryAST } from '@dxos/echo-protocol';
|
|
9
|
+
import { assertArgument } from '@dxos/invariant';
|
|
10
|
+
import type { DXN, ObjectId } from '@dxos/keys';
|
|
11
|
+
|
|
12
|
+
//
|
|
13
|
+
// Light-weight implementation of query execution.
|
|
14
|
+
//
|
|
15
|
+
|
|
16
|
+
// TODO(wittjosiah): The `export * as ...` syntax causes tsdown to genereate multiple files which breaks the sandbox.
|
|
17
|
+
|
|
18
|
+
class OrderClass implements Order$.Any {
|
|
19
|
+
private static variance: Order$.Any['~Order'] = {} as Order$.Any['~Order'];
|
|
20
|
+
|
|
21
|
+
static is(value: unknown): value is Order$.Any {
|
|
22
|
+
return typeof value === 'object' && value !== null && '~Order' in value;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
constructor(public readonly ast: QueryAST.Order) {}
|
|
26
|
+
|
|
27
|
+
'~Order' = OrderClass.variance;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
namespace Order1 {
|
|
31
|
+
export const natural: Order$.Any = new OrderClass({ kind: 'natural' });
|
|
32
|
+
export const property = <T>(property: keyof T & string, direction: QueryAST.OrderDirection): Order$.Order<T> =>
|
|
33
|
+
new OrderClass({
|
|
34
|
+
kind: 'property',
|
|
35
|
+
property,
|
|
36
|
+
direction,
|
|
37
|
+
});
|
|
38
|
+
export const rank = <T>(direction: QueryAST.OrderDirection = 'desc'): Order$.Order<T> =>
|
|
39
|
+
new OrderClass({
|
|
40
|
+
kind: 'rank',
|
|
41
|
+
direction,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const Order2: typeof Order$ = Order1;
|
|
46
|
+
export { Order2 as Order };
|
|
47
|
+
|
|
48
|
+
class FilterClass implements Filter$.Any {
|
|
49
|
+
private static variance: Filter$.Any['~Filter'] = {} as Filter$.Any['~Filter'];
|
|
50
|
+
|
|
51
|
+
static is(value: unknown): value is Filter$.Any {
|
|
52
|
+
return typeof value === 'object' && value !== null && '~Filter' in value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static fromAst(ast: QueryAST.Filter): Filter$.Any {
|
|
56
|
+
return new FilterClass(ast);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static everything(): FilterClass {
|
|
60
|
+
return new FilterClass({
|
|
61
|
+
type: 'object',
|
|
62
|
+
typename: null,
|
|
63
|
+
props: {},
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static nothing(): FilterClass {
|
|
68
|
+
return new FilterClass({
|
|
69
|
+
type: 'not',
|
|
70
|
+
filter: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
typename: null,
|
|
73
|
+
props: {},
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static relation() {
|
|
79
|
+
return new FilterClass({
|
|
80
|
+
type: 'object',
|
|
81
|
+
typename: null,
|
|
82
|
+
props: {},
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static id(...ids: ObjectId[]): Filter$.Any {
|
|
87
|
+
// assertArgument(
|
|
88
|
+
// ids.every((id) => ObjectId.isValid(id)),
|
|
89
|
+
// 'ids',
|
|
90
|
+
// 'ids must be valid',
|
|
91
|
+
// );
|
|
92
|
+
|
|
93
|
+
if (ids.length === 0) {
|
|
94
|
+
return FilterClass.nothing();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return new FilterClass({
|
|
98
|
+
type: 'object',
|
|
99
|
+
typename: null,
|
|
100
|
+
id: ids,
|
|
101
|
+
props: {},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
static type<S extends Schema.Schema.All>(
|
|
106
|
+
schema: S | string,
|
|
107
|
+
props?: Filter$.Props<Schema.Schema.Type<S>>,
|
|
108
|
+
): Filter$.Filter<Schema.Schema.Type<S>> {
|
|
109
|
+
if (typeof schema !== 'string') {
|
|
110
|
+
throw new TypeError('expected typename as the first paramter');
|
|
111
|
+
}
|
|
112
|
+
return new FilterClass({
|
|
113
|
+
type: 'object',
|
|
114
|
+
typename: makeTypeDxn(schema),
|
|
115
|
+
...propsFilterToAst(props ?? {}),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static typename(typename: string): Filter$.Any {
|
|
120
|
+
return new FilterClass({
|
|
121
|
+
type: 'object',
|
|
122
|
+
typename: makeTypeDxn(typename),
|
|
123
|
+
props: {},
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static typeDXN(dxn: DXN): Filter$.Any {
|
|
128
|
+
return new FilterClass({
|
|
129
|
+
type: 'object',
|
|
130
|
+
typename: dxn.toString(),
|
|
131
|
+
props: {},
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static tag(tag: string): Filter$.Any {
|
|
136
|
+
return new FilterClass({
|
|
137
|
+
type: 'tag',
|
|
138
|
+
tag,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static props<T>(props: Filter$.Props<T>): Filter$.Filter<T> {
|
|
143
|
+
return new FilterClass({
|
|
144
|
+
type: 'object',
|
|
145
|
+
typename: null,
|
|
146
|
+
...propsFilterToAst(props),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
static text(text: string, options?: Filter$.TextSearchOptions): Filter$.Any {
|
|
151
|
+
return new FilterClass({
|
|
152
|
+
type: 'text-search',
|
|
153
|
+
text,
|
|
154
|
+
searchKind: options?.type,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static foreignKeys<S extends Schema.Schema.All>(
|
|
159
|
+
schema: S | string,
|
|
160
|
+
keys: ForeignKey[],
|
|
161
|
+
): Filter$.Filter<Schema.Schema.Type<S>> {
|
|
162
|
+
assertArgument(typeof schema === 'string', 'schema');
|
|
163
|
+
assertArgument(!schema.startsWith('dxn:'), 'schema');
|
|
164
|
+
return new FilterClass({
|
|
165
|
+
type: 'object',
|
|
166
|
+
typename: `dxn:type:${schema}`,
|
|
167
|
+
props: {},
|
|
168
|
+
foreignKeys: keys,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static eq<T>(value: T): Filter$.Filter<T | undefined> {
|
|
173
|
+
if (!isRef(value) && typeof value === 'object' && value !== null) {
|
|
174
|
+
throw new TypeError('Cannot use object as a value for eq filter');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return new FilterClass({
|
|
178
|
+
type: 'compare',
|
|
179
|
+
operator: 'eq',
|
|
180
|
+
value: isRef(value) ? value.noInline().encode() : value,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
static neq<T>(value: T): Filter$.Filter<T | undefined> {
|
|
185
|
+
return new FilterClass({
|
|
186
|
+
type: 'compare',
|
|
187
|
+
operator: 'neq',
|
|
188
|
+
value,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static gt<T>(value: T): Filter$.Filter<T | undefined> {
|
|
193
|
+
return new FilterClass({
|
|
194
|
+
type: 'compare',
|
|
195
|
+
operator: 'gt',
|
|
196
|
+
value,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
static gte<T>(value: T): Filter$.Filter<T | undefined> {
|
|
201
|
+
return new FilterClass({
|
|
202
|
+
type: 'compare',
|
|
203
|
+
operator: 'gte',
|
|
204
|
+
value,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
static lt<T>(value: T): Filter$.Filter<T | undefined> {
|
|
209
|
+
return new FilterClass({
|
|
210
|
+
type: 'compare',
|
|
211
|
+
operator: 'lt',
|
|
212
|
+
value,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
static lte<T>(value: T): Filter$.Filter<T | undefined> {
|
|
217
|
+
return new FilterClass({
|
|
218
|
+
type: 'compare',
|
|
219
|
+
operator: 'lte',
|
|
220
|
+
value,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
static in<T>(...values: T[]): Filter$.Filter<T | undefined> {
|
|
225
|
+
return new FilterClass({
|
|
226
|
+
type: 'in',
|
|
227
|
+
values,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
static contains<T>(value: T): Filter$.Filter<readonly T[] | undefined> {
|
|
232
|
+
return new FilterClass({
|
|
233
|
+
type: 'contains',
|
|
234
|
+
value,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
static between<T>(from: T, to: T): Filter$.Filter<unknown> {
|
|
239
|
+
return new FilterClass({
|
|
240
|
+
type: 'range',
|
|
241
|
+
from,
|
|
242
|
+
to,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
static not<F extends Filter$.Any>(filter: F): Filter$.Filter<Filter$.Type<F>> {
|
|
247
|
+
return new FilterClass({
|
|
248
|
+
type: 'not',
|
|
249
|
+
filter: filter.ast,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
static and<Filters extends readonly Filter$.Any[]>(
|
|
254
|
+
...filters: Filters
|
|
255
|
+
): Filter$.Filter<Filter$.Type<Filters[number]>> {
|
|
256
|
+
return new FilterClass({
|
|
257
|
+
type: 'and',
|
|
258
|
+
filters: filters.map((f) => f.ast),
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
static or<Filters extends readonly Filter$.Any[]>(
|
|
263
|
+
...filters: Filters
|
|
264
|
+
): Filter$.Filter<Filter$.Type<Filters[number]>> {
|
|
265
|
+
return new FilterClass({
|
|
266
|
+
type: 'or',
|
|
267
|
+
filters: filters.map((f) => f.ast),
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private constructor(public readonly ast: QueryAST.Filter) {}
|
|
272
|
+
|
|
273
|
+
'~Filter' = FilterClass.variance;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export const Filter1: typeof Filter$ = FilterClass;
|
|
277
|
+
export { Filter1 as Filter };
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* All property paths inside T that are references.
|
|
281
|
+
*/
|
|
282
|
+
// TODO(dmaretskyi): Filter only properties that are references (or optional references, or unions that include references).
|
|
283
|
+
type RefPropKey<T> = keyof T & string;
|
|
284
|
+
|
|
285
|
+
const propsFilterToAst = (predicates: Filter$.Props<any>): Pick<QueryAST.FilterObject, 'id' | 'props'> => {
|
|
286
|
+
let idFilter: readonly ObjectId[] | undefined;
|
|
287
|
+
if ('id' in predicates) {
|
|
288
|
+
assertArgument(
|
|
289
|
+
typeof predicates.id === 'string' || Array.isArray(predicates.id),
|
|
290
|
+
'predicates.id',
|
|
291
|
+
'invalid id filter',
|
|
292
|
+
);
|
|
293
|
+
idFilter = typeof predicates.id === 'string' ? [predicates.id] : predicates.id;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return {
|
|
297
|
+
id: idFilter,
|
|
298
|
+
props: Object.fromEntries(
|
|
299
|
+
Object.entries(predicates)
|
|
300
|
+
.filter(([prop, _value]) => prop !== 'id')
|
|
301
|
+
.map(([prop, predicate]) => [prop, processPredicate(predicate)]),
|
|
302
|
+
) as Record<string, QueryAST.Filter>,
|
|
303
|
+
};
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const processPredicate = (predicate: any): QueryAST.Filter => {
|
|
307
|
+
if (FilterClass.is(predicate)) {
|
|
308
|
+
return predicate.ast;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (Array.isArray(predicate)) {
|
|
312
|
+
throw new Error('Array predicates are not yet supported.');
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (!isRef(predicate) && typeof predicate === 'object' && predicate !== null) {
|
|
316
|
+
const nestedProps = Object.fromEntries(
|
|
317
|
+
Object.entries(predicate).map(([key, value]) => [key, processPredicate(value)]),
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
type: 'object',
|
|
322
|
+
typename: null,
|
|
323
|
+
props: nestedProps,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return FilterClass.eq(predicate).ast;
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
class QueryClass implements Query$.Any {
|
|
331
|
+
private static variance: Query$.Any['~Query'] = {} as Query$.Any['~Query'];
|
|
332
|
+
|
|
333
|
+
static is(value: unknown): value is Query$.Any {
|
|
334
|
+
return typeof value === 'object' && value !== null && '~Query' in value;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
static fromAst(ast: QueryAST.Query): Query$.Any {
|
|
338
|
+
return new QueryClass(ast);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
static select<F extends Filter$.Any>(filter: F): Query$.Query<Filter$.Type<F>> {
|
|
342
|
+
return new QueryClass({
|
|
343
|
+
type: 'select',
|
|
344
|
+
filter: filter.ast,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
static type(schema: Schema.Schema.All | string, predicates?: Filter$.Props<unknown>): Query$.Any {
|
|
349
|
+
return new QueryClass({
|
|
350
|
+
type: 'select',
|
|
351
|
+
filter: FilterClass.type(schema, predicates).ast,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
static all(...queries: Query$.Any[]): Query$.Any {
|
|
356
|
+
if (queries.length === 0) {
|
|
357
|
+
throw new TypeError(
|
|
358
|
+
'Query.all combines results of multiple queries, to query all objects use Query.select(Filter.everything())',
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
return new QueryClass({
|
|
362
|
+
type: 'union',
|
|
363
|
+
queries: queries.map((q) => q.ast),
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
static without<T>(source: Query$.Query<T>, exclude: Query$.Query<T>): Query$.Query<T> {
|
|
368
|
+
return new QueryClass({
|
|
369
|
+
type: 'set-difference',
|
|
370
|
+
source: source.ast,
|
|
371
|
+
exclude: exclude.ast,
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
constructor(public readonly ast: QueryAST.Query) {}
|
|
376
|
+
|
|
377
|
+
'~Query' = QueryClass.variance;
|
|
378
|
+
|
|
379
|
+
select(filter: Filter$.Any | Filter$.Props<any>): Query$.Any {
|
|
380
|
+
if (FilterClass.is(filter)) {
|
|
381
|
+
return new QueryClass({
|
|
382
|
+
type: 'filter',
|
|
383
|
+
selection: this.ast,
|
|
384
|
+
filter: filter.ast,
|
|
385
|
+
});
|
|
386
|
+
} else {
|
|
387
|
+
return new QueryClass({
|
|
388
|
+
type: 'filter',
|
|
389
|
+
selection: this.ast,
|
|
390
|
+
filter: FilterClass.props(filter).ast,
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
reference(key: string): Query$.Any {
|
|
396
|
+
return new QueryClass({
|
|
397
|
+
type: 'reference-traversal',
|
|
398
|
+
anchor: this.ast,
|
|
399
|
+
property: key,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
referencedBy(target?: Schema.Schema.All | string, key?: string): Query$.Any {
|
|
404
|
+
const typename =
|
|
405
|
+
target !== undefined
|
|
406
|
+
? (assertArgument(typeof target === 'string', 'target'),
|
|
407
|
+
assertArgument(!target.startsWith('dxn:'), 'target'),
|
|
408
|
+
target)
|
|
409
|
+
: null;
|
|
410
|
+
return new QueryClass({
|
|
411
|
+
type: 'incoming-references',
|
|
412
|
+
anchor: this.ast,
|
|
413
|
+
property: key ?? null,
|
|
414
|
+
typename,
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
sourceOf(relation: Schema.Schema.All | string, predicates?: Filter$.Props<unknown> | undefined): Query$.Any {
|
|
419
|
+
return new QueryClass({
|
|
420
|
+
type: 'relation',
|
|
421
|
+
anchor: this.ast,
|
|
422
|
+
direction: 'outgoing',
|
|
423
|
+
filter: FilterClass.type(relation, predicates).ast,
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
targetOf(relation: Schema.Schema.All | string, predicates?: Filter$.Props<unknown> | undefined): Query$.Any {
|
|
428
|
+
return new QueryClass({
|
|
429
|
+
type: 'relation',
|
|
430
|
+
anchor: this.ast,
|
|
431
|
+
direction: 'incoming',
|
|
432
|
+
filter: FilterClass.type(relation, predicates).ast,
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
source(): Query$.Any {
|
|
437
|
+
return new QueryClass({
|
|
438
|
+
type: 'relation-traversal',
|
|
439
|
+
anchor: this.ast,
|
|
440
|
+
direction: 'source',
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
target(): Query$.Any {
|
|
445
|
+
return new QueryClass({
|
|
446
|
+
type: 'relation-traversal',
|
|
447
|
+
anchor: this.ast,
|
|
448
|
+
direction: 'target',
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
orderBy(...order: Order$.Any[]): Query$.Any {
|
|
453
|
+
return new QueryClass({
|
|
454
|
+
type: 'order',
|
|
455
|
+
query: this.ast,
|
|
456
|
+
order: order.map((o) => o.ast),
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
limit(limit: number): Query$.Any {
|
|
461
|
+
return new QueryClass({
|
|
462
|
+
type: 'limit',
|
|
463
|
+
query: this.ast,
|
|
464
|
+
limit,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
options(options: QueryAST.QueryOptions): Query$.Any {
|
|
469
|
+
return new QueryClass({
|
|
470
|
+
type: 'options',
|
|
471
|
+
query: this.ast,
|
|
472
|
+
options,
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export const Query1: typeof Query$ = QueryClass;
|
|
478
|
+
export { Query1 as Query };
|
|
479
|
+
|
|
480
|
+
const RefTypeId: unique symbol = Symbol('@dxos/echo-query/Ref');
|
|
481
|
+
const isRef = (obj: any): obj is Ref.Ref<any> => {
|
|
482
|
+
return obj && typeof obj === 'object' && RefTypeId in obj;
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
const makeTypeDxn = (typename: string) => {
|
|
486
|
+
assertArgument(typeof typename === 'string', 'typename');
|
|
487
|
+
assertArgument(!typename.startsWith('dxn:'), 'typename');
|
|
488
|
+
return `dxn:type:${typename}`;
|
|
489
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { Filter, Order, Query } from '@dxos/echo';
|
|
8
|
+
import { trim } from '@dxos/util';
|
|
9
|
+
|
|
10
|
+
import { QuerySandbox } from './query-sandbox';
|
|
11
|
+
|
|
12
|
+
describe('QuerySandbox', () => {
|
|
13
|
+
const sandbox = new QuerySandbox();
|
|
14
|
+
beforeAll(() => sandbox.open());
|
|
15
|
+
afterAll(() => sandbox.close());
|
|
16
|
+
|
|
17
|
+
test('works', { timeout: 10_000 }, async () => {
|
|
18
|
+
const ast = sandbox.eval(trim`
|
|
19
|
+
Query.select(Filter.typename('dxos.org/type/Person'))
|
|
20
|
+
`);
|
|
21
|
+
expect(ast).toEqual(Query.select(Filter.typename('dxos.org/type/Person')).ast);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('works with just Filter passed in', () => {
|
|
25
|
+
const ast = sandbox.eval(trim`
|
|
26
|
+
Filter.typename('dxos.org/type/Person')
|
|
27
|
+
`);
|
|
28
|
+
expect(ast).toEqual(Query.select(Filter.typename('dxos.org/type/Person')).ast);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('Order', () => {
|
|
32
|
+
const ast = sandbox.eval(trim`
|
|
33
|
+
Query.type('dxos.org/type/Person').orderBy(Order.property('name', 'desc'))
|
|
34
|
+
`);
|
|
35
|
+
expect(ast).toEqual(Query.type('dxos.org/type/Person').orderBy(Order.property('name', 'desc')).ast);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('traversal', () => {
|
|
39
|
+
const ast = sandbox.eval(trim`
|
|
40
|
+
Query.select(Filter.type('dxos.org/type/Person', { jobTitle: 'investor' }))
|
|
41
|
+
.reference('organization')
|
|
42
|
+
.targetOf('dxos.org/relation/HasSubject')
|
|
43
|
+
.source()
|
|
44
|
+
`);
|
|
45
|
+
|
|
46
|
+
expect(ast).toEqual(
|
|
47
|
+
Query.select(Filter.type('dxos.org/type/Person', { jobTitle: 'investor' }))
|
|
48
|
+
.reference('organization')
|
|
49
|
+
.targetOf('dxos.org/relation/HasSubject')
|
|
50
|
+
.source().ast,
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Resource } from '@dxos/context';
|
|
6
|
+
import { Query, type QueryAST } from '@dxos/echo';
|
|
7
|
+
import { trim } from '@dxos/util';
|
|
8
|
+
import { type QuickJSRuntime, type QuickJSWASMModule, createQuickJS } from '@dxos/vendor-quickjs';
|
|
9
|
+
|
|
10
|
+
import envCode from '#query-lite?raw';
|
|
11
|
+
|
|
12
|
+
import { unwrapResult } from './quickjs';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Evaluates queries written in JavaScript using QuickJS.
|
|
16
|
+
* Queries are expected to use the echo Query API.
|
|
17
|
+
* `Query`, `Filter` and `Order` are provided as globals.
|
|
18
|
+
*/
|
|
19
|
+
export class QuerySandbox extends Resource {
|
|
20
|
+
// Caching the wasm module.
|
|
21
|
+
private static quickJS: Promise<QuickJSWASMModule> | null = null;
|
|
22
|
+
static getQuickJS() {
|
|
23
|
+
if (!QuerySandbox.quickJS) {
|
|
24
|
+
QuerySandbox.quickJS = createQuickJS();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return QuerySandbox.quickJS;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
#runtime!: QuickJSRuntime;
|
|
31
|
+
|
|
32
|
+
protected override async _open() {
|
|
33
|
+
const quickJS = await QuerySandbox.getQuickJS();
|
|
34
|
+
this.#runtime = quickJS.newRuntime({
|
|
35
|
+
moduleLoader: (moduleName, _context) => {
|
|
36
|
+
switch (moduleName) {
|
|
37
|
+
case 'dxos:query-lite':
|
|
38
|
+
return envCode;
|
|
39
|
+
default:
|
|
40
|
+
throw new Error(`Module not found: ${moduleName}`);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
protected override async _close() {
|
|
47
|
+
this.#runtime.dispose();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Evaluates the query code.
|
|
52
|
+
* @param queryCode Example: `Query.select(Filter.typename('dxos.org/type/Person'))`
|
|
53
|
+
*/
|
|
54
|
+
eval(queryCode: string): QueryAST.Query {
|
|
55
|
+
using context = this.#runtime.newContext();
|
|
56
|
+
const globals = trim`
|
|
57
|
+
import { Filter, Order, Query } from 'dxos:query-lite';
|
|
58
|
+
globalThis.Filter = Filter;
|
|
59
|
+
globalThis.Order = Order;
|
|
60
|
+
globalThis.Query = Query;
|
|
61
|
+
`;
|
|
62
|
+
|
|
63
|
+
unwrapResult(context, context.evalCode(globals)).dispose();
|
|
64
|
+
using query = unwrapResult(context, context.evalCode(queryCode));
|
|
65
|
+
const result = context.dump(query);
|
|
66
|
+
if ('~Filter' in result) {
|
|
67
|
+
return Query.select(result).ast;
|
|
68
|
+
} else {
|
|
69
|
+
return result.ast;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { createQuickJS } from '@dxos/vendor-quickjs';
|
|
8
|
+
|
|
9
|
+
import { unwrapResult } from './quickjs';
|
|
10
|
+
|
|
11
|
+
test('works', async () => {
|
|
12
|
+
const QuickJS = await createQuickJS();
|
|
13
|
+
using context = QuickJS.newContext();
|
|
14
|
+
|
|
15
|
+
using world = context.newString('world');
|
|
16
|
+
context.setProp(context.global, 'NAME', world);
|
|
17
|
+
|
|
18
|
+
using result = unwrapResult(context, context.evalCode(`"Hello " + NAME + "!"`));
|
|
19
|
+
expect(context.dump(result)).toBe('Hello world!');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('errors get propagated', async () => {
|
|
23
|
+
const QuickJS = await createQuickJS();
|
|
24
|
+
using runtime = QuickJS.newRuntime();
|
|
25
|
+
using context = runtime.newContext();
|
|
26
|
+
expect(() => unwrapResult(context, context.evalCode(`throw new Error('test')`))).toThrow();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('global variables are shared in a context', async () => {
|
|
30
|
+
const QuickJS = await createQuickJS();
|
|
31
|
+
const context = QuickJS.newContext();
|
|
32
|
+
|
|
33
|
+
unwrapResult(context, context.evalCode('globalThis.name = "world"')).dispose();
|
|
34
|
+
using result = unwrapResult(context, context.evalCode(`"Hello " + name + "!"`));
|
|
35
|
+
expect(context.dump(result)).toBe('Hello world!');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('global variables are shared in a context created from runtime', async () => {
|
|
39
|
+
const QuickJS = await createQuickJS();
|
|
40
|
+
using runtime = QuickJS.newRuntime();
|
|
41
|
+
using context = runtime.newContext();
|
|
42
|
+
|
|
43
|
+
unwrapResult(context, context.evalCode('globalThis.name = "world"'));
|
|
44
|
+
using result = unwrapResult(context, context.evalCode(`"Hello " + name + "!"`));
|
|
45
|
+
expect(context.dump(result)).toBe('Hello world!');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('load module', async () => {
|
|
49
|
+
const QuickJS = await createQuickJS();
|
|
50
|
+
using runtime = QuickJS.newRuntime({
|
|
51
|
+
moduleLoader: (name, _context) => {
|
|
52
|
+
switch (name) {
|
|
53
|
+
case 'test:name':
|
|
54
|
+
return `
|
|
55
|
+
export const name = 'world';
|
|
56
|
+
`;
|
|
57
|
+
default:
|
|
58
|
+
throw new Error('unknown module');
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
using context = runtime.newContext();
|
|
63
|
+
|
|
64
|
+
unwrapResult(context, context.evalCode('import { name } from "test:name"; globalThis.name = name;')).dispose();
|
|
65
|
+
using result = unwrapResult(context, context.evalCode(`"Hello " + name + "!"`));
|
|
66
|
+
expect(context.dump(result)).toBe('Hello world!');
|
|
67
|
+
});
|