@dxos/echo 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.
Files changed (43) hide show
  1. package/README.md +1 -1
  2. package/dist/lib/browser/{chunk-UYPR62ZB.mjs → chunk-WG555MU5.mjs} +404 -174
  3. package/dist/lib/browser/chunk-WG555MU5.mjs.map +7 -0
  4. package/dist/lib/browser/index.mjs +9 -1
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +2 -2
  7. package/dist/lib/node-esm/{chunk-BYBICDIO.mjs → chunk-N55B5WWQ.mjs} +404 -174
  8. package/dist/lib/node-esm/chunk-N55B5WWQ.mjs.map +7 -0
  9. package/dist/lib/node-esm/index.mjs +9 -1
  10. package/dist/lib/node-esm/meta.json +1 -1
  11. package/dist/lib/node-esm/testing/index.mjs +2 -2
  12. package/dist/types/src/Obj.d.ts +104 -13
  13. package/dist/types/src/Obj.d.ts.map +1 -1
  14. package/dist/types/src/Ref.d.ts +2 -2
  15. package/dist/types/src/Relation.d.ts +10 -7
  16. package/dist/types/src/Relation.d.ts.map +1 -1
  17. package/dist/types/src/Type.d.ts +34 -33
  18. package/dist/types/src/Type.d.ts.map +1 -1
  19. package/dist/types/src/errors.d.ts +72 -0
  20. package/dist/types/src/errors.d.ts.map +1 -0
  21. package/dist/types/src/index.d.ts +3 -1
  22. package/dist/types/src/index.d.ts.map +1 -1
  23. package/dist/types/src/query/dsl.d.ts +27 -2
  24. package/dist/types/src/query/dsl.d.ts.map +1 -1
  25. package/dist/types/tsconfig.tsbuildinfo +1 -1
  26. package/package.json +20 -13
  27. package/src/Obj.ts +214 -21
  28. package/src/Relation.ts +22 -11
  29. package/src/Type.ts +66 -51
  30. package/src/errors.ts +18 -0
  31. package/src/index.ts +4 -1
  32. package/src/query/dsl.test.ts +40 -1
  33. package/src/query/dsl.ts +112 -6
  34. package/src/test/api.test.ts +9 -2
  35. package/dist/lib/browser/chunk-UYPR62ZB.mjs.map +0 -7
  36. package/dist/lib/node/chunk-4HQE2F3L.cjs +0 -644
  37. package/dist/lib/node/chunk-4HQE2F3L.cjs.map +0 -7
  38. package/dist/lib/node/index.cjs +0 -43
  39. package/dist/lib/node/index.cjs.map +0 -7
  40. package/dist/lib/node/meta.json +0 -1
  41. package/dist/lib/node/testing/index.cjs +0 -89
  42. package/dist/lib/node/testing/index.cjs.map +0 -7
  43. package/dist/lib/node-esm/chunk-BYBICDIO.mjs.map +0 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/echo",
3
- "version": "0.8.3",
3
+ "version": "0.8.4-main.1da679c",
4
4
  "description": "ECHO API",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -10,31 +10,37 @@
10
10
  "type": "module",
