@dxos/echo-protocol 0.8.4-main.e098934 → 0.8.4-main.e250131250

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/package.json CHANGED
@@ -1,20 +1,22 @@
1
1
  {
2
2
  "name": "@dxos/echo-protocol",
3
- "version": "0.8.4-main.e098934",
3
+ "version": "0.8.4-main.e250131250",
4
4
  "description": "Core ECHO APIs.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
- "repository": "github:dxos/dxos",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
8
11
  "license": "MIT",
9
12
  "author": "DXOS.org",
10
- "sideEffects": true,
13
+ "sideEffects": false,
11
14
  "type": "module",
12
15
  "exports": {
13
16
  ".": {
14
17
  "source": "./src/index.ts",
15
18
  "types": "./dist/types/src/index.d.ts",
16
- "browser": "./dist/lib/browser/index.mjs",
17
- "node": "./dist/lib/node-esm/index.mjs"
19
+ "default": "./dist/lib/neutral/index.mjs"
18
20
  }
19
21
  },
20
22
  "types": "dist/types/src/index.d.ts",
@@ -26,12 +28,12 @@
26
28
  "src"
27
29
  ],
28
30
  "dependencies": {
29
- "effect": "3.17.7",
30
- "@dxos/crypto": "0.8.4-main.e098934",
31
- "@dxos/invariant": "0.8.4-main.e098934",
32
- "@dxos/keys": "0.8.4-main.e098934",
33
- "@dxos/util": "0.8.4-main.e098934",
34
- "@dxos/protocols": "0.8.4-main.e098934"
31
+ "effect": "3.20.0",
32
+ "@dxos/crypto": "0.8.4-main.e250131250",
33
+ "@dxos/invariant": "0.8.4-main.e250131250",
34
+ "@dxos/protocols": "0.8.4-main.e250131250",
35
+ "@dxos/util": "0.8.4-main.e250131250",
36
+ "@dxos/keys": "0.8.4-main.e250131250"
35
37
  },
