@dxos/echo-query 0.8.4-main.72ec0f3 → 0.8.4-main.7996785055
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/dist/lib/{browser → neutral}/index.mjs +159 -3
- package/dist/lib/{node-esm → neutral}/index.mjs.map +4 -4
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/query-lite/index.d.ts +9921 -0
- package/dist/query-lite/index.d.ts.map +1 -0
- package/dist/query-lite/index.js +427 -375
- package/dist/query-lite/index.js.map +1 -0
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/query-lite/query-lite.d.ts +4 -4
- package/dist/types/src/query-lite/query-lite.d.ts.map +1 -1
- 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 +1 -1
- package/dist/types/src/sandbox/quickjs.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +21 -16
- package/src/index.ts +1 -0
- package/src/parser/query.test.ts +29 -29
- package/src/query-lite/query-lite.ts +166 -59
- package/src/sandbox/index.ts +5 -0
- package/src/sandbox/query-sandbox.test.ts +10 -10
- package/src/sandbox/query-sandbox.ts +1 -1
- package/src/sandbox/quickjs.ts +1 -2
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/node-esm/index.mjs +0 -563
- package/dist/lib/node-esm/meta.json +0 -1
|
@@ -4,8 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import type * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
|
-
import type { Filter
|
|
8
|
-
import type * as Echo from '@dxos/echo';
|
|
7
|
+
import type { Filter as Filter$, Order as Order$, Query as Query$, Ref } from '@dxos/echo';
|
|
9
8
|
import type { ForeignKey, QueryAST } from '@dxos/echo-protocol';
|
|
10
9
|
import { assertArgument } from '@dxos/invariant';
|
|
11
10
|
import type { DXN, ObjectId } from '@dxos/keys';
|
|
@@ -14,10 +13,12 @@ import type { DXN, ObjectId } from '@dxos/keys';
|
|
|
14
13
|
// Light-weight implementation of query execution.
|
|
15
14
|
//
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
private static variance: Echo.Order<any>['~Order'] = {} as Echo.Order<any>['~Order'];
|
|
16
|
+
// TODO(wittjosiah): The `export * as ...` syntax causes tsdown to genereate multiple files which breaks the sandbox.
|
|
19
17
|
|
|
20
|
-
|
|
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 {
|
|
21
22
|
return typeof value === 'object' && value !== null && '~Order' in value;
|
|
22
23
|
}
|
|
23
24
|
|
|
@@ -27,26 +28,31 @@ class OrderClass implements Echo.Order<any> {
|
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
namespace Order1 {
|
|
30
|
-
export const natural:
|
|
31
|
-
export const property = <T>(property: keyof T & string, direction: QueryAST.OrderDirection):
|
|
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> =>
|
|
32
33
|
new OrderClass({
|
|
33
34
|
kind: 'property',
|
|
34
35
|
property,
|
|
35
36
|
direction,
|
|
36
37
|
});
|
|
38
|
+
export const rank = <T>(direction: QueryAST.OrderDirection = 'desc'): Order$.Order<T> =>
|
|
39
|
+
new OrderClass({
|
|
40
|
+
kind: 'rank',
|
|
41
|
+
direction,
|
|
42
|
+
});
|
|
37
43
|
}
|
|
38
44
|
|
|
39
|
-
const Order2: typeof
|
|
45
|
+
const Order2: typeof Order$ = Order1;
|
|
40
46
|
export { Order2 as Order };
|
|
41
47
|
|
|
42
|
-
class FilterClass implements
|
|
43
|
-
private static variance:
|
|
48
|
+
class FilterClass implements Filter$.Any {
|
|
49
|
+
private static 'variance': Filter$.Any['~Filter'] = {} as Filter$.Any['~Filter'];
|
|
44
50
|
|
|
45
|
-
static is(value: unknown): value is
|
|
51
|
+
static is(value: unknown): value is Filter$.Any {
|
|
46
52
|
return typeof value === 'object' && value !== null && '~Filter' in value;
|
|
47
53
|
}
|
|
48
54
|
|
|
49
|
-
static fromAst(ast: QueryAST.Filter): Filter
|
|
55
|
+
static fromAst(ast: QueryAST.Filter): Filter$.Any {
|
|
50
56
|
return new FilterClass(ast);
|
|
51
57
|
}
|
|
52
58
|
|
|
@@ -77,7 +83,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
77
83
|
});
|
|
78
84
|
}
|
|
79
85
|
|
|
80
|
-
static
|
|
86
|
+
static id(...ids: ObjectId[]): Filter$.Any {
|
|
81
87
|
// assertArgument(
|
|
82
88
|
// ids.every((id) => ObjectId.isValid(id)),
|
|
83
89
|
// 'ids',
|
|
@@ -98,8 +104,8 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
98
104
|
|
|
99
105
|
static type<S extends Schema.Schema.All>(
|
|
100
106
|
schema: S | string,
|
|
101
|
-
props?:
|
|
102
|
-
):
|
|
107
|
+
props?: Filter$.Props<Schema.Schema.Type<S>>,
|
|
108
|
+
): Filter$.Filter<Schema.Schema.Type<S>> {
|
|
103
109
|
if (typeof schema !== 'string') {
|
|
104
110
|
throw new TypeError('expected typename as the first paramter');
|
|
105
111
|
}
|
|
@@ -110,7 +116,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
110
116
|
});
|
|
111
117
|
}
|
|
112
118
|
|
|
113
|
-
static typename(typename: string):
|
|
119
|
+
static typename(typename: string): Filter$.Any {
|
|
114
120
|
return new FilterClass({
|
|
115
121
|
type: 'object',
|
|
116
122
|
typename: makeTypeDxn(typename),
|
|
@@ -118,7 +124,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
118
124
|
});
|
|
119
125
|
}
|
|
120
126
|
|
|
121
|
-
static typeDXN(dxn: DXN):
|
|
127
|
+
static typeDXN(dxn: DXN): Filter$.Any {
|
|
122
128
|
return new FilterClass({
|
|
123
129
|
type: 'object',
|
|
124
130
|
typename: dxn.toString(),
|
|
@@ -126,14 +132,14 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
126
132
|
});
|
|
127
133
|
}
|
|
128
134
|
|
|
129
|
-
static tag(tag: string):
|
|
135
|
+
static tag(tag: string): Filter$.Any {
|
|
130
136
|
return new FilterClass({
|
|
131
137
|
type: 'tag',
|
|
132
138
|
tag,
|
|
133
139
|
});
|
|
134
140
|
}
|
|
135
141
|
|
|
136
|
-
static props<T>(props:
|
|
142
|
+
static props<T>(props: Filter$.Props<T>): Filter$.Filter<T> {
|
|
137
143
|
return new FilterClass({
|
|
138
144
|
type: 'object',
|
|
139
145
|
typename: null,
|
|
@@ -141,7 +147,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
141
147
|
});
|
|
142
148
|
}
|
|
143
149
|
|
|
144
|
-
static text(text: string, options?:
|
|
150
|
+
static text(text: string, options?: Filter$.TextSearchOptions): Filter$.Any {
|
|
145
151
|
return new FilterClass({
|
|
146
152
|
type: 'text-search',
|
|
147
153
|
text,
|
|
@@ -152,7 +158,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
152
158
|
static foreignKeys<S extends Schema.Schema.All>(
|
|
153
159
|
schema: S | string,
|
|
154
160
|
keys: ForeignKey[],
|
|
155
|
-
):
|
|
161
|
+
): Filter$.Filter<Schema.Schema.Type<S>> {
|
|
156
162
|
assertArgument(typeof schema === 'string', 'schema');
|
|
157
163
|
assertArgument(!schema.startsWith('dxn:'), 'schema');
|
|
158
164
|
return new FilterClass({
|
|
@@ -163,7 +169,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
163
169
|
});
|
|
164
170
|
}
|
|
165
171
|
|
|
166
|
-
static eq<T>(value: T):
|
|
172
|
+
static eq<T>(value: T): Filter$.Filter<T | undefined> {
|
|
167
173
|
if (!isRef(value) && typeof value === 'object' && value !== null) {
|
|
168
174
|
throw new TypeError('Cannot use object as a value for eq filter');
|
|
169
175
|
}
|
|
@@ -175,7 +181,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
175
181
|
});
|
|
176
182
|
}
|
|
177
183
|
|
|
178
|
-
static neq<T>(value: T):
|
|
184
|
+
static neq<T>(value: T): Filter$.Filter<T | undefined> {
|
|
179
185
|
return new FilterClass({
|
|
180
186
|
type: 'compare',
|
|
181
187
|
operator: 'neq',
|
|
@@ -183,7 +189,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
183
189
|
});
|
|
184
190
|
}
|
|
185
191
|
|
|
186
|
-
static gt<T>(value: T):
|
|
192
|
+
static gt<T>(value: T): Filter$.Filter<T | undefined> {
|
|
187
193
|
return new FilterClass({
|
|
188
194
|
type: 'compare',
|
|
189
195
|
operator: 'gt',
|
|
@@ -191,7 +197,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
191
197
|
});
|
|
192
198
|
}
|
|
193
199
|
|
|
194
|
-
static gte<T>(value: T):
|
|
200
|
+
static gte<T>(value: T): Filter$.Filter<T | undefined> {
|
|
195
201
|
return new FilterClass({
|
|
196
202
|
type: 'compare',
|
|
197
203
|
operator: 'gte',
|
|
@@ -199,7 +205,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
199
205
|
});
|
|
200
206
|
}
|
|
201
207
|
|
|
202
|
-
static lt<T>(value: T):
|
|
208
|
+
static lt<T>(value: T): Filter$.Filter<T | undefined> {
|
|
203
209
|
return new FilterClass({
|
|
204
210
|
type: 'compare',
|
|
205
211
|
operator: 'lt',
|
|
@@ -207,7 +213,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
207
213
|
});
|
|
208
214
|
}
|
|
209
215
|
|
|
210
|
-
static lte<T>(value: T):
|
|
216
|
+
static lte<T>(value: T): Filter$.Filter<T | undefined> {
|
|
211
217
|
return new FilterClass({
|
|
212
218
|
type: 'compare',
|
|
213
219
|
operator: 'lte',
|
|
@@ -215,21 +221,21 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
215
221
|
});
|
|
216
222
|
}
|
|
217
223
|
|
|
218
|
-
static in<T>(...values: T[]):
|
|
224
|
+
static in<T>(...values: T[]): Filter$.Filter<T | undefined> {
|
|
219
225
|
return new FilterClass({
|
|
220
226
|
type: 'in',
|
|
221
227
|
values,
|
|
222
228
|
});
|
|
223
229
|
}
|
|
224
230
|
|
|
225
|
-
static contains<T>(value: T):
|
|
231
|
+
static contains<T>(value: T): Filter$.Filter<readonly T[] | undefined> {
|
|
226
232
|
return new FilterClass({
|
|
227
233
|
type: 'contains',
|
|
228
234
|
value,
|
|
229
235
|
});
|
|
230
236
|
}
|
|
231
237
|
|
|
232
|
-
static between<T>(from: T, to: T):
|
|
238
|
+
static between<T>(from: T, to: T): Filter$.Filter<unknown> {
|
|
233
239
|
return new FilterClass({
|
|
234
240
|
type: 'range',
|
|
235
241
|
from,
|
|
@@ -237,21 +243,51 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
237
243
|
});
|
|
238
244
|
}
|
|
239
245
|
|
|
240
|
-
static
|
|
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 #timeRangeFilter(
|
|
255
|
+
field: 'updatedAt' | 'createdAt',
|
|
256
|
+
range: { after?: Date | number; before?: Date | number },
|
|
257
|
+
): Filter$.Any {
|
|
258
|
+
const toMs = (d: Date | number) => (typeof d === 'number' ? d : d.getTime());
|
|
259
|
+
const filters: Filter$.Any[] = [];
|
|
260
|
+
if (range.after != null) {
|
|
261
|
+
filters.push(new FilterClass({ type: 'timestamp', field, operator: 'gte', value: toMs(range.after) }));
|
|
262
|
+
}
|
|
263
|
+
if (range.before != null) {
|
|
264
|
+
filters.push(new FilterClass({ type: 'timestamp', field, operator: 'lte', value: toMs(range.before) }));
|
|
265
|
+
}
|
|
266
|
+
if (filters.length === 0) {
|
|
267
|
+
return FilterClass.everything();
|
|
268
|
+
}
|
|
269
|
+
return filters.length === 1 ? filters[0] : FilterClass.and(...filters);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
static not<F extends Filter$.Any>(filter: F): Filter$.Filter<Filter$.Type<F>> {
|
|
241
273
|
return new FilterClass({
|
|
242
274
|
type: 'not',
|
|
243
275
|
filter: filter.ast,
|
|
244
276
|
});
|
|
245
277
|
}
|
|
246
278
|
|
|
247
|
-
static and<
|
|
279
|
+
static and<Filters extends readonly Filter$.Any[]>(
|
|
280
|
+
...filters: Filters
|
|
281
|
+
): Filter$.Filter<Filter$.Type<Filters[number]>> {
|
|
248
282
|
return new FilterClass({
|
|
249
283
|
type: 'and',
|
|
250
284
|
filters: filters.map((f) => f.ast),
|
|
251
285
|
});
|
|
252
286
|
}
|
|
253
287
|
|
|
254
|
-
static or<
|
|
288
|
+
static or<Filters extends readonly Filter$.Any[]>(
|
|
289
|
+
...filters: Filters
|
|
290
|
+
): Filter$.Filter<Filter$.Type<Filters[number]>> {
|
|
255
291
|
return new FilterClass({
|
|
256
292
|
type: 'or',
|
|
257
293
|
filters: filters.map((f) => f.ast),
|
|
@@ -263,7 +299,7 @@ class FilterClass implements Echo.Filter<any> {
|
|
|
263
299
|
'~Filter' = FilterClass.variance;
|
|
264
300
|
}
|
|
265
301
|
|
|
266
|
-
export const Filter1: typeof
|
|
302
|
+
export const Filter1: typeof Filter$ = FilterClass;
|
|
267
303
|
export { Filter1 as Filter };
|
|
268
304
|
|
|
269
305
|
/**
|
|
@@ -272,7 +308,7 @@ export { Filter1 as Filter };
|
|
|
272
308
|
// TODO(dmaretskyi): Filter only properties that are references (or optional references, or unions that include references).
|
|
273
309
|
type RefPropKey<T> = keyof T & string;
|
|
274
310
|
|
|
275
|
-
const propsFilterToAst = (predicates:
|
|
311
|
+
const propsFilterToAst = (predicates: Filter$.Props<any>): Pick<QueryAST.FilterObject, 'id' | 'props'> => {
|
|
276
312
|
let idFilter: readonly ObjectId[] | undefined;
|
|
277
313
|
if ('id' in predicates) {
|
|
278
314
|
assertArgument(
|
|
@@ -317,32 +353,32 @@ const processPredicate = (predicate: any): QueryAST.Filter => {
|
|
|
317
353
|
return FilterClass.eq(predicate).ast;
|
|
318
354
|
};
|
|
319
355
|
|
|
320
|
-
class QueryClass implements
|
|
321
|
-
private static variance:
|
|
356
|
+
class QueryClass implements Query$.Any {
|
|
357
|
+
private static 'variance': Query$.Any['~Query'] = {} as Query$.Any['~Query'];
|
|
322
358
|
|
|
323
|
-
static is(value: unknown): value is
|
|
359
|
+
static is(value: unknown): value is Query$.Any {
|
|
324
360
|
return typeof value === 'object' && value !== null && '~Query' in value;
|
|
325
361
|
}
|
|
326
362
|
|
|
327
|
-
static fromAst(ast: QueryAST.Query):
|
|
363
|
+
static fromAst(ast: QueryAST.Query): Query$.Any {
|
|
328
364
|
return new QueryClass(ast);
|
|
329
365
|
}
|
|
330
366
|
|
|
331
|
-
static select<F extends
|
|
367
|
+
static select<F extends Filter$.Any>(filter: F): Query$.Query<Filter$.Type<F>> {
|
|
332
368
|
return new QueryClass({
|
|
333
369
|
type: 'select',
|
|
334
370
|
filter: filter.ast,
|
|
335
371
|
});
|
|
336
372
|
}
|
|
337
373
|
|
|
338
|
-
static type(schema: Schema.Schema.All | string, predicates?:
|
|
374
|
+
static type(schema: Schema.Schema.All | string, predicates?: Filter$.Props<unknown>): Query$.Any {
|
|
339
375
|
return new QueryClass({
|
|
340
376
|
type: 'select',
|
|
341
377
|
filter: FilterClass.type(schema, predicates).ast,
|
|
342
378
|
});
|
|
343
379
|
}
|
|
344
380
|
|
|
345
|
-
static all(...queries: Query
|
|
381
|
+
static all(...queries: Query$.Any[]): Query$.Any {
|
|
346
382
|
if (queries.length === 0) {
|
|
347
383
|
throw new TypeError(
|
|
348
384
|
'Query.all combines results of multiple queries, to query all objects use Query.select(Filter.everything())',
|
|
@@ -354,7 +390,7 @@ class QueryClass implements Echo.Query<any> {
|
|
|
354
390
|
});
|
|
355
391
|
}
|
|
356
392
|
|
|
357
|
-
static without<T>(source: Query<T>, exclude: Query<T>): Query<T> {
|
|
393
|
+
static without<T>(source: Query$.Query<T>, exclude: Query$.Query<T>): Query$.Query<T> {
|
|
358
394
|
return new QueryClass({
|
|
359
395
|
type: 'set-difference',
|
|
360
396
|
source: source.ast,
|
|
@@ -362,11 +398,20 @@ class QueryClass implements Echo.Query<any> {
|
|
|
362
398
|
});
|
|
363
399
|
}
|
|
364
400
|
|
|
401
|
+
static from(source: any, options?: { includeFeeds?: boolean }): Query$.Any {
|
|
402
|
+
const baseQuery: QueryAST.Query = {
|
|
403
|
+
type: 'select',
|
|
404
|
+
filter: FilterClass.everything().ast,
|
|
405
|
+
};
|
|
406
|
+
const wrapper = new QueryClass(baseQuery);
|
|
407
|
+
return wrapper.from(source, options);
|
|
408
|
+
}
|
|
409
|
+
|
|
365
410
|
constructor(public readonly ast: QueryAST.Query) {}
|
|
366
411
|
|
|
367
412
|
'~Query' = QueryClass.variance;
|
|
368
413
|
|
|
369
|
-
select(filter: Filter
|
|
414
|
+
select(filter: Filter$.Any | Filter$.Props<any>): Query$.Any {
|
|
370
415
|
if (FilterClass.is(filter)) {
|
|
371
416
|
return new QueryClass({
|
|
372
417
|
type: 'filter',
|
|
@@ -382,7 +427,7 @@ class QueryClass implements Echo.Query<any> {
|
|
|
382
427
|
}
|
|
383
428
|
}
|
|
384
429
|
|
|
385
|
-
reference(key: string): Query
|
|
430
|
+
reference(key: string): Query$.Any {
|
|
386
431
|
return new QueryClass({
|
|
387
432
|
type: 'reference-traversal',
|
|
388
433
|
anchor: this.ast,
|
|
@@ -390,36 +435,40 @@ class QueryClass implements Echo.Query<any> {
|
|
|
390
435
|
});
|
|
391
436
|
}
|
|
392
437
|
|
|
393
|
-
referencedBy(target
|
|
394
|
-
|
|
395
|
-
|
|
438
|
+
referencedBy(target?: Schema.Schema.All | string, key?: string): Query$.Any {
|
|
439
|
+
const typename =
|
|
440
|
+
target !== undefined
|
|
441
|
+
? (assertArgument(typeof target === 'string', 'target'),
|
|
442
|
+
assertArgument(!target.startsWith('dxn:'), 'target'),
|
|
443
|
+
target)
|
|
444
|
+
: null;
|
|
396
445
|
return new QueryClass({
|
|
397
446
|
type: 'incoming-references',
|
|
398
447
|
anchor: this.ast,
|
|
399
|
-
property: key,
|
|
400
|
-
typename
|
|
448
|
+
property: key ?? null,
|
|
449
|
+
typename,
|
|
401
450
|
});
|
|
402
451
|
}
|
|
403
452
|
|
|
404
|
-
sourceOf(relation
|
|
453
|
+
sourceOf(relation?: Schema.Schema.All | string, predicates?: Filter$.Props<unknown> | undefined): Query$.Any {
|
|
405
454
|
return new QueryClass({
|
|
406
455
|
type: 'relation',
|
|
407
456
|
anchor: this.ast,
|
|
408
457
|
direction: 'outgoing',
|
|
409
|
-
filter: FilterClass.type(relation, predicates).ast,
|
|
458
|
+
filter: relation !== undefined ? FilterClass.type(relation, predicates).ast : undefined,
|
|
410
459
|
});
|
|
411
460
|
}
|
|
412
461
|
|
|
413
|
-
targetOf(relation
|
|
462
|
+
targetOf(relation?: Schema.Schema.All | string, predicates?: Filter$.Props<unknown> | undefined): Query$.Any {
|
|
414
463
|
return new QueryClass({
|
|
415
464
|
type: 'relation',
|
|
416
465
|
anchor: this.ast,
|
|
417
466
|
direction: 'incoming',
|
|
418
|
-
filter: FilterClass.type(relation, predicates).ast,
|
|
467
|
+
filter: relation !== undefined ? FilterClass.type(relation, predicates).ast : undefined,
|
|
419
468
|
});
|
|
420
469
|
}
|
|
421
470
|
|
|
422
|
-
source(): Query
|
|
471
|
+
source(): Query$.Any {
|
|
423
472
|
return new QueryClass({
|
|
424
473
|
type: 'relation-traversal',
|
|
425
474
|
anchor: this.ast,
|
|
@@ -427,7 +476,7 @@ class QueryClass implements Echo.Query<any> {
|
|
|
427
476
|
});
|
|
428
477
|
}
|
|
429
478
|
|
|
430
|
-
target(): Query
|
|
479
|
+
target(): Query$.Any {
|
|
431
480
|
return new QueryClass({
|
|
432
481
|
type: 'relation-traversal',
|
|
433
482
|
anchor: this.ast,
|
|
@@ -435,7 +484,23 @@ class QueryClass implements Echo.Query<any> {
|
|
|
435
484
|
});
|
|
436
485
|
}
|
|
437
486
|
|
|
438
|
-
|
|
487
|
+
parent(): Query$.Any {
|
|
488
|
+
return new QueryClass({
|
|
489
|
+
type: 'hierarchy-traversal',
|
|
490
|
+
anchor: this.ast,
|
|
491
|
+
direction: 'to-parent',
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
children(): Query$.Any {
|
|
496
|
+
return new QueryClass({
|
|
497
|
+
type: 'hierarchy-traversal',
|
|
498
|
+
anchor: this.ast,
|
|
499
|
+
direction: 'to-children',
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
orderBy(...order: Order$.Any[]): Query$.Any {
|
|
439
504
|
return new QueryClass({
|
|
440
505
|
type: 'order',
|
|
441
506
|
query: this.ast,
|
|
@@ -443,7 +508,40 @@ class QueryClass implements Echo.Query<any> {
|
|
|
443
508
|
});
|
|
444
509
|
}
|
|
445
510
|
|
|
446
|
-
|
|
511
|
+
limit(limit: number): Query$.Any {
|
|
512
|
+
return new QueryClass({
|
|
513
|
+
type: 'limit',
|
|
514
|
+
query: this.ast,
|
|
515
|
+
limit,
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
from(arg: any, options?: { includeFeeds?: boolean }): Query$.Any {
|
|
520
|
+
if (arg === 'all-accessible-spaces') {
|
|
521
|
+
return new QueryClass({
|
|
522
|
+
type: 'from',
|
|
523
|
+
query: this.ast,
|
|
524
|
+
from: {
|
|
525
|
+
_tag: 'scope',
|
|
526
|
+
scope: {
|
|
527
|
+
...(options?.includeFeeds ? { allQueuesFromSpaces: true } : {}),
|
|
528
|
+
},
|
|
529
|
+
},
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
if (_isScopeLike(arg)) {
|
|
534
|
+
return new QueryClass({
|
|
535
|
+
type: 'from',
|
|
536
|
+
query: this.ast,
|
|
537
|
+
from: { _tag: 'scope', scope: arg },
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
throw new TypeError('Database and Feed objects are not supported in query-lite sandbox');
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
options(options: QueryAST.QueryOptions): Query$.Any {
|
|
447
545
|
return new QueryClass({
|
|
448
546
|
type: 'options',
|
|
449
547
|
query: this.ast,
|
|
@@ -452,7 +550,7 @@ class QueryClass implements Echo.Query<any> {
|
|
|
452
550
|
}
|
|
453
551
|
}
|
|
454
552
|
|
|
455
|
-
export const Query1: typeof
|
|
553
|
+
export const Query1: typeof Query$ = QueryClass;
|
|
456
554
|
export { Query1 as Query };
|
|
457
555
|
|
|
458
556
|
const RefTypeId: unique symbol = Symbol('@dxos/echo-query/Ref');
|
|
@@ -465,3 +563,12 @@ const makeTypeDxn = (typename: string) => {
|
|
|
465
563
|
assertArgument(!typename.startsWith('dxn:'), 'typename');
|
|
466
564
|
return `dxn:type:${typename}`;
|
|
467
565
|
};
|
|
566
|
+
|
|
567
|
+
const SCOPE_KEYS = new Set(['spaceIds', 'queues', 'allQueuesFromSpaces']);
|
|
568
|
+
|
|
569
|
+
const _isScopeLike = (value: unknown): value is QueryAST.Scope => {
|
|
570
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
571
|
+
return false;
|
|
572
|
+
}
|
|
573
|
+
return Object.keys(value).every((key) => SCOPE_KEYS.has(key));
|
|
574
|
+
};
|
|
@@ -16,37 +16,37 @@ describe('QuerySandbox', () => {
|
|
|
16
16
|
|
|
17
17
|
test('works', { timeout: 10_000 }, async () => {
|
|
18
18
|
const ast = sandbox.eval(trim`
|
|
19
|
-
Query.select(Filter.typename('dxos.
|
|
19
|
+
Query.select(Filter.typename('org.dxos.type.person'))
|
|
20
20
|
`);
|
|
21
|
-
expect(ast).toEqual(Query.select(Filter.typename('dxos.
|
|
21
|
+
expect(ast).toEqual(Query.select(Filter.typename('org.dxos.type.person')).ast);
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
test('works with just Filter passed in', () => {
|
|
25
25
|
const ast = sandbox.eval(trim`
|
|
26
|
-
Filter.typename('dxos.
|
|
26
|
+
Filter.typename('org.dxos.type.person')
|
|
27
27
|
`);
|
|
28
|
-
expect(ast).toEqual(Query.select(Filter.typename('dxos.
|
|
28
|
+
expect(ast).toEqual(Query.select(Filter.typename('org.dxos.type.person')).ast);
|
|
29
29
|
});
|
|
30
30
|
|
|
31
31
|
test('Order', () => {
|
|
32
32
|
const ast = sandbox.eval(trim`
|
|
33
|
-
Query.type('dxos.
|
|
33
|
+
Query.type('org.dxos.type.person').orderBy(Order.property('name', 'desc'))
|
|
34
34
|
`);
|
|
35
|
-
expect(ast).toEqual(Query.type('dxos.
|
|
35
|
+
expect(ast).toEqual(Query.type('org.dxos.type.person').orderBy(Order.property('name', 'desc')).ast);
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
test('traversal', () => {
|
|
39
39
|
const ast = sandbox.eval(trim`
|
|
40
|
-
Query.select(Filter.type('dxos.
|
|
40
|
+
Query.select(Filter.type('org.dxos.type.person', { jobTitle: 'investor' }))
|
|
41
41
|
.reference('organization')
|
|
42
|
-
.targetOf('dxos.
|
|
42
|
+
.targetOf('org.dxos.relation.has-subject')
|
|
43
43
|
.source()
|
|
44
44
|
`);
|
|
45
45
|
|
|
46
46
|
expect(ast).toEqual(
|
|
47
|
-
Query.select(Filter.type('dxos.
|
|
47
|
+
Query.select(Filter.type('org.dxos.type.person', { jobTitle: 'investor' }))
|
|
48
48
|
.reference('organization')
|
|
49
|
-
.targetOf('dxos.
|
|
49
|
+
.targetOf('org.dxos.relation.has-subject')
|
|
50
50
|
.source().ast,
|
|
51
51
|
);
|
|
52
52
|
});
|
|
@@ -49,7 +49,7 @@ export class QuerySandbox extends Resource {
|
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
51
|
* Evaluates the query code.
|
|
52
|
-
* @param queryCode Example: `Query.select(Filter.typename('dxos.
|
|
52
|
+
* @param queryCode Example: `Query.select(Filter.typename('org.dxos.type.person'))`
|
|
53
53
|
*/
|
|
54
54
|
eval(queryCode: string): QueryAST.Query {
|
|
55
55
|
using context = this.#runtime.newContext();
|
package/src/sandbox/quickjs.ts
CHANGED
|
@@ -17,8 +17,7 @@ export const unwrapResult = <T>(context: QuickJSContext, result: SuccessOrFail<T
|
|
|
17
17
|
if (
|
|
18
18
|
typeof contextError === 'object' &&
|
|
19
19
|
typeof contextError.name === 'string' &&
|
|
20
|
-
typeof contextError.message === 'string'
|
|
21
|
-
typeof contextError.stack === 'string'
|
|
20
|
+
typeof contextError.message === 'string'
|
|
22
21
|
) {
|
|
23
22
|
const error = new Error(contextError.message);
|
|
24
23
|
Object.defineProperty(error, 'name', { value: contextError.name });
|