@dxos/echo 0.8.2 → 0.8.3-main.7f5a14c
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 +41 -35
- package/dist/lib/browser/index.mjs +179 -143
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +170 -135
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +179 -143
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/Key.d.ts +2 -0
- package/dist/types/src/Key.d.ts.map +1 -0
- package/dist/types/src/Obj.d.ts +33 -7
- package/dist/types/src/Obj.d.ts.map +1 -1
- package/dist/types/src/Ref.d.ts +13 -3
- package/dist/types/src/Ref.d.ts.map +1 -1
- package/dist/types/src/Relation.d.ts +13 -0
- package/dist/types/src/Relation.d.ts.map +1 -1
- package/dist/types/src/Type.d.ts +15 -13
- package/dist/types/src/Type.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +5 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/package.json +12 -12
- package/src/Key.ts +5 -0
- package/src/Obj.ts +54 -17
- package/src/Ref.ts +14 -6
- package/src/Relation.ts +33 -4
- package/src/Type.ts +19 -14
- package/src/index.ts +5 -3
- package/src/test/api.test.ts +17 -2
package/dist/types/src/Ref.d.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import * as EchoSchema from '@dxos/echo-schema';
|
|
2
2
|
import type * as Obj from './Obj';
|
|
3
|
+
export type Ref<T extends Obj.Any> = EchoSchema.Ref<T>;
|
|
3
4
|
export type Any = EchoSchema.Ref<Obj.Any>;
|
|
4
|
-
export declare const
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
export declare const Array: Readonly<{
|
|
6
|
+
targets: <T extends EchoSchema.AnyEchoObject>(refs: EchoSchema.Ref<T>[]) => T[];
|
|
7
|
+
loadAll: <T extends EchoSchema.AnyEchoObject>(refs: EchoSchema.Ref<T>[]) => Promise<T[]>;
|
|
8
|
+
removeById: (refs: EchoSchema.Ref<EchoSchema.AnyEchoObject>[], id: EchoSchema.ObjectId) => void;
|
|
9
|
+
}>;
|
|
7
10
|
/**
|
|
8
11
|
* Extract reference target.
|
|
9
12
|
*/
|
|
10
13
|
export type Target<R extends Any> = R extends EchoSchema.Ref<infer T> ? T : never;
|
|
14
|
+
/**
|
|
15
|
+
* Reference resolver.
|
|
16
|
+
*/
|
|
17
|
+
export type Resolver = EchoSchema.RefResolver;
|
|
18
|
+
export declare const isRef: (value: unknown) => value is Any;
|
|
19
|
+
export declare const make: <T extends EchoSchema.WithId>(object: T) => EchoSchema.Ref<T>;
|
|
20
|
+
export declare const fromDXN: (dxn: import("@dxos/keys").DXN) => EchoSchema.Ref<any>;
|
|
11
21
|
//# sourceMappingURL=Ref.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Ref.d.ts","sourceRoot":"","sources":["../../../src/Ref.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;AAEhD,OAAO,KAAK,KAAK,GAAG,MAAM,OAAO,CAAC;AAElC,MAAM,MAAM,GAAG,
|
|
1
|
+
{"version":3,"file":"Ref.d.ts","sourceRoot":"","sources":["../../../src/Ref.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;AAEhD,OAAO,KAAK,KAAK,GAAG,MAAM,OAAO,CAAC;AAElC,MAAM,MAAM,GAAG,CAAC,CAAC,SAAS,GAAG,CAAC,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACvD,MAAM,MAAM,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAE1C,eAAO,MAAM,KAAK;;;;EAAsB,CAAC;AAEzC;;GAEG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAElF;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,UAAU,CAAC,WAAW,CAAC;AAE9C,eAAO,MAAM,KAAK,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,GAA0B,CAAC;AAE5E,eAAO,MAAM,IAAI,+DAAsB,CAAC;AAGxC,eAAO,MAAM,OAAO,wDAAyB,CAAC"}
|
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
import * as EchoSchema from '@dxos/echo-schema';
|
|
2
|
+
import { DXN } from '@dxos/keys';
|
|
2
3
|
import * as LiveObject from '@dxos/live-object';
|
|
3
4
|
export type Any = EchoSchema.AnyEchoObject & EchoSchema.RelationSourceTargetRefs;
|
|
5
|
+
export declare const Source: symbol;
|
|
6
|
+
export declare const Target: symbol;
|
|
4
7
|
export declare const make: {
|
|
5
8
|
<T extends EchoSchema.BaseObject>(obj: T): LiveObject.Live<T>;
|
|
6
9
|
<T extends EchoSchema.BaseObject>(schema: import("effect/Schema").Schema<T, any, never>, obj: NoInfer<EchoSchema.ExcludeId<T>>, meta?: EchoSchema.ObjectMeta): LiveObject.Live<T>;
|
|
7
10
|
};
|
|
8
11
|
export declare const isRelation: (value: unknown) => value is Any;
|
|
12
|
+
/**
|
|
13
|
+
* @returns Relation source DXN.
|
|
14
|
+
* @throws If the object is not a relation.
|
|
15
|
+
*/
|
|
16
|
+
export declare const getSourceDXN: (value: Any) => DXN;
|
|
17
|
+
/**
|
|
18
|
+
* @returns Relation target DXN.
|
|
19
|
+
* @throws If the object is not a relation.
|
|
20
|
+
*/
|
|
21
|
+
export declare const getTargetDXN: (value: Any) => DXN;
|
|
9
22
|
/**
|
|
10
23
|
* @returns Relation source.
|
|
11
24
|
* @throws If the object is not a relation.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Relation.d.ts","sourceRoot":"","sources":["../../../src/Relation.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;AAEhD,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"Relation.d.ts","sourceRoot":"","sources":["../../../src/Relation.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;AAGhD,MAAM,MAAM,GAAG,GAAG,UAAU,CAAC,aAAa,GAAG,UAAU,CAAC,wBAAwB,CAAC;AAEjF,eAAO,MAAM,MAAM,QAA8B,CAAC;AAClD,eAAO,MAAM,MAAM,QAA8B,CAAC;AAElD,eAAO,MAAM,IAAI;;;CAAkB,CAAC;AAEpC,eAAO,MAAM,UAAU,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,GAUpD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,GAAG,KAAG,GAMzC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,GAAG,KAAG,GAMzC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,GAAG,EAAE,UAAU,CAAC,KAAG,UAAU,CAAC,cAAc,CAAC,CAAC,CAKjF,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,GAAG,EAAE,UAAU,CAAC,KAAG,UAAU,CAAC,cAAc,CAAC,CAAC,CAKjF,CAAC"}
|
package/dist/types/src/Type.d.ts
CHANGED
|
@@ -10,6 +10,13 @@ export type Schema = EchoSchema.EchoSchema;
|
|
|
10
10
|
* EchoObject schema.
|
|
11
11
|
*/
|
|
12
12
|
export declare const Obj: (meta: EchoSchema.TypeMeta) => <Self extends Schema.Schema.Any>(self: Self) => EchoSchema.EchoTypeSchema<Self>;
|
|
13
|
+
export declare namespace Obj {
|
|
14
|
+
/**
|
|
15
|
+
* Type that represents an arbitrary schema type of an object.
|
|
16
|
+
* NOTE: This is not an instance type.
|
|
17
|
+
*/
|
|
18
|
+
type Any = Schema.Schema.AnyNoContext;
|
|
19
|
+
}
|
|
13
20
|
/**
|
|
14
21
|
* EchoRelation schema.
|
|
15
22
|
*/
|
|
@@ -19,17 +26,6 @@ export declare const Relation: <Source extends Schema.Schema.AnyNoContext, Targe
|
|
|
19
26
|
source: Source;
|
|
20
27
|
target: Target;
|
|
21
28
|
}) => <Self extends Schema.Schema.Any>(self: Self) => EchoSchema.EchoTypeSchema<Self, EchoSchema.RelationSourceTargetRefs<Schema.Schema.Type<Source>, Schema.Schema.Type<Target>>>;
|
|
22
|
-
/**
|
|
23
|
-
* Ref schema.
|
|
24
|
-
*/
|
|
25
|
-
export declare const Ref: <S extends Obj.Any>(schema: S) => EchoSchema.Ref$<Schema.Schema.Type<S>>;
|
|
26
|
-
export declare namespace Obj {
|
|
27
|
-
/**
|
|
28
|
-
* Type that represents an arbitrary schema type of an object.
|
|
29
|
-
* NOTE: This is not an instance type.
|
|
30
|
-
*/
|
|
31
|
-
type Any = Schema.Schema.AnyNoContext;
|
|
32
|
-
}
|
|
33
29
|
export declare namespace Relation {
|
|
34
30
|
/**
|
|
35
31
|
* Type that represents an arbitrary schema type of a relation.
|
|
@@ -45,6 +41,12 @@ export declare namespace Relation {
|
|
|
45
41
|
*/
|
|
46
42
|
type Source<A> = A extends EchoSchema.RelationSourceTargetRefs<infer _T, infer S> ? S : never;
|
|
47
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Ref schema.
|
|
46
|
+
*/
|
|
47
|
+
export declare const Ref: <S extends Obj.Any>(schema: S) => EchoSchema.Ref$<Schema.Schema.Type<S>>;
|
|
48
|
+
export interface Ref<T> extends Schema.SchemaClass<EchoSchema.Ref<T>, EncodedReference> {
|
|
49
|
+
}
|
|
48
50
|
export declare namespace Ref {
|
|
49
51
|
/**
|
|
50
52
|
* Type that represents an arbitrary schema type of a reference.
|
|
@@ -81,7 +83,7 @@ export { EntityKind as Kind } from '@dxos/echo-schema';
|
|
|
81
83
|
/**
|
|
82
84
|
* @returns True if the schema is mutable.
|
|
83
85
|
*/
|
|
84
|
-
export declare const isMutable: (schema:
|
|
86
|
+
export declare const isMutable: (schema: Schema.Schema.AnyNoContext) => schema is EchoSchema.EchoSchema;
|
|
85
87
|
export { SpaceId, ObjectId, DXN } from '@dxos/keys';
|
|
86
|
-
export { Expando, JsonSchemaType as JsonSchema,
|
|
88
|
+
export { Expando, Format, JsonSchemaType as JsonSchema, toEffectSchema, toJsonSchema, } from '@dxos/echo-schema';
|
|
87
89
|
//# sourceMappingURL=Type.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Type.d.ts","sourceRoot":"","sources":["../../../src/Type.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;AAEhD,OAAO,KAAK,KAAK,IAAI,MAAM,YAAY,CAAC;AAExC;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG,UAAU,CAAC,UAAU,CAAC;AAE3C;;GAEG;AACH,eAAO,MAAM,GAAG,gHAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"Type.d.ts","sourceRoot":"","sources":["../../../src/Type.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;AAEhD,OAAO,KAAK,KAAK,IAAI,MAAM,YAAY,CAAC;AAExC;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG,UAAU,CAAC,UAAU,CAAC;AAE3C;;GAEG;AACH,eAAO,MAAM,GAAG,gHAAwB,CAAC;AAGzC,yBAAiB,GAAG,CAAC;IACnB;;;OAGG;IAEH,KAAY,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC;CAC9C;AAED;;GAEG;AACH,eAAO,MAAM,QAAQ;;;;;kLAA0B,CAAC;AAGhD,yBAAiB,QAAQ,CAAC;IACxB;;;OAGG;IAEH,KAAY,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC;IAE7C;;OAEG;IACH,KAAY,MAAM,CAAC,CAAC,IAAI,CAAC,SAAS,UAAU,CAAC,wBAAwB,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IAErG;;OAEG;IACH,KAAY,MAAM,CAAC,CAAC,IAAI,CAAC,SAAS,UAAU,CAAC,wBAAwB,CAAC,MAAM,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;CACtG;AAED;;GAEG;AACH,eAAO,MAAM,GAAG,EAAE,CAAC,CAAC,SAAS,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,KAAK,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAkB,CAAC;AAE5G,MAAM,WAAW,GAAG,CAAC,CAAC,CAAE,SAAQ,MAAM,CAAC,WAAW,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC;CAAG;AAG1F,yBAAiB,GAAG,CAAC;IACnB;;;OAGG;IACH,KAAY,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,gBAAgB,CAAC,CAAC;CACxE;AAED;;;;;GAKG;AACH,eAAO,MAAM,MAAM,GAAI,QAAQ,GAAG,CAAC,GAAG,GAAG,QAAQ,CAAC,GAAG,KAAG,IAAI,CAAC,GAAG,GAAG,SAElE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,WAAW,GAAI,QAAQ,GAAG,CAAC,GAAG,GAAG,QAAQ,CAAC,GAAG,KAAG,MAI5D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,UAAU,GAAI,QAAQ,GAAG,CAAC,GAAG,GAAG,QAAQ,CAAC,GAAG,KAAG,MAI3D,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,IAAI,GAAG,UAAU,CAAC,cAAc,CAAC;AAE7C;;GAEG;AACH,eAAO,MAAM,OAAO,GAAI,QAAQ,GAAG,CAAC,GAAG,GAAG,QAAQ,CAAC,GAAG,KAAG,IAAI,GAAG,SAE/D,CAAC;AAEF,OAAO,EAAE,UAAU,IAAI,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAEvD;;GAEG;AACH,eAAO,MAAM,SAAS,yEAAuB,CAAC;AAE9C,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AAEpD,OAAO,EAEL,OAAO,EAEP,MAAM,EACN,cAAc,IAAI,UAAU,EAC5B,cAAc,EACd,YAAY,GACb,MAAM,mBAAmB,CAAC"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
export * as
|
|
1
|
+
export * as Key from './Key';
|
|
2
2
|
export * as Obj from './Obj';
|
|
3
|
-
export * as Relation from './Relation';
|
|
4
3
|
export * as Ref from './Ref';
|
|
5
|
-
export
|
|
4
|
+
export * as Relation from './Relation';
|
|
5
|
+
export * as Type from './Type';
|
|
6
|
+
export { DXN } from '@dxos/keys';
|
|
6
7
|
export { Filter, Query } from '@dxos/echo-schema';
|
|
8
|
+
export { type Live } from '@dxos/live-object';
|
|
7
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAC7B,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAC7B,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAC7B,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAC;AACvC,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAE/B,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,mBAAmB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/echo",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.3-main.7f5a14c",
|
|
4
4
|
"description": "ECHO API",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -23,17 +23,17 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@preact/signals-core": "^1.9.0",
|
|
25
25
|
"effect": "3.14.21",
|
|
26
|
-
"@dxos/
|
|
27
|
-
"@dxos/
|
|
28
|
-
"@dxos/echo-
|
|
29
|
-
"@dxos/echo-
|
|
30
|
-
"@dxos/
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/log": "0.8.
|
|
35
|
-
"@dxos/node-std": "0.8.
|
|
36
|
-
"@dxos/util": "0.8.
|
|
26
|
+
"@dxos/debug": "0.8.3-main.7f5a14c",
|
|
27
|
+
"@dxos/echo-protocol": "0.8.3-main.7f5a14c",
|
|
28
|
+
"@dxos/echo-schema": "0.8.3-main.7f5a14c",
|
|
29
|
+
"@dxos/echo-signals": "0.8.3-main.7f5a14c",
|
|
30
|
+
"@dxos/effect": "0.8.3-main.7f5a14c",
|
|
31
|
+
"@dxos/keys": "0.8.3-main.7f5a14c",
|
|
32
|
+
"@dxos/invariant": "0.8.3-main.7f5a14c",
|
|
33
|
+
"@dxos/live-object": "0.8.3-main.7f5a14c",
|
|
34
|
+
"@dxos/log": "0.8.3-main.7f5a14c",
|
|
35
|
+
"@dxos/node-std": "0.8.3-main.7f5a14c",
|
|
36
|
+
"@dxos/util": "0.8.3-main.7f5a14c"
|
|
37
37
|
},
|
|
38
38
|
"publishConfig": {
|
|
39
39
|
"access": "public"
|
package/src/Key.ts
ADDED
package/src/Obj.ts
CHANGED
|
@@ -6,33 +6,42 @@ import { Schema } from 'effect';
|
|
|
6
6
|
|
|
7
7
|
import * as EchoSchema from '@dxos/echo-schema';
|
|
8
8
|
import { assertArgument, invariant } from '@dxos/invariant';
|
|
9
|
-
import type
|
|
9
|
+
import { type DXN } from '@dxos/keys';
|
|
10
10
|
import * as LiveObject from '@dxos/live-object';
|
|
11
|
+
import { assumeType } from '@dxos/util';
|
|
11
12
|
|
|
13
|
+
import type * as Ref from './Ref';
|
|
12
14
|
import type * as Type from './Type';
|
|
13
15
|
|
|
16
|
+
export type Obj<T = any> = EchoSchema.AnyEchoObject & T;
|
|
14
17
|
export type Any = EchoSchema.AnyEchoObject;
|
|
15
18
|
|
|
16
19
|
export const make = LiveObject.live;
|
|
17
20
|
|
|
18
|
-
// TODO(dmaretskyi): Currently broken
|
|
19
21
|
export const isObject = (obj: unknown): obj is Any => {
|
|
20
|
-
|
|
22
|
+
assumeType<EchoSchema.InternalObjectProps>(obj);
|
|
23
|
+
return typeof obj === 'object' && obj !== null && obj[EchoSchema.EntityKindId] === EchoSchema.EntityKind.Object;
|
|
21
24
|
};
|
|
22
25
|
|
|
23
26
|
/**
|
|
24
|
-
*
|
|
27
|
+
* Test if object or relation is an instance of a schema.
|
|
25
28
|
* @example
|
|
26
29
|
* ```ts
|
|
27
|
-
* const
|
|
30
|
+
* const john = Obj.make(Person, { name: 'John' });
|
|
31
|
+
* const johnIsPerson = Obj.instanceOf(Person)(john);
|
|
32
|
+
*
|
|
28
33
|
* const isPerson = Obj.instanceOf(Person);
|
|
29
|
-
* isPerson(
|
|
34
|
+
* if(isPerson(john)) {
|
|
35
|
+
* // john is Person
|
|
36
|
+
* }
|
|
30
37
|
* ```
|
|
31
38
|
*/
|
|
32
39
|
export const instanceOf: {
|
|
33
|
-
<S extends Type.Relation.Any | Type.Obj.Any>(schema: S): (value: unknown) => value is S
|
|
34
|
-
<S extends Type.Relation.Any | Type.Obj.Any>(schema: S, value: unknown): value is S
|
|
35
|
-
} = ((
|
|
40
|
+
<S extends Type.Relation.Any | Type.Obj.Any>(schema: S): (value: unknown) => value is Schema.Schema.Type<S>;
|
|
41
|
+
<S extends Type.Relation.Any | Type.Obj.Any>(schema: S, value: unknown): value is Schema.Schema.Type<S>;
|
|
42
|
+
} = ((
|
|
43
|
+
...args: [schema: Type.Relation.Any | Type.Obj.Any, value: unknown] | [schema: Type.Relation.Any | Type.Obj.Any]
|
|
44
|
+
) => {
|
|
36
45
|
if (args.length === 1) {
|
|
37
46
|
return (obj: unknown) => EchoSchema.isInstanceOf(args[0], obj);
|
|
38
47
|
}
|
|
@@ -42,9 +51,10 @@ export const instanceOf: {
|
|
|
42
51
|
|
|
43
52
|
export const getSchema = EchoSchema.getSchema;
|
|
44
53
|
|
|
54
|
+
// TODO(dmaretskyi): Allow returning undefined.
|
|
45
55
|
export const getDXN = (obj: Any): DXN => {
|
|
46
56
|
assertArgument(!Schema.isSchema(obj), 'Object should not be a schema.');
|
|
47
|
-
const dxn = EchoSchema.
|
|
57
|
+
const dxn = EchoSchema.getObjectDXN(obj);
|
|
48
58
|
invariant(dxn != null, 'Invalid object.');
|
|
49
59
|
return dxn;
|
|
50
60
|
};
|
|
@@ -53,11 +63,8 @@ export const getDXN = (obj: Any): DXN => {
|
|
|
53
63
|
* @returns The DXN of the object's type.
|
|
54
64
|
* @example dxn:example.com/type/Contact:1.0.0
|
|
55
65
|
*/
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
invariant(type != null, 'Invalid object.');
|
|
59
|
-
return type;
|
|
60
|
-
};
|
|
66
|
+
// TODO(burdon): Expando does not have a type.
|
|
67
|
+
export const getTypeDXN = EchoSchema.getType;
|
|
61
68
|
|
|
62
69
|
/**
|
|
63
70
|
* @returns The typename of the object's type.
|
|
@@ -67,20 +74,50 @@ export const getTypename = (obj: Any): string | undefined => {
|
|
|
67
74
|
const schema = getSchema(obj);
|
|
68
75
|
if (schema == null) {
|
|
69
76
|
// Try to extract typename from DXN.
|
|
70
|
-
return
|
|
77
|
+
return EchoSchema.getType(obj)?.asTypeDXN()?.type;
|
|
71
78
|
}
|
|
72
79
|
|
|
73
|
-
return EchoSchema.
|
|
80
|
+
return EchoSchema.getSchemaTypename(schema);
|
|
74
81
|
};
|
|
75
82
|
|
|
83
|
+
// TODO(dmaretskyi): Allow returning undefined.
|
|
76
84
|
export const getMeta = (obj: Any): EchoSchema.ObjectMeta => {
|
|
77
85
|
const meta = EchoSchema.getMeta(obj);
|
|
78
86
|
invariant(meta != null, 'Invalid object.');
|
|
79
87
|
return meta;
|
|
80
88
|
};
|
|
81
89
|
|
|
90
|
+
// TODO(dmaretskyi): Default to `false`.
|
|
82
91
|
export const isDeleted = (obj: Any): boolean => {
|
|
83
92
|
const deleted = EchoSchema.isDeleted(obj);
|
|
84
93
|
invariant(typeof deleted === 'boolean', 'Invalid object.');
|
|
85
94
|
return deleted;
|
|
86
95
|
};
|
|
96
|
+
|
|
97
|
+
export const getLabel = (obj: Any): string | undefined => {
|
|
98
|
+
const schema = getSchema(obj);
|
|
99
|
+
if (schema != null) {
|
|
100
|
+
return EchoSchema.getLabel(schema, obj);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* JSON representation of an object.
|
|
106
|
+
*/
|
|
107
|
+
export type JSON = EchoSchema.ObjectJSON;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Converts object to its JSON representation.
|
|
111
|
+
*
|
|
112
|
+
* The same algorithm is used when calling the standard `JSON.stringify(obj)` function.
|
|
113
|
+
*/
|
|
114
|
+
export const toJSON = (obj: Any): JSON => EchoSchema.objectToJSON(obj);
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Creates an object from its json representation, performing schema validation.
|
|
118
|
+
* References and schemas will be resolvable if the `refResolver` is provided.
|
|
119
|
+
*
|
|
120
|
+
* The function need to be async to support resolving the schema as well as the relation endpoints.
|
|
121
|
+
*/
|
|
122
|
+
export const fromJSON: (json: unknown, options?: { refResolver?: Ref.Resolver }) => Promise<Any> =
|
|
123
|
+
EchoSchema.objectFromJSON;
|
package/src/Ref.ts
CHANGED
|
@@ -6,16 +6,24 @@ import * as EchoSchema from '@dxos/echo-schema';
|
|
|
6
6
|
|
|
7
7
|
import type * as Obj from './Obj';
|
|
8
8
|
|
|
9
|
+
export type Ref<T extends Obj.Any> = EchoSchema.Ref<T>;
|
|
9
10
|
export type Any = EchoSchema.Ref<Obj.Any>;
|
|
10
11
|
|
|
11
|
-
export const
|
|
12
|
-
|
|
13
|
-
export const isRef: (value: unknown) => value is Any = EchoSchema.Ref.isRef;
|
|
14
|
-
|
|
15
|
-
// TODO(dmaretskyi): Consider just allowing `make` to accept DXN.
|
|
16
|
-
export const fromDXN = EchoSchema.Ref.fromDXN;
|
|
12
|
+
export const Array = EchoSchema.RefArray;
|
|
17
13
|
|
|
18
14
|
/**
|
|
19
15
|
* Extract reference target.
|
|
20
16
|
*/
|
|
21
17
|
export type Target<R extends Any> = R extends EchoSchema.Ref<infer T> ? T : never;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Reference resolver.
|
|
21
|
+
*/
|
|
22
|
+
export type Resolver = EchoSchema.RefResolver;
|
|
23
|
+
|
|
24
|
+
export const isRef: (value: unknown) => value is Any = EchoSchema.Ref.isRef;
|
|
25
|
+
|
|
26
|
+
export const make = EchoSchema.Ref.make;
|
|
27
|
+
|
|
28
|
+
// TODO(dmaretskyi): Consider just allowing `make` to accept DXN.
|
|
29
|
+
export const fromDXN = EchoSchema.Ref.fromDXN;
|
package/src/Relation.ts
CHANGED
|
@@ -3,11 +3,16 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import * as EchoSchema from '@dxos/echo-schema';
|
|
6
|
-
import { invariant } from '@dxos/invariant';
|
|
6
|
+
import { assertArgument, invariant } from '@dxos/invariant';
|
|
7
|
+
import { DXN } from '@dxos/keys';
|
|
7
8
|
import * as LiveObject from '@dxos/live-object';
|
|
9
|
+
import { assumeType } from '@dxos/util';
|
|
8
10
|
|
|
9
11
|
export type Any = EchoSchema.AnyEchoObject & EchoSchema.RelationSourceTargetRefs;
|
|
10
12
|
|
|
13
|
+
export const Source = EchoSchema.RelationSourceId;
|
|
14
|
+
export const Target = EchoSchema.RelationTargetId;
|
|
15
|
+
|
|
11
16
|
export const make = LiveObject.live;
|
|
12
17
|
|
|
13
18
|
export const isRelation = (value: unknown): value is Any => {
|
|
@@ -18,16 +23,40 @@ export const isRelation = (value: unknown): value is Any => {
|
|
|
18
23
|
return true;
|
|
19
24
|
}
|
|
20
25
|
|
|
21
|
-
const kind = (value as any)[EchoSchema.
|
|
26
|
+
const kind = (value as any)[EchoSchema.EntityKindId];
|
|
22
27
|
return kind === EchoSchema.EntityKind.Relation;
|
|
23
28
|
};
|
|
24
29
|
|
|
30
|
+
/**
|
|
31
|
+
* @returns Relation source DXN.
|
|
32
|
+
* @throws If the object is not a relation.
|
|
33
|
+
*/
|
|
34
|
+
export const getSourceDXN = (value: Any): DXN => {
|
|
35
|
+
assertArgument(isRelation(value), 'Expected a relation');
|
|
36
|
+
assumeType<EchoSchema.InternalObjectProps>(value);
|
|
37
|
+
const dxn = value[EchoSchema.RelationSourceDXNId];
|
|
38
|
+
invariant(dxn instanceof DXN);
|
|
39
|
+
return dxn;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @returns Relation target DXN.
|
|
44
|
+
* @throws If the object is not a relation.
|
|
45
|
+
*/
|
|
46
|
+
export const getTargetDXN = (value: Any): DXN => {
|
|
47
|
+
assertArgument(isRelation(value), 'Expected a relation');
|
|
48
|
+
assumeType<EchoSchema.InternalObjectProps>(value);
|
|
49
|
+
const dxn = value[EchoSchema.RelationTargetDXNId];
|
|
50
|
+
invariant(dxn instanceof DXN);
|
|
51
|
+
return dxn;
|
|
52
|
+
};
|
|
53
|
+
|
|
25
54
|
/**
|
|
26
55
|
* @returns Relation source.
|
|
27
56
|
* @throws If the object is not a relation.
|
|
28
57
|
*/
|
|
29
58
|
export const getSource = <T extends Any>(relation: T): EchoSchema.RelationSource<T> => {
|
|
30
|
-
|
|
59
|
+
assertArgument(isRelation(relation), 'Expected a relation');
|
|
31
60
|
const obj = relation[EchoSchema.RelationSourceId];
|
|
32
61
|
invariant(obj !== undefined, `Invalid source: ${relation.id}`);
|
|
33
62
|
return obj;
|
|
@@ -38,7 +67,7 @@ export const getSource = <T extends Any>(relation: T): EchoSchema.RelationSource
|
|
|
38
67
|
* @throws If the object is not a relation.
|
|
39
68
|
*/
|
|
40
69
|
export const getTarget = <T extends Any>(relation: T): EchoSchema.RelationTarget<T> => {
|
|
41
|
-
|
|
70
|
+
assertArgument(isRelation(relation), 'Expected a relation');
|
|
42
71
|
const obj = relation[EchoSchema.RelationTargetId];
|
|
43
72
|
invariant(obj !== undefined, `Invalid target: ${relation.id}`);
|
|
44
73
|
return obj;
|
package/src/Type.ts
CHANGED
|
@@ -19,16 +19,7 @@ export type Schema = EchoSchema.EchoSchema;
|
|
|
19
19
|
*/
|
|
20
20
|
export const Obj = EchoSchema.EchoObject;
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
* EchoRelation schema.
|
|
24
|
-
*/
|
|
25
|
-
export const Relation = EchoSchema.EchoRelation;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Ref schema.
|
|
29
|
-
*/
|
|
30
|
-
export const Ref: <S extends Obj.Any>(schema: S) => EchoSchema.Ref$<Schema.Schema.Type<S>> = EchoSchema.Ref;
|
|
31
|
-
|
|
22
|
+
// TODO(buurdon): Move to Obj?
|
|
32
23
|
export namespace Obj {
|
|
33
24
|
/**
|
|
34
25
|
* Type that represents an arbitrary schema type of an object.
|
|
@@ -38,6 +29,12 @@ export namespace Obj {
|
|
|
38
29
|
export type Any = Schema.Schema.AnyNoContext;
|
|
39
30
|
}
|
|
40
31
|
|
|
32
|
+
/**
|
|
33
|
+
* EchoRelation schema.
|
|
34
|
+
*/
|
|
35
|
+
export const Relation = EchoSchema.EchoRelation;
|
|
36
|
+
|
|
37
|
+
// TODO(buurdon): Move to Relation?
|
|
41
38
|
export namespace Relation {
|
|
42
39
|
/**
|
|
43
40
|
* Type that represents an arbitrary schema type of a relation.
|
|
@@ -57,6 +54,14 @@ export namespace Relation {
|
|
|
57
54
|
export type Source<A> = A extends EchoSchema.RelationSourceTargetRefs<infer _T, infer S> ? S : never;
|
|
58
55
|
}
|
|
59
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Ref schema.
|
|
59
|
+
*/
|
|
60
|
+
export const Ref: <S extends Obj.Any>(schema: S) => EchoSchema.Ref$<Schema.Schema.Type<S>> = EchoSchema.Ref;
|
|
61
|
+
|
|
62
|
+
export interface Ref<T> extends Schema.SchemaClass<EchoSchema.Ref<T>, EncodedReference> {}
|
|
63
|
+
|
|
64
|
+
// TODO(buurdon): Move to Ref?
|
|
60
65
|
export namespace Ref {
|
|
61
66
|
/**
|
|
62
67
|
* Type that represents an arbitrary schema type of a reference.
|
|
@@ -112,16 +117,16 @@ export { EntityKind as Kind } from '@dxos/echo-schema';
|
|
|
112
117
|
/**
|
|
113
118
|
* @returns True if the schema is mutable.
|
|
114
119
|
*/
|
|
115
|
-
export const isMutable =
|
|
116
|
-
return EchoSchema.isMutable(schema);
|
|
117
|
-
};
|
|
120
|
+
export const isMutable = EchoSchema.isMutable;
|
|
118
121
|
|
|
119
122
|
export { SpaceId, ObjectId, DXN } from '@dxos/keys';
|
|
120
123
|
|
|
121
124
|
export {
|
|
122
125
|
//
|
|
123
126
|
Expando,
|
|
127
|
+
// TODO(burdon): Standardize.
|
|
128
|
+
Format,
|
|
124
129
|
JsonSchemaType as JsonSchema,
|
|
130
|
+
toEffectSchema,
|
|
125
131
|
toJsonSchema,
|
|
126
|
-
Format,
|
|
127
132
|
} from '@dxos/echo-schema';
|
package/src/index.ts
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export * as
|
|
5
|
+
export * as Key from './Key';
|
|
6
6
|
export * as Obj from './Obj';
|
|
7
|
-
export * as Relation from './Relation';
|
|
8
7
|
export * as Ref from './Ref';
|
|
8
|
+
export * as Relation from './Relation';
|
|
9
|
+
export * as Type from './Type';
|
|
9
10
|
|
|
10
|
-
export {
|
|
11
|
+
export { DXN } from '@dxos/keys';
|
|
11
12
|
export { Filter, Query } from '@dxos/echo-schema';
|
|
13
|
+
export { type Live } from '@dxos/live-object';
|
package/src/test/api.test.ts
CHANGED
|
@@ -117,12 +117,27 @@ describe('Experimental API review', () => {
|
|
|
117
117
|
|
|
118
118
|
expect(Schema.is(Testing.Person)(contact)).to.be.true;
|
|
119
119
|
expect(Testing.Person.instanceOf(contact)).to.be.true;
|
|
120
|
-
expect(Obj.instanceOf(Testing.Person
|
|
121
|
-
expect(Obj.instanceOf(Testing.Organization
|
|
120
|
+
expect(Obj.instanceOf(Testing.Person, contact)).to.be.true;
|
|
121
|
+
expect(Obj.instanceOf(Testing.Organization, organization)).to.be.true;
|
|
122
|
+
|
|
123
|
+
const isPerson = Obj.instanceOf(Testing.Person);
|
|
124
|
+
const x: any = {};
|
|
125
|
+
if (isPerson(x)) {
|
|
126
|
+
x.name;
|
|
127
|
+
}
|
|
122
128
|
});
|
|
123
129
|
|
|
124
130
|
test('default props', ({ expect }) => {
|
|
125
131
|
const message = Obj.make(Testing.Message, Testing.MessageStruct.make({}));
|
|
126
132
|
expect(message.timestamp).to.exist;
|
|
127
133
|
});
|
|
134
|
+
|
|
135
|
+
test('Obj.isObject', ({ expect }) => {
|
|
136
|
+
const guy = Obj.make(Testing.Person, { name: 'Test' });
|
|
137
|
+
expect(Obj.isObject(guy)).to.be.true;
|
|
138
|
+
expect(Obj.isObject(null)).to.be.false;
|
|
139
|
+
expect(Obj.isObject(undefined)).to.be.false;
|
|
140
|
+
expect(Obj.isObject(1)).to.be.false;
|
|
141
|
+
expect(Obj.isObject('string')).to.be.false;
|
|
142
|
+
});
|
|
128
143
|
});
|