36
38
  "publishConfig": {
37
39
  "access": "public"
@@ -137,6 +137,10 @@ export const ObjectStructure = Object.freeze({
137
137
  return object.system?.target;
138
138
  },
139
139
 
140
+ getParent: (object: ObjectStructure): EncodedReference | undefined => {
141
+ return object.system?.parent;
142
+ },
143
+
140
144
  /**
141
145
  * @returns All references in the data section of the object.
142
146
  */
@@ -153,6 +157,10 @@ export const ObjectStructure = Object.freeze({
153
157
  return references;
154
158
  },
155
159
 
160
+ getTags: (object: ObjectStructure): string[] => {
161
+ return object.meta.tags ?? [];
162
+ },
163
+
156
164
  makeObject: ({
157
165
  type,
158
166
  data,
@@ -214,6 +222,14 @@ export type ObjectMeta = {
214
222
  * Foreign keys.
215
223
  */
216
224
  keys: ForeignKey[];
225
+
226
+ /**
227
+ * Tags.
228
+ * An array of DXNs of Tag objects within the space.
229
+ *
230
+ * NOTE: Optional for backwards compatibilty.
231
+ */
232
+ tags?: string[];
217
233
  };
218
234
 
219
235
  /**
@@ -236,13 +252,19 @@ export type ObjectSystem = {
236
252
  */
237
253
  deleted?: boolean;
238
254
 
255
+ /**
256
+ * Object parent.
257
+ * Objects with no parent are at the top level of the object hierarchy in the space.
258
+ */
259
+ parent?: EncodedReference;
260
+
239
261
  /**
240
262
  * Only for relations.
241
263
  */
242
264
  source?: EncodedReference;
243
265
 
244
266
  /**
245
- * Only for relations.w
267
+ * Only for relations.
246
268
  */
247
269
  target?: EncodedReference;
248
270
  };
@@ -0,0 +1,67 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { FeedProtocol } from '@dxos/protocols';
6
+
7
+ import type { ForeignKey } from './foreign-key';
8
+
9
+ /** Property name for meta when object is serialized to JSON. Matches @dxos/echo/internal ATTR_META. */
10
+ const ATTR_META = '@meta';
11
+
12
+ /**
13
+ * Codec for ECHO objects in feed block payload: JSON object ↔ UTF-8 bytes.
14
+ * Encodes with queue position stripped; decodes with optional position injection.
15
+ */
16
+ export class EchoFeedCodec {
17
+ static readonly #encoder = new TextEncoder();
18
+ static readonly #decoder = new TextDecoder();
19
+
20
+ /**
21
+ * Prepares a value for feed storage (strips queue position from metadata) and encodes to bytes.
22
+ */
23
+ static encode(value: Record<string, unknown>): Uint8Array {
24
+ const prepared = EchoFeedCodec.#stripQueuePosition(value);
25
+ return EchoFeedCodec.#encoder.encode(JSON.stringify(prepared));
26
+ }
27
+
28
+ /**
29
+ * Decodes feed block bytes to a JSON value.
30
+ * If position is provided, injects queue position into the decoded object's metadata.
31
+ */
32
+ static decode(data: Uint8Array, position?: number): Record<string, unknown> {
33
+ const decoded = JSON.parse(EchoFeedCodec.#decoder.decode(data));
34
+ if (position !== undefined && typeof decoded === 'object' && decoded !== null) {
35
+ EchoFeedCodec.#setQueuePosition(decoded, position);
36
+ }
37
+ return decoded;
38
+ }
39
+
40
+ static #stripQueuePosition(value: Record<string, unknown>): Record<string, unknown> {
41
+ if (typeof value !== 'object' || value === null) {
42
+ return value;
43
+ }
44
+ const obj = structuredClone(value);
45
+ const meta = obj[ATTR_META] as { keys?: ForeignKey[] } | undefined;
46
+ if (meta?.keys?.some((key: ForeignKey) => key.source === FeedProtocol.KEY_QUEUE_POSITION)) {
47
+ meta.keys = meta.keys.filter((key: ForeignKey) => key.source !== FeedProtocol.KEY_QUEUE_POSITION);
48
+ }
49
+ return obj;
50
+ }
51
+
52
+ static #setQueuePosition(obj: Record<string, any>, position: number): void {
53
+ obj[ATTR_META] ??= { keys: [] };
54
+ obj[ATTR_META]!.keys ??= [];
55
+ const keys = obj[ATTR_META]!.keys!;
56
+ for (let i = 0; i < keys.length; i++) {
57
+ if (keys[i].source === FeedProtocol.KEY_QUEUE_POSITION) {
58
+ keys.splice(i, 1);
59
+ i--;
60
+ }
61
+ }
62
+ keys.push({
63
+ source: FeedProtocol.KEY_QUEUE_POSITION,
64
+ id: position.toString(),
65
+ });
66
+ }
67
+ }
@@ -2,7 +2,8 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Schema, SchemaAST } from 'effect';
5
+ import * as Schema from 'effect/Schema';
6
+ import * as SchemaAST from 'effect/SchemaAST';
6
7
 
7
8
  const ForeignKey_ = Schema.Struct({
8
9
  /**
@@ -15,8 +16,8 @@ const ForeignKey_ = Schema.Struct({
15
16
  * Id within the foreign database.
16
17
  */
17
18
  // TODO(wittjosiah): This annotation is currently used to ensure id field shows up in forms.
18
- // TODO(dmaretskyi): `false` is not a valid value for the annotation.
19
- id: Schema.String.annotations({ [SchemaAST.IdentifierAnnotationId]: false }),
19
+ // TODO(dmaretskyi): `false` is not a valid value for the annotation. Use a different annotation.
20
+ id: Schema.String.annotations({ [SchemaAST.IdentifierAnnotationId]: 'false' }),
20
21
  });
21
22
 
22
23
  export type ForeignKey = Schema.Schema.Type<typeof ForeignKey_>;
package/src/index.ts CHANGED
@@ -2,10 +2,11 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ export type * from './collection-sync';
5
6
  export * from './document-structure';
7
+ export * from './echo-feed-codec';
8
+ export * from './foreign-key';
9
+ export * from './query';
6
10
  export * from './reference';
7
11
  export * from './space-doc-version';
8
- export type * from './collection-sync';
9
12
  export * from './space-id';
10
- export * from './foreign-key';
11
- export * from './query';
package/src/query/ast.ts CHANGED
@@ -2,14 +2,15 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Match, Schema } from 'effect';
5
+ import * as Match from 'effect/Match';
6
+ import * as Schema from 'effect/Schema';
6
7
 
7
8
  import { DXN, ObjectId } from '@dxos/keys';
8
9
 
9
10
  import { ForeignKey } from '../foreign-key';
10
11
 
11
12
  const TypenameSpecifier = Schema.Union(DXN.Schema, Schema.Null).annotations({
12
- description: 'DXN or null. Null means any type will match',
13
+ description: 'DXN or null; null matches any type',
13
14
  });
14
15
 
15
16
  // NOTE: This pattern with 3 definitions per schema is need to make the types opaque, and circular references in AST to not cause compiler errors.
@@ -46,6 +47,9 @@ const FilterObject_ = Schema.Struct({
46
47
  export interface FilterObject extends Schema.Schema.Type<typeof FilterObject_> {}
47
48
  export const FilterObject: Schema.Schema<FilterObject> = FilterObject_;
48
49
 
50
+ /**
51
+ * Compare.
52
+ */
49
53
  const FilterCompare_ = Schema.Struct({
50
54
  type: Schema.Literal('compare'),
51
55
  operator: Schema.Literal('eq', 'neq', 'gt', 'gte', 'lt', 'lte'),
@@ -54,6 +58,9 @@ const FilterCompare_ = Schema.Struct({
54
58
  export interface FilterCompare extends Schema.Schema.Type<typeof FilterCompare_> {}
55
59
  export const FilterCompare: Schema.Schema<FilterCompare> = FilterCompare_;
56
60
 
61
+ /**
62
+ * In.
63
+ */
57
64
  const FilterIn_ = Schema.Struct({
58
65
  type: Schema.Literal('in'),
59
66
  values: Schema.Array(Schema.Any),
@@ -61,65 +68,121 @@ const FilterIn_ = Schema.Struct({
61
68
  export interface FilterIn extends Schema.Schema.Type<typeof FilterIn_> {}
62
69
  export const FilterIn: Schema.Schema<FilterIn> = FilterIn_;
63
70
 
71
+ /**
72
+ * Contains.
73
+ */
64
74
  const FilterContains_ = Schema.Struct({
65
75
  type: Schema.Literal('contains'),
66
76
  value: Schema.Any,
67
77
  });
78
+
68
79
  export interface FilterContains extends Schema.Schema.Type<typeof FilterContains_> {}
80
+
69
81
  /**
70
82
  * Predicate for an array property to contain the provided value.
71
83
  * Nested objects are matched using strict structural matching.
72
84
  */
73
85
  export const FilterContains: Schema.Schema<FilterContains> = FilterContains_;
74
86
 
87
+ /**
88
+ * Filters objects that have certain tag.
89
+ */
90
+ const FilterTag_ = Schema.Struct({
91
+ type: Schema.Literal('tag'),
92
+ tag: Schema.String, // TODO(burdon): Make OR-collection?
93
+ });
94
+
95
+ export interface FilterTag extends Schema.Schema.Type<typeof FilterTag_> {}
96
+ export const FilterTag: Schema.Schema<FilterTag> = FilterTag_;
97
+
98
+ /**
99
+ * Range.
100
+ */
75
101
  const FilterRange_ = Schema.Struct({
76
102
  type: Schema.Literal('range'),
77
103
  from: Schema.Any,
78
104
  to: Schema.Any,
79
105
  });
106
+
80
107
  export interface FilterRange extends Schema.Schema.Type<typeof FilterRange_> {}
81
108
  export const FilterRange: Schema.Schema<FilterRange> = FilterRange_;
82
109
 
110
+ /**
111
+ * Filter by system timestamp (createdAt / updatedAt).
112
+ * Timestamps are unix milliseconds stored in the object meta index.
113
+ */
114
+ const FilterTimestamp_ = Schema.Struct({
115
+ type: Schema.Literal('timestamp'),
116
+ field: Schema.Literal('createdAt', 'updatedAt'),
117
+ operator: Schema.Literal('gt', 'gte', 'lt', 'lte'),
118
+ value: Schema.Number,
119
+ });
120
+
121
+ export interface FilterTimestamp extends Schema.Schema.Type<typeof FilterTimestamp_> {}
122
+ export const FilterTimestamp: Schema.Schema<FilterTimestamp> = FilterTimestamp_;
123
+
124
+ /**
125
+ * Text search.
126
+ */
83
127
  const FilterTextSearch_ = Schema.Struct({
84
128
  type: Schema.Literal('text-search'),
85
129
  text: Schema.String,
86
130
  searchKind: Schema.optional(Schema.Literal('full-text', 'vector')),
87
131
  });
132
+
88
133
  export interface FilterTextSearch extends Schema.Schema.Type<typeof FilterTextSearch_> {}
89
134
  export const FilterTextSearch: Schema.Schema<FilterTextSearch> = FilterTextSearch_;
90
135
 
136
+ /**
137
+ * Not.
138
+ */
91
139
  const FilterNot_ = Schema.Struct({
92
140
  type: Schema.Literal('not'),
93
141
  filter: Schema.suspend(() => Filter),
94
142
  });
143
+
95
144
  export interface FilterNot extends Schema.Schema.Type<typeof FilterNot_> {}
96
145
  export const FilterNot: Schema.Schema<FilterNot> = FilterNot_;
97
146
 
147
+ /**
148
+ * And.
149
+ */
98
150
  const FilterAnd_ = Schema.Struct({
99
151
  type: Schema.Literal('and'),
100
152
  filters: Schema.Array(Schema.suspend(() => Filter)),
101
153
  });
154
+
102
155
  export interface FilterAnd extends Schema.Schema.Type<typeof FilterAnd_> {}
103
156
  export const FilterAnd: Schema.Schema<FilterAnd> = FilterAnd_;
104
157
 
158
+ /**
159
+ * Or.
160
+ */
105
161
  const FilterOr_ = Schema.Struct({
106
162
  type: Schema.Literal('or'),
107
163
  filters: Schema.Array(Schema.suspend(() => Filter)),
108
164
  });
165
+
109
166
  export interface FilterOr extends Schema.Schema.Type<typeof FilterOr_> {}
110
167
  export const FilterOr: Schema.Schema<FilterOr> = FilterOr_;
111
168
 
169
+ /**
170
+ * Union of filters.
171
+ */
112
172
  export const Filter = Schema.Union(
113
173
  FilterObject,
114
- FilterTextSearch,
115
174
  FilterCompare,
116
175
  FilterIn,
117
176
  FilterContains,
177
+ FilterTag,
118
178
  FilterRange,
179
+ FilterTimestamp,
180
+ FilterTextSearch,
119
181
  FilterNot,
120
182
  FilterAnd,
121
183
  FilterOr,
122
- );
184
+ ).annotations({ identifier: 'org.dxos.schema.filter' });
185
+
123
186
  export type Filter = Schema.Schema.Type<typeof Filter>;
124
187
 
125
188
  /**
@@ -129,6 +192,7 @@ const QuerySelectClause_ = Schema.Struct({
129
192
  type: Schema.Literal('select'),
130
193
  filter: Schema.suspend(() => Filter),
131
194
  });
195
+
132
196
  export interface QuerySelectClause extends Schema.Schema.Type<typeof QuerySelectClause_> {}
133
197
  export const QuerySelectClause: Schema.Schema<QuerySelectClause> = QuerySelectClause_;
134
198
 
@@ -140,6 +204,7 @@ const QueryFilterClause_ = Schema.Struct({
140
204
  selection: Schema.suspend(() => Query),
141
205
  filter: Schema.suspend(() => Filter),
142
206
  });
207
+
143
208
  export interface QueryFilterClause extends Schema.Schema.Type<typeof QueryFilterClause_> {}
144
209
  export const QueryFilterClause: Schema.Schema<QueryFilterClause> = QueryFilterClause_;
145
210
 
@@ -151,6 +216,7 @@ const QueryReferenceTraversalClause_ = Schema.Struct({
151
216
  anchor: Schema.suspend(() => Query),
152
217
  property: Schema.String, // TODO(dmaretskyi): Change to EscapedPropPath.
153
218
  });
219
+
154
220
  export interface QueryReferenceTraversalClause extends Schema.Schema.Type<typeof QueryReferenceTraversalClause_> {}
155
221
  export const QueryReferenceTraversalClause: Schema.Schema<QueryReferenceTraversalClause> =
156
222
  QueryReferenceTraversalClause_;
@@ -161,9 +227,14 @@ export const QueryReferenceTraversalClause: Schema.Schema<QueryReferenceTraversa
161
227
  const QueryIncomingReferencesClause_ = Schema.Struct({
162
228
  type: Schema.Literal('incoming-references'),
163
229
  anchor: Schema.suspend(() => Query),
164
- property: Schema.String,
230
+ /**
231
+ * Property path where the reference is located.
232
+ * If null, matches references from any property.
233
+ */
234
+ property: Schema.NullOr(Schema.String),
165
235
  typename: TypenameSpecifier,
166
236
  });
237
+
167
238
  export interface QueryIncomingReferencesClause extends Schema.Schema.Type<typeof QueryIncomingReferencesClause_> {}
168
239
  export const QueryIncomingReferencesClause: Schema.Schema<QueryIncomingReferencesClause> =
169
240
  QueryIncomingReferencesClause_;
@@ -182,6 +253,7 @@ const QueryRelationClause_ = Schema.Struct({
182
253
  direction: Schema.Literal('outgoing', 'incoming', 'both'),
183
254
  filter: Schema.optional(Schema.suspend(() => Filter)),
184
255
  });
256
+
185
257
  export interface QueryRelationClause extends Schema.Schema.Type<typeof QueryRelationClause_> {}
186
258
  export const QueryRelationClause: Schema.Schema<QueryRelationClause> = QueryRelationClause_;
187
259
 
@@ -193,9 +265,27 @@ const QueryRelationTraversalClause_ = Schema.Struct({
193
265
  anchor: Schema.suspend(() => Query),
194
266
  direction: Schema.Literal('source', 'target', 'both'),
195
267
  });
268
+
196
269
  export interface QueryRelationTraversalClause extends Schema.Schema.Type<typeof QueryRelationTraversalClause_> {}
197
270
  export const QueryRelationTraversalClause: Schema.Schema<QueryRelationTraversalClause> = QueryRelationTraversalClause_;
198
271
 
272
+ /**
273
+ * Traverse parent-child hierarchy.
274
+ */
275
+ const QueryHierarchyTraversalClause_ = Schema.Struct({
276
+ type: Schema.Literal('hierarchy-traversal'),
277
+ anchor: Schema.suspend(() => Query),
278
+ /**
279
+ * to-parent: traverse from child to parent.
280
+ * to-children: traverse from parent to children.
281
+ */
282
+ direction: Schema.Literal('to-parent', 'to-children'),
283
+ });
284
+
285
+ export interface QueryHierarchyTraversalClause extends Schema.Schema.Type<typeof QueryHierarchyTraversalClause_> {}
286
+ export const QueryHierarchyTraversalClause: Schema.Schema<QueryHierarchyTraversalClause> =
287
+ QueryHierarchyTraversalClause_;
288
+
199
289
  /**
200
290
  * Union of multiple queries.
201
291
  */
@@ -203,6 +293,7 @@ const QueryUnionClause_ = Schema.Struct({
203
293
  type: Schema.Literal('union'),
204
294
  queries: Schema.Array(Schema.suspend(() => Query)),
205
295
  });
296
+
206
297
  export interface QueryUnionClause extends Schema.Schema.Type<typeof QueryUnionClause_> {}
207
298
  export const QueryUnionClause: Schema.Schema<QueryUnionClause> = QueryUnionClause_;
208
299
 
@@ -214,6 +305,7 @@ const QuerySetDifferenceClause_ = Schema.Struct({
214
305
  source: Schema.suspend(() => Query),
215
306
  exclude: Schema.suspend(() => Query),
216
307
  });
308
+
217
309
  export interface QuerySetDifferenceClause extends Schema.Schema.Type<typeof QuerySetDifferenceClause_> {}
218
310
  export const QuerySetDifferenceClause: Schema.Schema<QuerySetDifferenceClause> = QuerySetDifferenceClause_;
219
311
 
@@ -230,7 +322,14 @@ const Order_ = Schema.Union(
230
322
  property: Schema.String,
231
323
  direction: OrderDirection,
232
324
  }),
325
+ Schema.Struct({
326
+ // Order by relevance rank (for FTS/vector search results).
327
+ // Default direction is 'desc' (higher rank = better match first).
328
+ kind: Schema.Literal('rank'),
329
+ direction: OrderDirection,
330
+ }),
233
331
  );
332
+
234
333
  export type Order = Schema.Schema.Type<typeof Order_>;
235
334
  export const Order: Schema.Schema<Order> = Order_;
236
335
 
@@ -243,6 +342,7 @@ const QueryOrderClause_ = Schema.Struct({
243
342
  query: Schema.suspend(() => Query),
244
343
  order: Schema.Array(Order),
245
344
  });
345
+
246
346
  export interface QueryOrderClause extends Schema.Schema.Type<typeof QueryOrderClause_> {}
247
347
  export const QueryOrderClause: Schema.Schema<QueryOrderClause> = QueryOrderClause_;
248
348
 
@@ -254,9 +354,37 @@ const QueryOptionsClause_ = Schema.Struct({
254
354
  query: Schema.suspend(() => Query),
255
355
  options: Schema.suspend(() => QueryOptions),
256
356
  });
357
+
257
358
  export interface QueryOptionsClause extends Schema.Schema.Type<typeof QueryOptionsClause_> {}
258
359
  export const QueryOptionsClause: Schema.Schema<QueryOptionsClause> = QueryOptionsClause_;
259
360
 
361
+ /**
362
+ * Limit the number of results.
363
+ */
364
+ const QueryLimitClause_ = Schema.Struct({
365
+ type: Schema.Literal('limit'),
366
+ query: Schema.suspend(() => Query),
367
+ limit: Schema.Number,
368
+ });
369
+
370
+ export interface QueryLimitClause extends Schema.Schema.Type<typeof QueryLimitClause_> {}
371
+ export const QueryLimitClause: Schema.Schema<QueryLimitClause> = QueryLimitClause_;
372
+
373
+ export const QueryFromClause_ = Schema.Struct({
374
+ type: Schema.Literal('from'),
375
+ query: Schema.suspend(() => Query),
376
+ from: Schema.Union(
377
+ Schema.TaggedStruct('scope', {
378
+ scope: Schema.suspend(() => Scope),
379
+ }),
380
+ Schema.TaggedStruct('query', {
381
+ query: Schema.suspend(() => Query),
382
+ }),
383
+ ),
384
+ });
385
+ export interface QueryFromClause extends Schema.Schema.Type<typeof QueryFromClause_> {}
386
+ export const QueryFromClause: Schema.Schema<QueryFromClause> = QueryFromClause_;
387
+
260
388
  const Query_ = Schema.Union(
261
389
  QuerySelectClause,
262
390
  QueryFilterClause,
@@ -264,16 +392,31 @@ const Query_ = Schema.Union(
264
392
  QueryIncomingReferencesClause,
265
393
  QueryRelationClause,
266
394
  QueryRelationTraversalClause,
395
+ QueryHierarchyTraversalClause,
267
396
  QueryUnionClause,
268
397
  QuerySetDifferenceClause,
269
398
  QueryOrderClause,
270
399
  QueryOptionsClause,
271
- );
400
+ QueryLimitClause,
401
+ QueryFromClause,
402
+ ).annotations({ identifier: 'org.dxos.schema.query' });
272
403
 
273
404
  export type Query = Schema.Schema.Type<typeof Query_>;
274
405
  export const Query: Schema.Schema<Query> = Query_;
275
406
 
276
407
  export const QueryOptions = Schema.Struct({
408
+ /**
409
+ * Nested select statements will use this option to filter deleted objects.
410
+ */
411
+ deleted: Schema.optional(Schema.Literal('include', 'exclude', 'only')),
412
+ });
413
+
414
+ export interface QueryOptions extends Schema.Schema.Type<typeof QueryOptions> {}
415
+
416
+ /**
417
+ * Specifies the scope of the data to query from.
418
+ */
419
+ export const Scope = Schema.Struct({
277
420
  /**
278
421
  * The nested select statemets will select from the given spaces.
279
422
  *
@@ -281,19 +424,19 @@ export const QueryOptions = Schema.Struct({
281
424
  */
282
425
  spaceIds: Schema.optional(Schema.Array(Schema.String)),
283
426
 
427
+ /**
428
+ * If true, the nested select statements will select from all queues in the spaces specified by `spaceIds`.
429
+ */
430
+ allQueuesFromSpaces: Schema.optional(Schema.Boolean),
431
+
284
432
  /**
285
433
  * The nested select statemets will select from the given queues.
286
434
  *
287
435
  * NOTE: Spaces and queues are unioned together if both are specified.
288
436
  */
289
437
  queues: Schema.optional(Schema.Array(DXN.Schema)),
290
-
291
- /**
292
- * Nested select statements will use this option to filter deleted objects.
293
- */
294
- deleted: Schema.optional(Schema.Literal('include', 'exclude', 'only')),
295
438
  });
296
- export interface QueryOptions extends Schema.Schema.Type<typeof QueryOptions> {}
439
+ export interface Scope extends Schema.Schema.Type<typeof Scope> {}
297
440
 
298
441
  export const visit = (query: Query, visitor: (node: Query) => void) => {
299
442
  visitor(query);
@@ -305,17 +448,57 @@ export const visit = (query: Query, visitor: (node: Query) => void) => {
305
448
  Match.when({ type: 'relation' }, ({ anchor }) => visit(anchor, visitor)),
306
449
  Match.when({ type: 'options' }, ({ query }) => visit(query, visitor)),
307
450
  Match.when({ type: 'relation-traversal' }, ({ anchor }) => visit(anchor, visitor)),
451
+ Match.when({ type: 'hierarchy-traversal' }, ({ anchor }) => visit(anchor, visitor)),
308
452
  Match.when({ type: 'union' }, ({ queries }) => queries.forEach((q) => visit(q, visitor))),
309
453
  Match.when({ type: 'set-difference' }, ({ source, exclude }) => {
310
454
  visit(source, visitor);
311
455
  visit(exclude, visitor);
312
456
  }),
313
457
  Match.when({ type: 'order' }, ({ query }) => visit(query, visitor)),
458
+ Match.when({ type: 'limit' }, ({ query }) => visit(query, visitor)),
459
+ Match.when({ type: 'from' }, (node) => {
460
+ visit(node.query, visitor);
461
+ if (node.from._tag === 'query') {
462
+ visit(node.from.query, visitor);
463
+ }
464
+ }),
314
465
  Match.when({ type: 'select' }, () => {}),
315
466
  Match.exhaustive,
316
467
  );
317
468
  };
318
469
 
470
+ /**
471
+ * Recursively transforms a query tree bottom-up.
472
+ * The mapper receives each node with its children already transformed.
473
+ */
474
+ export const map = (query: Query, mapper: (node: Query) => Query): Query => {
475
+ const mapped: Query = Match.value(query).pipe(
476
+ Match.when({ type: 'filter' }, (node) => ({ ...node, selection: map(node.selection, mapper) })),
477
+ Match.when({ type: 'reference-traversal' }, (node) => ({ ...node, anchor: map(node.anchor, mapper) })),
478
+ Match.when({ type: 'incoming-references' }, (node) => ({ ...node, anchor: map(node.anchor, mapper) })),
479
+ Match.when({ type: 'relation' }, (node) => ({ ...node, anchor: map(node.anchor, mapper) })),
480
+ Match.when({ type: 'relation-traversal' }, (node) => ({ ...node, anchor: map(node.anchor, mapper) })),
481
+ Match.when({ type: 'hierarchy-traversal' }, (node) => ({ ...node, anchor: map(node.anchor, mapper) })),
482
+ Match.when({ type: 'options' }, (node) => ({ ...node, query: map(node.query, mapper) })),
483
+ Match.when({ type: 'order' }, (node) => ({ ...node, query: map(node.query, mapper) })),
484
+ Match.when({ type: 'limit' }, (node) => ({ ...node, query: map(node.query, mapper) })),
485
+ Match.when({ type: 'from' }, (node) => ({
486
+ ...node,
487
+ query: map(node.query, mapper),
488
+ ...(node.from._tag === 'query' ? { from: { _tag: 'query' as const, query: map(node.from.query, mapper) } } : {}),
489
+ })),
490
+ Match.when({ type: 'union' }, (node) => ({ ...node, queries: node.queries.map((q) => map(q, mapper)) })),
491
+ Match.when({ type: 'set-difference' }, (node) => ({
492
+ ...node,
493
+ source: map(node.source, mapper),
494
+ exclude: map(node.exclude, mapper),
495
+ })),
496
+ Match.when({ type: 'select' }, (node) => node),
497
+ Match.exhaustive,
498
+ );
499
+ return mapper(mapped);
500
+ };
501
+
319
502
  export const fold = <T>(query: Query, reducer: (node: Query) => T): T[] => {
320
503
  return Match.value(query).pipe(
321
504
  Match.withReturnType<T[]>(),
@@ -325,11 +508,20 @@ export const fold = <T>(query: Query, reducer: (node: Query) => T): T[] => {
325
508
  Match.when({ type: 'relation' }, ({ anchor }) => fold(anchor, reducer)),
326
509
  Match.when({ type: 'options' }, ({ query }) => fold(query, reducer)),
327
510
  Match.when({ type: 'relation-traversal' }, ({ anchor }) => fold(anchor, reducer)),
511
+ Match.when({ type: 'hierarchy-traversal' }, ({ anchor }) => fold(anchor, reducer)),
328
512
  Match.when({ type: 'union' }, ({ queries }) => queries.flatMap((q) => fold(q, reducer))),
329
513
  Match.when({ type: 'set-difference' }, ({ source, exclude }) =>
330
514
  fold(source, reducer).concat(fold(exclude, reducer)),
331
515
  ),
332
516
  Match.when({ type: 'order' }, ({ query }) => fold(query, reducer)),
517
+ Match.when({ type: 'limit' }, ({ query }) => fold(query, reducer)),
518
+ Match.when({ type: 'from' }, (node) => {
519
+ const results = fold(node.query, reducer);
520
+ if (node.from._tag === 'query') {
521
+ return results.concat(fold(node.from.query, reducer));
522
+ }
523
+ return results;
524
+ }),
333
525
  Match.when({ type: 'select' }, () => []),
334
526
  Match.exhaustive,
335
527
  );
package/src/reference.ts CHANGED
@@ -177,6 +177,9 @@ export const EncodedReference = Object.freeze({
177
177
  return DXN.parse(EncodedReference.getReferenceString(value));
178
178
  },
179
179
  fromDXN: (dxn: DXN): EncodedReference => {
180
- return encodeReference(Reference.fromDXN(dxn));
180
+ return { '/': dxn.toString() };
181
+ },
182
+ fromLegacyTypename: (typename: string): EncodedReference => {
183
+ return { '/': DXN.fromTypename(typename).toString() };
181
184
  },
182
185
  });