11
11
  "exports": {
12
12
  ".": {
13
+ "source": "./src/index.ts",
13
14
  "types": "./dist/types/src/index.d.ts",
14
15
  "browser": "./dist/lib/browser/index.mjs",
15
16
  "node": "./dist/lib/node-esm/index.mjs"
16
17
  },
17
18
  "./Type": {
19
+ "source": "./src/Type.ts",
18
20
  "types": "./dist/types/src/Type.d.ts",
19
21
  "browser": "./dist/lib/browser/Type.mjs",
20
22
  "node": "./dist/lib/node-esm/Type.mjs"
21
23
  },
22
24
  "./Obj": {
25
+ "source": "./src/Obj.ts",
23
26
  "types": "./dist/types/src/Obj.d.ts",
24
27
  "browser": "./dist/lib/browser/Obj.mjs",
25
28
  "node": "./dist/lib/node-esm/Obj.mjs"
26
29
  },
27
30
  "./Relation": {
31
+ "source": "./src/Relation.ts",
28
32
  "types": "./dist/types/src/Relation.d.ts",
29
33
  "browser": "./dist/lib/browser/Relation.mjs",
30
34
  "node": "./dist/lib/node-esm/Relation.mjs"
31
35
  },
32
36
  "./Ref": {
37
+ "source": "./src/Ref.ts",
33
38
  "types": "./dist/types/src/Ref.d.ts",
34
39
  "browser": "./dist/lib/browser/Ref.mjs",
35
40
  "node": "./dist/lib/node-esm/Ref.mjs"
36
41
  },
37
42
  "./testing": {
43
+ "source": "./src/testing/types.ts",
38
44
  "types": "./dist/types/src/testing/types.d.ts",
39
45
  "browser": "./dist/lib/browser/testing/index.mjs",
40
46
  "node": "./dist/lib/node-esm/testing/index.mjs"
@@ -66,18 +72,19 @@
66
72
  ],
67
73
  "dependencies": {
68
74
  "@preact/signals-core": "^1.9.0",
69
- "effect": "3.14.21",
70
- "@dxos/debug": "0.8.3",
71
- "@dxos/echo-schema": "0.8.3",
72
- "@dxos/echo-protocol": "0.8.3",
73
- "@dxos/echo-signals": "0.8.3",
74
- "@dxos/effect": "0.8.3",
75
- "@dxos/invariant": "0.8.3",
76
- "@dxos/live-object": "0.8.3",
77
- "@dxos/keys": "0.8.3",
78
- "@dxos/log": "0.8.3",
79
- "@dxos/node-std": "0.8.3",
80
- "@dxos/util": "0.8.3"
75
+ "effect": "3.17.7",
76
+ "@dxos/debug": "0.8.4-main.1da679c",
77
+ "@dxos/echo-protocol": "0.8.4-main.1da679c",
78
+ "@dxos/echo-schema": "0.8.4-main.1da679c",
79
+ "@dxos/echo-signals": "0.8.4-main.1da679c",
80
+ "@dxos/invariant": "0.8.4-main.1da679c",
81
+ "@dxos/errors": "0.8.4-main.1da679c",
82
+ "@dxos/effect": "0.8.4-main.1da679c",
83
+ "@dxos/keys": "0.8.4-main.1da679c",
84
+ "@dxos/live-object": "0.8.4-main.1da679c",
85
+ "@dxos/log": "0.8.4-main.1da679c",
86
+ "@dxos/util": "0.8.4-main.1da679c",
87
+ "@dxos/node-std": "0.8.4-main.1da679c"
81
88
  },
82
89
  "publishConfig": {
83
90
  "access": "public"
package/src/Obj.ts CHANGED
@@ -3,48 +3,72 @@
3
3
  //
4
4
 
5
5
  import { Schema } from 'effect';
6
+ import { dual } from 'effect/Function';
6
7
 
7
8
  import * as EchoSchema from '@dxos/echo-schema';
8
9
  import { assertArgument, invariant } from '@dxos/invariant';
9
10
  import { type DXN } from '@dxos/keys';
10
- import type * as LiveObject from '@dxos/live-object';
11
+ import * as LiveObject from '@dxos/live-object';
11
12
  import { live } from '@dxos/live-object';
12
- import { assumeType } from '@dxos/util';
13
+ import { assumeType, deepMapValues } from '@dxos/util';
13
14
 
14
15
  import type * as Ref from './Ref';
15
16
  import type * as Relation from './Relation';
16
- import type * as Type from './Type';
17
+ import * as Type from './Type';
17
18
 
18
- // NOTE: Don't export: Obj.Any and Obj.Obj form the public API.
19
- interface ObjBase extends Type.OfKind<EchoSchema.EntityKind.Object> {
20
- readonly id: EchoSchema.ObjectId;
21
- }
19
+ /**
20
+ * NOTE: Don't export: Obj.Any and Obj.Obj form the public API.
21
+ */
22
+ interface BaseObj extends EchoSchema.HasId, Type.OfKind<EchoSchema.EntityKind.Object> {}
22
23
 
23
24
  /**
24
25
  * Object type with specific properties.
25
26
  */
26
- export type Obj<Props> = ObjBase & Props;
27
+ export type Obj<Props> = BaseObj & Props;
27
28
 
28
29
  /**
29
30
  * Base type for all ECHO objects.
30
31
  */
31
- export interface Any extends ObjBase {}
32
+ export interface Any extends BaseObj {}
33
+
34
+ export const Any = Schema.Struct({}).pipe(
35
+ Type.Obj({
36
+ typename: 'dxos.org/types/Any',
37
+ version: '0.1.0',
38
+ }),
39
+ );
40
+
41
+ type Props<T = any> = { id?: EchoSchema.ObjectId } & Type.Properties<T>;
32
42
 
33
- type MakeProps<T> = {
34
- id?: EchoSchema.ObjectId;
35
- } & Type.Properties<T>;
43
+ export type MakeProps<T extends Type.Obj.Any> = NoInfer<Props<Schema.Schema.Type<T>>> & {
44
+ [Meta]?: Partial<EchoSchema.ObjectMeta>;
45
+ };
46
+
47
+ export const Meta: unique symbol = EchoSchema.MetaId as any;
48
+
49
+ // TODO(dmaretskyi): Expose Meta = EchoSchema.MetaId.
36
50
 
37
51
  /**
38
52
  * Creates new object.
53
+ * @param schema - Object schema.
54
+ * @param props - Object properties.
55
+ * @param meta - Object metadata (deprecated) -- pass with Obj.Meta.
56
+ *
57
+ * Meta can be passed as a symbol in `props`.
58
+ *
59
+ * Example:
60
+ * ```ts
61
+ * const obj = Obj.make(Person, { [Obj.Meta]: { keys: [...] }, name: 'John' });
62
+ * ```
39
63
  */
40
- // TODO(dmaretskyi): Move meta into props.
41
64
  export const make = <S extends Type.Obj.Any>(
42
65
  schema: S,
43
- props: NoInfer<MakeProps<Schema.Schema.Type<S>>>,
66
+ props: MakeProps<S>,
44
67
  meta?: EchoSchema.ObjectMeta,
45
68
  ): LiveObject.Live<Schema.Schema.Type<S>> => {
46
69
  assertArgument(
47
70
  EchoSchema.getTypeAnnotation(schema)?.kind === EchoSchema.EntityKind.Object,
71
+ 'schema',
48
72
  'Expected an object schema',
49
73
  );
50
74
 
@@ -90,8 +114,8 @@ export const instanceOf: {
90
114
  export const getSchema = EchoSchema.getSchema;
91
115
 
92
116
  // TODO(dmaretskyi): Allow returning undefined.
93
- export const getDXN = (obj: Any): DXN => {
94
- assertArgument(!Schema.isSchema(obj), 'Object should not be a schema.');
117
+ export const getDXN = (obj: Any | Relation.Any): DXN => {
118
+ assertArgument(!Schema.isSchema(obj), 'obj', 'Object should not be a schema.');
95
119
  const dxn = EchoSchema.getObjectDXN(obj);
96
120
  invariant(dxn != null, 'Invalid object.');
97
121
  return dxn;
@@ -108,7 +132,7 @@ export const getTypeDXN = EchoSchema.getType;
108
132
  * @returns The typename of the object's type.
109
133
  * @example `example.com/type/Contact`
110
134
  */
111
- export const getTypename = (obj: Any): string | undefined => {
135
+ export const getTypename = (obj: Any | Relation.Any): string | undefined => {
112
136
  const schema = getSchema(obj);
113
137
  if (schema == null) {
114
138
  // Try to extract typename from DXN.
@@ -119,26 +143,61 @@ export const getTypename = (obj: Any): string | undefined => {
119
143
  };
120
144
 
121
145
  // TODO(dmaretskyi): Allow returning undefined.
122
- export const getMeta = (obj: Any): EchoSchema.ObjectMeta => {
146
+ export const getMeta = (obj: Any | Relation.Any): EchoSchema.ObjectMeta => {
123
147
  const meta = EchoSchema.getMeta(obj);
124
148
  invariant(meta != null, 'Invalid object.');
125
149
  return meta;
126
150
  };
127
151
 
152
+ /**
153
+ * @returns Foreign keys for the object from the specified source.
154
+ */
155
+ export const getKeys: {
156
+ (obj: Any | Relation.Any, source: string): EchoSchema.ForeignKey[];
157
+ (source: string): (obj: Any | Relation.Any) => EchoSchema.ForeignKey[];
158
+ } = dual(2, (obj: Any | Relation.Any, source?: string): EchoSchema.ForeignKey[] => {
159
+ const meta = EchoSchema.getMeta(obj);
160
+ invariant(meta != null, 'Invalid object.');
161
+ return meta.keys.filter((key) => key.source === source);
162
+ });
163
+
164
+ /**
165
+ * Delete all keys from the object for the specified source.
166
+ * @param obj
167
+ * @param source
168
+ */
169
+ export const deleteKeys = (obj: Any | Relation.Any, source: string) => {
170
+ const meta = EchoSchema.getMeta(obj);
171
+ for (let i = 0; i < meta.keys.length; i++) {
172
+ if (meta.keys[i].source === source) {
173
+ meta.keys.splice(i, 1);
174
+ i--;
175
+ }
176
+ }
177
+ };
178
+
128
179
  // TODO(dmaretskyi): Default to `false`.
129
- export const isDeleted = (obj: Any): boolean => {
180
+ export const isDeleted = (obj: Any | Relation.Any): boolean => {
130
181
  const deleted = EchoSchema.isDeleted(obj);
131
182
  invariant(typeof deleted === 'boolean', 'Invalid object.');
132
183
  return deleted;
133
184
  };
134
185
 
135
- export const getLabel = (obj: Any): string | undefined => {
186
+ // TODO(burdon): Rename "label"
187
+ export const getLabel = (obj: Any | Relation.Any): string | undefined => {
136
188
  const schema = getSchema(obj);
137
189
  if (schema != null) {
138
190
  return EchoSchema.getLabel(schema, obj);
139
191
  }
140
192
  };
141
193
 
194
+ export const setLabel = (obj: Any | Relation.Any, label: string) => {
195
+ const schema = getSchema(obj);
196
+ if (schema != null) {
197
+ EchoSchema.setLabel(schema, obj, label);
198
+ }
199
+ };
200
+
142
201
  /**
143
202
  * JSON representation of an object.
144
203
  */
@@ -149,6 +208,7 @@ export type JSON = EchoSchema.ObjectJSON;
149
208
  *
150
209
  * The same algorithm is used when calling the standard `JSON.stringify(obj)` function.
151
210
  */
211
+ // TODO(burdon): Base util type for Obj/Relation?
152
212
  export const toJSON = (obj: Any | Relation.Any): JSON => EchoSchema.objectToJSON(obj);
153
213
 
154
214
  /**
@@ -156,6 +216,139 @@ export const toJSON = (obj: Any | Relation.Any): JSON => EchoSchema.objectToJSON
156
216
  * References and schemas will be resolvable if the `refResolver` is provided.
157
217
  *
158
218
  * The function need to be async to support resolving the schema as well as the relation endpoints.
219
+ *
220
+ * @param options.refResolver - Resolver for references. Produces hydrated references that can be resolved.
221
+ * @param options.dxn - Override object DXN. Changes the result of `Obj.getDXN`.
159
222
  */
160
- export const fromJSON: (json: unknown, options?: { refResolver?: Ref.Resolver }) => Promise<Any> =
223
+ export const fromJSON: (json: unknown, options?: { refResolver?: Ref.Resolver; dxn?: DXN }) => Promise<Any> =
161
224
  EchoSchema.objectFromJSON as any;
225
+
226
+ /**
227
+ * Returns an immutable snapshot of an object.
228
+ */
229
+ export const getSnapshot: <T extends Any>(obj: Obj<T>) => T = LiveObject.getSnapshot;
230
+
231
+ export type CloneOptions = {
232
+ /**
233
+ * Retain the original object's ID.
234
+ * @default false
235
+ */
236
+ retainId?: boolean;
237
+ };
238
+
239
+ /**
240
+ * Clones an object or relation.
241
+ * This does not clone referenced objects, only the properties in the object.
242
+ * @returns A new object with the same schema and properties.
243
+ */
244
+ export const clone = <T extends Any | Relation.Any>(obj: T, opts?: CloneOptions): T => {
245
+ const { id, ...data } = obj;
246
+ const schema = getSchema(obj);
247
+ invariant(schema != null, 'Object should have a schema');
248
+ const props: any = deepMapValues(data, (value, recurse) => {
249
+ if (EchoSchema.Ref.isRef(value)) {
250
+ return value;
251
+ }
252
+ return recurse(value);
253
+ });
254
+ if (opts?.retainId) {
255
+ props.id = id;
256
+ }
257
+ const meta = getMeta(obj);
258
+ props[EchoSchema.MetaId] = deepMapValues(meta, (value, recurse) => {
259
+ if (EchoSchema.Ref.isRef(value)) {
260
+ return value;
261
+ }
262
+ return recurse(value);
263
+ });
264
+ return make(schema, props);
265
+ };
266
+
267
+ export const VersionTypeId = EchoSchema.VersionTypeId;
268
+ export type VersionType = typeof VersionTypeId;
269
+
270
+ /**
271
+ * Represent object version.
272
+ * May be backed by Automerge.
273
+ * Objects with no history are not versioned.
274
+ */
275
+ export interface Version {
276
+ [VersionTypeId]: {};
277
+
278
+ /**
279
+ * Whether the object is versioned.
280
+ */
281
+ versioned: boolean;
282
+
283
+ /**
284
+ * Automerge heads.
285
+ */
286
+ automergeHeads?: string[];
287
+ }
288
+
289
+ const unversioned: Version = {
290
+ [VersionTypeId]: {},
291
+ versioned: false,
292
+ };
293
+
294
+ /**
295
+ * Checks that `obj` is a version object.
296
+ */
297
+ export const isVersion = (obj: unknown): obj is Version => {
298
+ return obj != null && typeof obj === 'object' && VersionTypeId in obj;
299
+ };
300
+
301
+ /**
302
+ * Returns the version of the object.
303
+ */
304
+ export const version = (obj: Any | Relation.Any): Version => {
305
+ const version = (obj as any)[EchoSchema.ObjectVersionId];
306
+ if (version === undefined) {
307
+ return unversioned;
308
+ }
309
+ return version;
310
+ };
311
+
312
+ /**
313
+ * Checks that `version` is a valid version object.
314
+ */
315
+ export const versionValid = (version: Version): boolean => {
316
+ assertArgument(isVersion(version), 'version', 'Invalid version object');
317
+ return !!version.versioned;
318
+ };
319
+
320
+ export type VersionCompareResult = 'unversioned' | 'equal' | 'different';
321
+
322
+ /**
323
+ * Compares two versions.
324
+ * @param version1
325
+ * @param version2
326
+ * @returns 'unversioned' if either object is unversioned, 'equal' if the versions are equal, 'different' if the versions are different.
327
+ */
328
+ export const compareVersions = (version1: Version, version2: Version): VersionCompareResult => {
329
+ assertArgument(isVersion(version1), 'version1', 'Invalid version object');
330
+ assertArgument(isVersion(version2), 'version2', 'Invalid version object');
331
+
332
+ if (!versionValid(version1) || !versionValid(version2)) {
333
+ return 'unversioned';
334
+ }
335
+
336
+ if (version1.automergeHeads?.length !== version2.automergeHeads?.length) {
337
+ return 'different';
338
+ }
339
+ if (version1.automergeHeads?.some((head) => !version2.automergeHeads?.includes(head))) {
340
+ return 'different';
341
+ }
342
+
343
+ return 'equal';
344
+ };
345
+
346
+ export const encodeVersion = (version: Version): string => {
347
+ return JSON.stringify(version);
348
+ };
349
+
350
+ export const decodeVersion = (version: string): Version => {
351
+ const parsed = JSON.parse(version);
352
+ parsed[VersionTypeId] = {};
353
+ return parsed;
354
+ };
package/src/Relation.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { type Schema } from 'effect';
5
+ import { Schema } from 'effect';
6
6
 
7
7
  import { raise } from '@dxos/debug';
8
8
  import * as EchoSchema from '@dxos/echo-schema';
@@ -11,25 +11,35 @@ import { DXN } from '@dxos/keys';
11
11
  import { type Live, live } from '@dxos/live-object';
12
12
  import { assumeType } from '@dxos/util';
13
13
 
14
- import type * as Obj from './Obj';
15
- import type * as Type from './Type';
14
+ import * as Obj from './Obj';
15
+ import * as Type from './Type';
16
16
 
17
- // NOTE: Don't export: Relation.Relation and Relation.Any form the public API.
18
- interface RelationBase<Source, Target>
19
- extends Type.Relation.Endpoints<Source, Target>,
20
- Type.OfKind<EchoSchema.EntityKind.Relation> {
21
- readonly id: EchoSchema.ObjectId;
22
- }
17
+ /**
18
+ * NOTE: Don't export: Relation.Relation and Relation.Any form the public API.
19
+ */
20
+ interface BaseRelation<Source, Target>
21
+ extends EchoSchema.HasId,
22
+ Type.Relation.Endpoints<Source, Target>,
23
+ Type.OfKind<EchoSchema.EntityKind.Relation> {}
23
24
 
24
25
  /**
25
26
  * Relation type with specific properties.
26
27
  */
27
- export type Relation<Source extends Obj.Any, Target extends Obj.Any, Props> = RelationBase<Source, Target> & Props;
28
+ export type Relation<Source extends Obj.Any, Target extends Obj.Any, Props> = BaseRelation<Source, Target> & Props;
28
29
 
29
30
  /**
30
31
  * Base type for all ECHO relations.
31
32
  */
32
- export interface Any extends RelationBase<Obj.Any, Obj.Any> {}
33
+ export interface Any extends BaseRelation<Obj.Any, Obj.Any> {}
34
+
35
+ export const Any = Schema.Struct({}).pipe(
36
+ Type.Relation({
37
+ typename: 'dxos.org/types/Any',
38
+ version: '0.1.0',
39
+ source: Obj.Any,
40
+ target: Obj.Any,
41
+ }),
42
+ );
33
43
 
34
44
  // TODO(dmaretskyi): Has to be `unique symbol`.
35
45
  export const Source: unique symbol = EchoSchema.RelationSourceId as any;
@@ -59,6 +69,7 @@ export const make = <S extends Type.Relation.Any>(
59
69
  ): Live<Schema.Schema.Type<S> & Type.OfKind<EchoSchema.EntityKind.Relation>> => {
60
70
  assertArgument(
61
71
  EchoSchema.getTypeAnnotation(schema)?.kind === EchoSchema.EntityKind.Relation,
72
+ 'schema',
62
73
  'Expected a relation schema',
63
74
  );
64
75
 
package/src/Type.ts CHANGED
@@ -3,20 +3,24 @@
3
3
  //
4
4
 
5
5
  import { type Schema } from 'effect';
6
- import type { Simplify } from 'effect/Schema';
6
+ import { type Simplify } from 'effect/Schema';
7
7
 
8
- import type { EncodedReference } from '@dxos/echo-protocol';
8
+ import { type EncodedReference } from '@dxos/echo-protocol';
9
9
  import * as EchoSchema from '@dxos/echo-schema';
10
- import type { ToMutable } from '@dxos/echo-schema';
10
+ import { type ToMutable } from '@dxos/echo-schema';
11
11
  import { invariant } from '@dxos/invariant';
12
12
  import type * as Keys from '@dxos/keys';
13
13
 
14
14
  import type * as RelationModule from './Relation';
15
15
 
16
+ //
17
+ // Kind
18
+ //
19
+
16
20
  export const KindId: unique symbol = EchoSchema.EntityKindId as any;
17
21
  export type KindId = typeof KindId;
18
22
 
19
- export { EntityKind as Kind } from '@dxos/echo-schema';
23
+ export const Kind = EchoSchema.EntityKind;
20
24
 
21
25
  /**
22
26
  * Assigns a kind to an Object or Relation instance.
@@ -27,25 +31,23 @@ export interface OfKind<Kind extends EchoSchema.EntityKind> {
27
31
  readonly [KindId]: Kind;
28
32
  }
29
33
 
30
- interface ObjJsonProps {
31
- id: string;
32
- }
33
-
34
- interface RelationJsonProps {
35
- id: string;
36
- [EchoSchema.ATTR_RELATION_SOURCE]: string;
37
- [EchoSchema.ATTR_RELATION_TARGET]: string;
38
- }
39
-
40
34
  /**
41
- * Returns all properties of an object or relation except for the id and kind.
35
+ * Base ECHO schema type.
42
36
  */
43
- export type Properties<T> = Omit<T, 'id' | KindId | RelationModule.Source | RelationModule.Target>;
37
+ export type Schema = EchoSchema.EchoSchema;
44
38
 
45
39
  /**
46
- * Base ECHO schema type.
40
+ * Returns all properties of an object or relation except for the id and kind.
47
41
  */
48
- export type Schema = EchoSchema.EchoSchema;
42
+ export type Properties<T = any> = Omit<T, 'id' | KindId | RelationModule.Source | RelationModule.Target>;
43
+
44
+ //
45
+ // Obj
46
+ //
47
+
48
+ interface ObjJsonProps {
49
+ id: string;
50
+ }
49
51
 
50
52
  /**
51
53
  * Return type of the `Obj` schema constructor.
@@ -54,13 +56,13 @@ export type Schema = EchoSchema.EchoSchema;
54
56
  * See Effect's note on interface types.
55
57
  */
56
58
  export interface obj<Self extends Schema.Schema.Any>
57
- extends Schema.AnnotableClass<
59
+ extends EchoSchema.TypeMeta,
60
+ Schema.AnnotableClass<
58
61
  obj<Self>,
59
62
  OfKind<EchoSchema.EntityKind.Object> & ToMutable<Schema.Schema.Type<Self>>,
60
63
  Simplify<ObjJsonProps & ToMutable<Schema.Schema.Encoded<Self>>>,
61
64
  Schema.Schema.Context<Self>
62
- >,
63
- EchoSchema.TypeMeta {}
65
+ > {}
64
66
 
65
67
  /**
66
68
  * Object schema.
@@ -72,15 +74,39 @@ export const Obj: {
72
74
  /**
73
75
  * Object schema type definitions.
74
76
  */
75
- export namespace Obj {
77
+ export declare namespace Obj {
76
78
  /**
77
79
  * Type that represents an arbitrary schema type of an object.
78
80
  * NOTE: This is not an instance type.
79
81
  */
80
82
  // TODO(dmaretskyi): If schema was covariant, we could specify props in here, like `id: ObjectId`.
83
+ // TODO(burdon): This erases the ECHO type info (e.g., id, typename).
81
84
  export type Any = Schema.Schema.AnyNoContext;
82
85
  }
83
86
 
87
+ //
88
+ // Expando
89
+ //
90
+
91
+ // TODO(burdon): We're using Expando in many places as a base type.
92
+ export interface Expando extends OfKind<EchoSchema.EntityKind.Object> {
93
+ [key: string]: any;
94
+ }
95
+
96
+ type ExpandoEncoded = Simplify<ObjJsonProps & { [key: string]: any }>;
97
+
98
+ export const Expando: Schema.Schema<Expando, ExpandoEncoded, never> = EchoSchema.Expando as any;
99
+
100
+ //
101
+ // Relation
102
+ //
103
+
104
+ interface RelationJsonProps {
105
+ id: string;
106
+ [EchoSchema.ATTR_RELATION_SOURCE]: string;
107
+ [EchoSchema.ATTR_RELATION_TARGET]: string;
108
+ }
109
+
84
110
  /**
85
111
  * Return type of the `Relation` schema constructor.
86
112
  *
@@ -91,15 +117,15 @@ export interface relation<
91
117
  Self extends Schema.Schema.Any,
92
118
  SourceSchema extends Schema.Schema.Any,
93
119
  TargetSchema extends Schema.Schema.Any,
94
- > extends Schema.AnnotableClass<
120
+ > extends EchoSchema.TypeMeta,
121
+ Schema.AnnotableClass<
95
122
  relation<Self, SourceSchema, TargetSchema>,
96
123
  OfKind<EchoSchema.EntityKind.Relation> &
97
124
  Relation.Endpoints<Schema.Schema.Type<SourceSchema>, Schema.Schema.Type<TargetSchema>> &
98
125
  ToMutable<Schema.Schema.Type<Self>>,
99
126
  Simplify<RelationJsonProps & ToMutable<Schema.Schema.Encoded<Self>>>,
100
127
  Schema.Schema.Context<Self>
101
- >,
102
- EchoSchema.TypeMeta {}
128
+ > {}
103
129
 
104
130
  /**
105
131
  * Relation schema.
@@ -123,14 +149,14 @@ export namespace Relation {
123
149
  export type Any = Schema.Schema.AnyNoContext;
124
150
 
125
151
  /**
126
- * Get relation target type.
152
+ * Get relation source type.
127
153
  */
128
- export type Target<A> = A extends Relation.Endpoints<infer _S, infer T> ? T : never;
154
+ export type Source<A> = A extends Relation.Endpoints<infer S, infer _T> ? S : never;
129
155
 
130
156
  /**
131
- * Get relation source type.
157
+ * Get relation target type.
132
158
  */
133
- export type Source<A> = A extends Relation.Endpoints<infer S, infer _T> ? S : never;
159
+ export type Target<A> = A extends Relation.Endpoints<infer _S, infer T> ? T : never;
134
160
 
135
161
  export type Endpoints<Source, Target> = {
136
162
  [RelationModule.Source]: Source;
@@ -138,6 +164,10 @@ export namespace Relation {
138
164
  };
139
165
  }
140
166
 
167
+ //
168
+ // Ref
169
+ //
170
+
141
171
  /**
142
172
  * Return type of the `Ref` schema constructor.
143
173
  *
@@ -193,6 +223,11 @@ export const getVersion = (schema: Obj.Any | Relation.Any): string => {
193
223
  return version;
194
224
  };
195
225
 
226
+ /**
227
+ * @returns True if the schema is mutable.
228
+ */
229
+ export const isMutable = EchoSchema.isMutable;
230
+
196
231
  /**
197
232
  * ECHO type metadata.
198
233
  */
@@ -205,27 +240,7 @@ export const getMeta = (schema: Obj.Any | Relation.Any): Meta | undefined => {
205
240
  return EchoSchema.getTypeAnnotation(schema);
206
241
  };
207
242
 
208
- /**
209
- * @returns True if the schema is mutable.
210
- */
211
- export const isMutable = EchoSchema.isMutable;
212
-
243
+ // TODO(dmaretskyi): Remove re-exports.
213
244
  export { SpaceId, ObjectId, DXN } from '@dxos/keys';
214
245
 
215
- export interface Expando extends OfKind<EchoSchema.EntityKind.Object> {
216
- [key: string]: any;
217
- }
218
-
219
- export const Expando: Schema.Schema<
220
- Expando,
221
- Simplify<ObjJsonProps & { [key: string]: any }>,
222
- never
223
- > = EchoSchema.Expando as any;
224
-
225
- export {
226
- // TODO(burdon): Standardize.
227
- Format,
228
- JsonSchemaType as JsonSchema,
229
- toEffectSchema,
230
- toJsonSchema,
231
- } from '@dxos/echo-schema';
246
+ export { Format, JsonSchemaType as JsonSchema, toEffectSchema, toJsonSchema } from '@dxos/echo-schema';
package/src/errors.ts ADDED
@@ -0,0 +1,18 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { BaseError, type BaseErrorOptions } from '@dxos/errors';
6
+ import { type DXN } from '@dxos/keys';
7
+
8
+ export class SchemaNotFoundError extends BaseError.extend('SCHEMA_NOT_FOUND', 'Schema not found') {
9
+ constructor(schema: string, options?: BaseErrorOptions) {
10
+ super({ context: { schema }, ...options });
11
+ }
12
+ }
13
+
14
+ export class ObjectNotFoundError extends BaseError.extend('OBJECT_NOT_FOUND', 'Object not found') {
15
+ constructor(dxn: DXN, options?: BaseErrorOptions) {
16
+ super({ context: { dxn }, ...options });
17
+ }
18
+ }