@dxos/echo-protocol 0.8.3 → 0.8.4-main.1da679c
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/index.mjs +140 -52
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +140 -52
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/document-structure.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/query/ast.d.ts +81 -18
- package/dist/types/src/query/ast.d.ts.map +1 -1
- package/dist/types/src/reference.d.ts +16 -0
- package/dist/types/src/reference.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -7
- package/src/document-structure.ts +1 -1
- package/src/index.ts +1 -1
- package/src/query/ast.ts +96 -28
- package/src/reference.ts +26 -0
- package/src/space-id.ts +1 -1
- package/dist/lib/node/index.cjs +0 -527
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
package/src/query/ast.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Schema } from 'effect';
|
|
5
|
+
import { Match, Schema } from 'effect';
|
|
6
6
|
|
|
7
7
|
import { DXN, ObjectId } from '@dxos/keys';
|
|
8
8
|
|
|
@@ -61,6 +61,17 @@ const FilterIn_ = Schema.Struct({
|
|
|
61
61
|
export interface FilterIn extends Schema.Schema.Type<typeof FilterIn_> {}
|
|
62
62
|
export const FilterIn: Schema.Schema<FilterIn> = FilterIn_;
|
|
63
63
|
|
|
64
|
+
const FilterContains_ = Schema.Struct({
|
|
65
|
+
type: Schema.Literal('contains'),
|
|
66
|
+
value: Schema.Any,
|
|
67
|
+
});
|
|
68
|
+
export interface FilterContains extends Schema.Schema.Type<typeof FilterContains_> {}
|
|
69
|
+
/**
|
|
70
|
+
* Predicate for an array property to contain the provided value.
|
|
71
|
+
* Nested objects are matched using strict structural matching.
|
|
72
|
+
*/
|
|
73
|
+
export const FilterContains: Schema.Schema<FilterContains> = FilterContains_;
|
|
74
|
+
|
|
64
75
|
const FilterRange_ = Schema.Struct({
|
|
65
76
|
type: Schema.Literal('range'),
|
|
66
77
|
from: Schema.Any,
|
|
@@ -103,6 +114,7 @@ export const Filter = Schema.Union(
|
|
|
103
114
|
FilterTextSearch,
|
|
104
115
|
FilterCompare,
|
|
105
116
|
FilterIn,
|
|
117
|
+
FilterContains,
|
|
106
118
|
FilterRange,
|
|
107
119
|
FilterNot,
|
|
108
120
|
FilterAnd,
|
|
@@ -205,6 +217,35 @@ const QuerySetDifferenceClause_ = Schema.Struct({
|
|
|
205
217
|
export interface QuerySetDifferenceClause extends Schema.Schema.Type<typeof QuerySetDifferenceClause_> {}
|
|
206
218
|
export const QuerySetDifferenceClause: Schema.Schema<QuerySetDifferenceClause> = QuerySetDifferenceClause_;
|
|
207
219
|
|
|
220
|
+
export const OrderDirection = Schema.Literal('asc', 'desc');
|
|
221
|
+
export type OrderDirection = Schema.Schema.Type<typeof OrderDirection>;
|
|
222
|
+
|
|
223
|
+
const Order_ = Schema.Union(
|
|
224
|
+
Schema.Struct({
|
|
225
|
+
// How database wants to order them (in practice - by id).
|
|
226
|
+
kind: Schema.Literal('natural'),
|
|
227
|
+
}),
|
|
228
|
+
Schema.Struct({
|
|
229
|
+
kind: Schema.Literal('property'),
|
|
230
|
+
property: Schema.String,
|
|
231
|
+
direction: OrderDirection,
|
|
232
|
+
}),
|
|
233
|
+
);
|
|
234
|
+
export type Order = Schema.Schema.Type<typeof Order_>;
|
|
235
|
+
export const Order: Schema.Schema<Order> = Order_;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Order the query results.
|
|
239
|
+
* Left-to-right the orders dominate.
|
|
240
|
+
*/
|
|
241
|
+
const QueryOrderClause_ = Schema.Struct({
|
|
242
|
+
type: Schema.Literal('order'),
|
|
243
|
+
query: Schema.suspend(() => Query),
|
|
244
|
+
order: Schema.Array(Order),
|
|
245
|
+
});
|
|
246
|
+
export interface QueryOrderClause extends Schema.Schema.Type<typeof QueryOrderClause_> {}
|
|
247
|
+
export const QueryOrderClause: Schema.Schema<QueryOrderClause> = QueryOrderClause_;
|
|
248
|
+
|
|
208
249
|
/**
|
|
209
250
|
* Add options to a query.
|
|
210
251
|
*/
|
|
@@ -225,6 +266,7 @@ const Query_ = Schema.Union(
|
|
|
225
266
|
QueryRelationTraversalClause,
|
|
226
267
|
QueryUnionClause,
|
|
227
268
|
QuerySetDifferenceClause,
|
|
269
|
+
QueryOrderClause,
|
|
228
270
|
QueryOptionsClause,
|
|
229
271
|
);
|
|
230
272
|
|
|
@@ -232,37 +274,63 @@ export type Query = Schema.Schema.Type<typeof Query_>;
|
|
|
232
274
|
export const Query: Schema.Schema<Query> = Query_;
|
|
233
275
|
|
|
234
276
|
export const QueryOptions = Schema.Struct({
|
|
277
|
+
/**
|
|
278
|
+
* The nested select statemets will select from the given spaces.
|
|
279
|
+
*
|
|
280
|
+
* NOTE: Spaces and queues are unioned together if both are specified.
|
|
281
|
+
*/
|
|
235
282
|
spaceIds: Schema.optional(Schema.Array(Schema.String)),
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* The nested select statemets will select from the given queues.
|
|
286
|
+
*
|
|
287
|
+
* NOTE: Spaces and queues are unioned together if both are specified.
|
|
288
|
+
*/
|
|
289
|
+
queues: Schema.optional(Schema.Array(DXN.Schema)),
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Nested select statements will use this option to filter deleted objects.
|
|
293
|
+
*/
|
|
236
294
|
deleted: Schema.optional(Schema.Literal('include', 'exclude', 'only')),
|
|
237
295
|
});
|
|
238
296
|
export interface QueryOptions extends Schema.Schema.Type<typeof QueryOptions> {}
|
|
239
297
|
|
|
240
298
|
export const visit = (query: Query, visitor: (node: Query) => void) => {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
visit(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
299
|
+
visitor(query);
|
|
300
|
+
|
|
301
|
+
Match.value(query).pipe(
|
|
302
|
+
Match.when({ type: 'filter' }, ({ selection }) => visit(selection, visitor)),
|
|
303
|
+
Match.when({ type: 'reference-traversal' }, ({ anchor }) => visit(anchor, visitor)),
|
|
304
|
+
Match.when({ type: 'incoming-references' }, ({ anchor }) => visit(anchor, visitor)),
|
|
305
|
+
Match.when({ type: 'relation' }, ({ anchor }) => visit(anchor, visitor)),
|
|
306
|
+
Match.when({ type: 'options' }, ({ query }) => visit(query, visitor)),
|
|
307
|
+
Match.when({ type: 'relation-traversal' }, ({ anchor }) => visit(anchor, visitor)),
|
|
308
|
+
Match.when({ type: 'union' }, ({ queries }) => queries.forEach((q) => visit(q, visitor))),
|
|
309
|
+
Match.when({ type: 'set-difference' }, ({ source, exclude }) => {
|
|
310
|
+
visit(source, visitor);
|
|
311
|
+
visit(exclude, visitor);
|
|
312
|
+
}),
|
|
313
|
+
Match.when({ type: 'order' }, ({ query }) => visit(query, visitor)),
|
|
314
|
+
Match.when({ type: 'select' }, () => {}),
|
|
315
|
+
Match.exhaustive,
|
|
316
|
+
);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
export const fold = <T>(query: Query, reducer: (node: Query) => T): T[] => {
|
|
320
|
+
return Match.value(query).pipe(
|
|
321
|
+
Match.withReturnType<T[]>(),
|
|
322
|
+
Match.when({ type: 'filter' }, ({ selection }) => fold(selection, reducer)),
|
|
323
|
+
Match.when({ type: 'reference-traversal' }, ({ anchor }) => fold(anchor, reducer)),
|
|
324
|
+
Match.when({ type: 'incoming-references' }, ({ anchor }) => fold(anchor, reducer)),
|
|
325
|
+
Match.when({ type: 'relation' }, ({ anchor }) => fold(anchor, reducer)),
|
|
326
|
+
Match.when({ type: 'options' }, ({ query }) => fold(query, reducer)),
|
|
327
|
+
Match.when({ type: 'relation-traversal' }, ({ anchor }) => fold(anchor, reducer)),
|
|
328
|
+
Match.when({ type: 'union' }, ({ queries }) => queries.flatMap((q) => fold(q, reducer))),
|
|
329
|
+
Match.when({ type: 'set-difference' }, ({ source, exclude }) =>
|
|
330
|
+
fold(source, reducer).concat(fold(exclude, reducer)),
|
|
331
|
+
),
|
|
332
|
+
Match.when({ type: 'order' }, ({ query }) => fold(query, reducer)),
|
|
333
|
+
Match.when({ type: 'select' }, () => []),
|
|
334
|
+
Match.exhaustive,
|
|
335
|
+
);
|
|
268
336
|
};
|
package/src/reference.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { assertArgument } from '@dxos/invariant';
|
|
5
6
|
import { DXN, LOCAL_SPACE_TAG, type PublicKey } from '@dxos/keys';
|
|
6
7
|
import { type ObjectId } from '@dxos/protocols';
|
|
7
8
|
import { type Reference as ReferenceProto } from '@dxos/protocols/proto/dxos/echo/model/document';
|
|
@@ -9,6 +10,7 @@ import { type Reference as ReferenceProto } from '@dxos/protocols/proto/dxos/ech
|
|
|
9
10
|
/**
|
|
10
11
|
* Runtime representation of an reference in ECHO.
|
|
11
12
|
* Implemented as a DXN, but we might extend it to other URIs in the future.
|
|
13
|
+
* @deprecated Use `EncodedReference` instead.
|
|
12
14
|
*/
|
|
13
15
|
export class Reference {
|
|
14
16
|
/**
|
|
@@ -122,6 +124,7 @@ export class Reference {
|
|
|
122
124
|
}
|
|
123
125
|
}
|
|
124
126
|
|
|
127
|
+
// TODO(dmaretskyi): Is this used anywhere?
|
|
125
128
|
export const REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';
|
|
126
129
|
|
|
127
130
|
/**
|
|
@@ -131,10 +134,16 @@ export type EncodedReference = {
|
|
|
131
134
|
'/': string;
|
|
132
135
|
};
|
|
133
136
|
|
|
137
|
+
/**
|
|
138
|
+
* @deprecated Use `EncodedReference.fromDXN` instead.
|
|
139
|
+
*/
|
|
134
140
|
export const encodeReference = (reference: Reference): EncodedReference => ({
|
|
135
141
|
'/': reference.toDXN().toString(),
|
|
136
142
|
});
|
|
137
143
|
|
|
144
|
+
/**
|
|
145
|
+
* @deprecated Use `EncodedReference.toDXN` instead.
|
|
146
|
+
*/
|
|
138
147
|
export const decodeReference = (value: any) => {
|
|
139
148
|
if (typeof value !== 'object' || value === null || typeof value['/'] !== 'string') {
|
|
140
149
|
throw new Error('Invalid reference');
|
|
@@ -152,5 +161,22 @@ export const decodeReference = (value: any) => {
|
|
|
152
161
|
return Reference.fromDXN(DXN.parse(dxnString));
|
|
153
162
|
};
|
|
154
163
|
|
|
164
|
+
/**
|
|
165
|
+
* @deprecated Use `EncodedReference.isEncodedReference` instead.
|
|
166
|
+
*/
|
|
155
167
|
export const isEncodedReference = (value: any): value is EncodedReference =>
|
|
156
168
|
typeof value === 'object' && value !== null && Object.keys(value).length === 1 && typeof value['/'] === 'string';
|
|
169
|
+
|
|
170
|
+
export const EncodedReference = Object.freeze({
|
|
171
|
+
isEncodedReference,
|
|
172
|
+
getReferenceString: (value: EncodedReference): string => {
|
|
173
|
+
assertArgument(isEncodedReference(value), 'value', 'invalid reference');
|
|
174
|
+
return value['/'];
|
|
175
|
+
},
|
|
176
|
+
toDXN: (value: EncodedReference): DXN => {
|
|
177
|
+
return DXN.parse(EncodedReference.getReferenceString(value));
|
|
178
|
+
},
|
|
179
|
+
fromDXN: (dxn: DXN): EncodedReference => {
|
|
180
|
+
return encodeReference(Reference.fromDXN(dxn));
|
|
181
|
+
},
|
|
182
|
+
});
|
package/src/space-id.ts
CHANGED
|
@@ -18,7 +18,7 @@ export const createIdFromSpaceKey = async (spaceKey: PublicKey): Promise<SpaceId
|
|
|
18
18
|
return cachedValue;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
const digest = await subtleCrypto.digest('SHA-256', spaceKey.asUint8Array());
|
|
21
|
+
const digest = await subtleCrypto.digest('SHA-256', spaceKey.asUint8Array() as Uint8Array<ArrayBuffer>);
|
|
22
22
|
|
|
23
23
|
const bytes = new Uint8Array(digest).slice(0, SpaceId.byteLength);
|
|
24
24
|
const spaceId = SpaceId.encode(bytes);
|