@dxos/echo 0.8.4-main.72ec0f3 → 0.8.4-main.7ace549
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/{chunk-BIDAASFK.mjs → chunk-7GH6RXJ3.mjs} +2246 -2290
- package/dist/lib/browser/chunk-7GH6RXJ3.mjs.map +7 -0
- package/dist/lib/browser/chunk-E4UTVJNF.mjs +1111 -0
- package/dist/lib/browser/chunk-E4UTVJNF.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +18 -8
- package/dist/lib/browser/internal/index.mjs +45 -45
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +252 -191
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node-esm/chunk-JE5RXM2I.mjs +1111 -0
- package/dist/lib/node-esm/chunk-JE5RXM2I.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-3SVRRCUU.mjs → chunk-M4B6BMD2.mjs} +2246 -2290
- package/dist/lib/node-esm/chunk-M4B6BMD2.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +18 -8
- package/dist/lib/node-esm/internal/index.mjs +45 -45
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +252 -191
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/Annotation.d.ts +2 -0
- package/dist/types/src/Annotation.d.ts.map +1 -0
- package/dist/types/src/Database.d.ts +137 -0
- package/dist/types/src/Database.d.ts.map +1 -0
- package/dist/types/src/Entity.d.ts +36 -0
- package/dist/types/src/Entity.d.ts.map +1 -0
- package/dist/types/src/Format.d.ts +4 -0
- package/dist/types/src/Format.d.ts.map +1 -0
- package/dist/types/src/JsonSchema.d.ts +9 -0
- package/dist/types/src/JsonSchema.d.ts.map +1 -0
- package/dist/types/src/Key.d.ts +1 -0
- package/dist/types/src/Key.d.ts.map +1 -1
- package/dist/types/src/Obj.d.ts +79 -70
- package/dist/types/src/Obj.d.ts.map +1 -1
- package/dist/types/src/Ref.d.ts +10 -10
- package/dist/types/src/Ref.d.ts.map +1 -1
- package/dist/types/src/Relation.d.ts +14 -11
- package/dist/types/src/Relation.d.ts.map +1 -1
- package/dist/types/src/Tag.d.ts +1 -1
- package/dist/types/src/Tag.d.ts.map +1 -1
- package/dist/types/src/Type.d.ts +39 -49
- package/dist/types/src/Type.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +6 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/internal/annotations/annotations.d.ts +174 -0
- package/dist/types/src/internal/annotations/annotations.d.ts.map +1 -0
- package/dist/types/src/internal/annotations/annotations.test.d.ts.map +1 -0
- package/dist/types/src/internal/annotations/index.d.ts +3 -0
- package/dist/types/src/internal/annotations/index.d.ts.map +1 -0
- package/dist/types/src/internal/annotations/util.d.ts +26 -0
- package/dist/types/src/internal/annotations/util.d.ts.map +1 -0
- package/dist/types/src/internal/entities/entity.d.ts +10 -0
- package/dist/types/src/internal/entities/entity.d.ts.map +1 -0
- package/dist/types/src/internal/{object → entities}/expando.d.ts +3 -1
- package/dist/types/src/internal/entities/expando.d.ts.map +1 -0
- package/dist/types/src/internal/entities/index.d.ts +6 -0
- package/dist/types/src/internal/entities/index.d.ts.map +1 -0
- package/dist/types/src/internal/entities/model.d.ts +70 -0
- package/dist/types/src/internal/entities/model.d.ts.map +1 -0
- package/dist/types/src/internal/entities/object.d.ts +11 -0
- package/dist/types/src/internal/entities/object.d.ts.map +1 -0
- package/dist/types/src/internal/entities/relation.d.ts +55 -0
- package/dist/types/src/internal/entities/relation.d.ts.map +1 -0
- package/dist/types/src/internal/entities/util.d.ts +2 -0
- package/dist/types/src/internal/entities/util.d.ts.map +1 -0
- package/dist/types/src/internal/formats/format.d.ts +2 -0
- package/dist/types/src/internal/formats/format.d.ts.map +1 -1
- package/dist/types/src/internal/formats/types.d.ts +7 -7
- package/dist/types/src/internal/formats/types.d.ts.map +1 -1
- package/dist/types/src/internal/index.d.ts +6 -10
- package/dist/types/src/internal/index.d.ts.map +1 -1
- package/dist/types/src/internal/json-schema/annotations.d.ts.map +1 -0
- package/dist/types/src/internal/json-schema/effect-schema.test.d.ts.map +1 -0
- package/dist/types/src/internal/json-schema/index.d.ts +2 -0
- package/dist/types/src/internal/json-schema/index.d.ts.map +1 -1
- package/dist/types/src/internal/json-schema/json-schema-type.d.ts.map +1 -1
- package/dist/types/src/internal/{json → json-schema}/json-schema.d.ts +2 -1
- package/dist/types/src/internal/json-schema/json-schema.d.ts.map +1 -0
- package/dist/types/src/internal/json-schema/json-schema.test.d.ts.map +1 -0
- package/dist/types/src/internal/object/common.d.ts.map +1 -1
- package/dist/types/src/internal/object/{create.d.ts → create-object.d.ts} +8 -9
- package/dist/types/src/internal/object/create-object.d.ts.map +1 -0
- package/dist/types/src/internal/object/create-object.test.d.ts +2 -0
- package/dist/types/src/internal/object/create-object.test.d.ts.map +1 -0
- package/dist/types/src/internal/object/deleted.d.ts +2 -2
- package/dist/types/src/internal/object/deleted.d.ts.map +1 -1
- package/dist/types/src/internal/object/ids.d.ts.map +1 -1
- package/dist/types/src/internal/object/index.d.ts +3 -11
- package/dist/types/src/internal/object/index.d.ts.map +1 -1
- package/dist/types/src/internal/object/json-serializer.d.ts +1 -2
- package/dist/types/src/internal/object/json-serializer.d.ts.map +1 -1
- package/dist/types/src/internal/object/schema-validator.d.ts.map +1 -1
- package/dist/types/src/internal/object/typed-object.d.ts +1 -1
- package/dist/types/src/internal/object/typed-object.d.ts.map +1 -1
- package/dist/types/src/internal/proxy/index.d.ts +3 -0
- package/dist/types/src/internal/proxy/index.d.ts.map +1 -0
- package/dist/types/src/internal/proxy/make-object.d.ts +16 -0
- package/dist/types/src/internal/proxy/make-object.d.ts.map +1 -0
- package/dist/types/src/internal/proxy/typed-handler.d.ts +1 -1
- package/dist/types/src/internal/proxy/typed-handler.d.ts.map +1 -1
- package/dist/types/src/internal/ref/ref.d.ts +15 -12
- package/dist/types/src/internal/ref/ref.d.ts.map +1 -1
- package/dist/types/src/internal/schema/compose.d.ts.map +1 -0
- package/dist/types/src/internal/schema/compose.test.d.ts.map +1 -0
- package/dist/types/src/internal/schema/echo-schema.d.ts +9 -9
- package/dist/types/src/internal/schema/echo-schema.d.ts.map +1 -1
- package/dist/types/src/internal/schema/index.d.ts +3 -4
- package/dist/types/src/internal/schema/index.d.ts.map +1 -1
- package/dist/types/src/internal/schema/persistent-schema.d.ts +18 -0
- package/dist/types/src/internal/schema/persistent-schema.d.ts.map +1 -0
- package/dist/types/src/internal/schema/runtime-schema-registry.d.ts +1 -1
- package/dist/types/src/internal/schema/runtime-schema-registry.d.ts.map +1 -1
- package/dist/types/src/internal/types/base.d.ts +37 -0
- package/dist/types/src/internal/types/base.d.ts.map +1 -0
- package/dist/types/src/internal/{ast/entity-kind.d.ts → types/entity.d.ts} +3 -1
- package/dist/types/src/internal/types/entity.d.ts.map +1 -0
- package/dist/types/src/internal/types/index.d.ts +5 -2
- package/dist/types/src/internal/types/index.d.ts.map +1 -1
- package/dist/types/src/internal/{object → types}/meta.d.ts +20 -11
- package/dist/types/src/internal/types/meta.d.ts.map +1 -0
- package/dist/types/src/internal/types/typename.d.ts +13 -0
- package/dist/types/src/internal/types/typename.d.ts.map +1 -0
- package/dist/types/src/internal/{object → types}/version.d.ts +3 -2
- package/dist/types/src/internal/types/version.d.ts.map +1 -0
- package/dist/types/src/query/filter.d.ts +167 -0
- package/dist/types/src/query/filter.d.ts.map +1 -0
- package/dist/types/src/query/index.d.ts +3 -0
- package/dist/types/src/query/index.d.ts.map +1 -1
- package/dist/types/src/query/order.d.ts +12 -0
- package/dist/types/src/query/order.d.ts.map +1 -0
- package/dist/types/src/query/query.d.ts +9 -145
- package/dist/types/src/query/query.d.ts.map +1 -1
- package/dist/types/src/query/testing.d.ts +51 -0
- package/dist/types/src/query/testing.d.ts.map +1 -0
- package/dist/types/src/{internal/query/query.d.ts → query/types.d.ts} +1 -1
- package/dist/types/src/query/types.d.ts.map +1 -0
- package/dist/types/src/query/util.d.ts +8 -0
- package/dist/types/src/query/util.d.ts.map +1 -0
- package/dist/types/src/{test → testing}/api.test.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +3 -3
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/test-data.d.ts +18 -0
- package/dist/types/src/testing/test-data.d.ts.map +1 -0
- package/dist/types/src/testing/{types.d.ts → test-schema.d.ts} +166 -57
- package/dist/types/src/testing/test-schema.d.ts.map +1 -0
- package/dist/types/src/{internal/testing/utils.d.ts → testing/util.d.ts} +11 -5
- package/dist/types/src/testing/util.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +46 -38
- package/src/Annotation.ts +17 -0
- package/src/Database.ts +189 -0
- package/src/Entity.ts +51 -0
- package/src/Format.ts +11 -0
- package/src/JsonSchema.ts +16 -0
- package/src/Key.ts +3 -0
- package/src/Obj.ts +248 -181
- package/src/Ref.ts +9 -9
- package/src/Relation.ts +58 -44
- package/src/Tag.ts +2 -1
- package/src/Type.ts +96 -83
- package/src/index.ts +9 -4
- package/src/internal/README.md +83 -0
- package/src/internal/{ast → annotations}/annotations.test.ts +13 -15
- package/src/internal/annotations/annotations.ts +463 -0
- package/src/internal/annotations/index.ts +6 -0
- package/src/internal/annotations/util.ts +70 -0
- package/src/internal/entities/entity.ts +109 -0
- package/src/internal/{object → entities}/expando.ts +7 -5
- package/src/internal/entities/index.ts +9 -0
- package/src/internal/{object → entities}/model.ts +49 -90
- package/src/internal/entities/object.ts +45 -0
- package/src/internal/entities/relation.ts +155 -0
- package/src/internal/entities/util.ts +33 -0
- package/src/internal/formats/date.test.ts +1 -1
- package/src/internal/formats/date.ts +5 -5
- package/src/internal/formats/format.test.ts +5 -5
- package/src/internal/formats/format.ts +3 -0
- package/src/internal/formats/number.ts +5 -5
- package/src/internal/formats/object.ts +2 -2
- package/src/internal/formats/string.ts +9 -9
- package/src/internal/formats/types.ts +44 -39
- package/src/internal/index.ts +29 -13
- package/src/internal/{json → json-schema}/annotations.ts +1 -1
- package/src/internal/json-schema/index.ts +2 -0
- package/src/internal/json-schema/json-schema-type.ts +4 -3
- package/src/internal/{json → json-schema}/json-schema.test.ts +48 -38
- package/src/internal/{json → json-schema}/json-schema.ts +16 -7
- package/src/internal/object/common.ts +3 -4
- package/src/internal/object/{create.test.ts → create-object.test.ts} +30 -32
- package/src/internal/object/{create.ts → create-object.ts} +33 -34
- package/src/internal/object/deleted.ts +6 -6
- package/src/internal/object/ids.ts +1 -1
- package/src/internal/object/index.ts +3 -11
- package/src/internal/object/inspect.ts +3 -5
- package/src/internal/object/json-serializer.test.ts +33 -38
- package/src/internal/object/json-serializer.ts +43 -38
- package/src/internal/object/schema-validator.ts +3 -2
- package/src/internal/object/typed-object.test.ts +1 -1
- package/src/internal/object/typed-object.ts +11 -5
- package/src/internal/proxy/handler.test.ts +30 -20
- package/src/internal/proxy/index.ts +6 -0
- package/src/internal/proxy/{reactive-object.ts → make-object.ts} +19 -14
- package/src/internal/proxy/schema.test.ts +5 -4
- package/src/internal/proxy/typed-handler.test.ts +28 -28
- package/src/internal/proxy/typed-handler.ts +8 -3
- package/src/internal/proxy/typed-object.test.ts +16 -11
- package/src/internal/ref/ref.test.ts +8 -7
- package/src/internal/ref/ref.ts +24 -20
- package/src/internal/{projection → schema}/compose.test.ts +5 -6
- package/src/internal/{projection → schema}/compose.ts +2 -1
- package/src/internal/schema/echo-schema.ts +30 -28
- package/src/internal/schema/index.ts +3 -4
- package/src/internal/schema/manipulation.ts +1 -1
- package/src/internal/schema/persistent-schema.ts +28 -0
- package/src/internal/schema/runtime-schema-registry.ts +4 -4
- package/src/internal/types/base.ts +58 -0
- package/src/internal/{ast/entity-kind.ts → types/entity.ts} +8 -0
- package/src/internal/types/index.ts +5 -2
- package/src/internal/{object → types}/meta.ts +32 -17
- package/src/internal/types/typename.ts +45 -0
- package/src/internal/types/version.ts +20 -0
- package/src/query/filter.ts +455 -0
- package/src/query/index.ts +4 -1
- package/src/query/order.ts +34 -0
- package/src/query/query.test.ts +48 -115
- package/src/query/query.ts +16 -502
- package/src/query/testing.ts +64 -0
- package/src/query/util.ts +25 -0
- package/src/testing/api.test.ts +100 -0
- package/src/testing/index.ts +3 -3
- package/src/testing/test-data.ts +130 -0
- package/src/testing/test-schema.ts +213 -0
- package/src/{internal/testing/utils.ts → testing/util.ts} +38 -14
- package/dist/lib/browser/chunk-BIDAASFK.mjs.map +0 -7
- package/dist/lib/browser/chunk-ZDLCWGEW.mjs +0 -410
- package/dist/lib/browser/chunk-ZDLCWGEW.mjs.map +0 -7
- package/dist/lib/browser/chunk-ZFRJKT4A.mjs +0 -585
- package/dist/lib/browser/chunk-ZFRJKT4A.mjs.map +0 -7
- package/dist/lib/browser/query/index.mjs +0 -13
- package/dist/lib/browser/query/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-3SVRRCUU.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-CGDHRZWH.mjs +0 -585
- package/dist/lib/node-esm/chunk-CGDHRZWH.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HWS6VBQC.mjs +0 -410
- package/dist/lib/node-esm/chunk-HWS6VBQC.mjs.map +0 -7
- package/dist/lib/node-esm/query/index.mjs +0 -13
- package/dist/lib/node-esm/query/index.mjs.map +0 -7
- package/dist/types/src/internal/ast/annotation-helper.d.ts +0 -8
- package/dist/types/src/internal/ast/annotation-helper.d.ts.map +0 -1
- package/dist/types/src/internal/ast/annotations.d.ts +0 -131
- package/dist/types/src/internal/ast/annotations.d.ts.map +0 -1
- package/dist/types/src/internal/ast/annotations.test.d.ts.map +0 -1
- package/dist/types/src/internal/ast/entity-kind.d.ts.map +0 -1
- package/dist/types/src/internal/ast/index.d.ts +0 -5
- package/dist/types/src/internal/ast/index.d.ts.map +0 -1
- package/dist/types/src/internal/ast/types.d.ts +0 -6
- package/dist/types/src/internal/ast/types.d.ts.map +0 -1
- package/dist/types/src/internal/json/annotations.d.ts.map +0 -1
- package/dist/types/src/internal/json/effect-schema.test.d.ts.map +0 -1
- package/dist/types/src/internal/json/index.d.ts +0 -2
- package/dist/types/src/internal/json/index.d.ts.map +0 -1
- package/dist/types/src/internal/json/json-schema.d.ts.map +0 -1
- package/dist/types/src/internal/json/json-schema.test.d.ts.map +0 -1
- package/dist/types/src/internal/object/accessors.d.ts +0 -37
- package/dist/types/src/internal/object/accessors.d.ts.map +0 -1
- package/dist/types/src/internal/object/create.d.ts.map +0 -1
- package/dist/types/src/internal/object/create.test.d.ts +0 -2
- package/dist/types/src/internal/object/create.test.d.ts.map +0 -1
- package/dist/types/src/internal/object/entity.d.ts +0 -33
- package/dist/types/src/internal/object/entity.d.ts.map +0 -1
- package/dist/types/src/internal/object/expando.d.ts.map +0 -1
- package/dist/types/src/internal/object/meta.d.ts.map +0 -1
- package/dist/types/src/internal/object/model.d.ts +0 -117
- package/dist/types/src/internal/object/model.d.ts.map +0 -1
- package/dist/types/src/internal/object/relation.d.ts +0 -17
- package/dist/types/src/internal/object/relation.d.ts.map +0 -1
- package/dist/types/src/internal/object/typename.d.ts +0 -15
- package/dist/types/src/internal/object/typename.d.ts.map +0 -1
- package/dist/types/src/internal/object/version.d.ts.map +0 -1
- package/dist/types/src/internal/projection/compose.d.ts.map +0 -1
- package/dist/types/src/internal/projection/compose.test.d.ts.map +0 -1
- package/dist/types/src/internal/projection/index.d.ts +0 -2
- package/dist/types/src/internal/projection/index.d.ts.map +0 -1
- package/dist/types/src/internal/proxy/reactive-object.d.ts +0 -15
- package/dist/types/src/internal/proxy/reactive-object.d.ts.map +0 -1
- package/dist/types/src/internal/query/index.d.ts +0 -2
- package/dist/types/src/internal/query/index.d.ts.map +0 -1
- package/dist/types/src/internal/query/query.d.ts.map +0 -1
- package/dist/types/src/internal/schema/stored-schema.d.ts +0 -13
- package/dist/types/src/internal/schema/stored-schema.d.ts.map +0 -1
- package/dist/types/src/internal/testing/index.d.ts +0 -3
- package/dist/types/src/internal/testing/index.d.ts.map +0 -1
- package/dist/types/src/internal/testing/types.d.ts +0 -381
- package/dist/types/src/internal/testing/types.d.ts.map +0 -1
- package/dist/types/src/internal/testing/utils.d.ts.map +0 -1
- package/dist/types/src/internal/types/types.d.ts +0 -79
- package/dist/types/src/internal/types/types.d.ts.map +0 -1
- package/dist/types/src/internal/types/types.test.d.ts +0 -2
- package/dist/types/src/internal/types/types.test.d.ts.map +0 -1
- package/dist/types/src/internal/types/util.d.ts +0 -5
- package/dist/types/src/internal/types/util.d.ts.map +0 -1
- package/dist/types/src/testing/echo-schema.d.ts +0 -7
- package/dist/types/src/testing/echo-schema.d.ts.map +0 -1
- package/dist/types/src/testing/types.d.ts.map +0 -1
- package/src/internal/ast/annotation-helper.ts +0 -22
- package/src/internal/ast/annotations.ts +0 -226
- package/src/internal/ast/index.ts +0 -8
- package/src/internal/ast/types.ts +0 -17
- package/src/internal/json/index.ts +0 -5
- package/src/internal/object/accessors.ts +0 -153
- package/src/internal/object/entity.ts +0 -248
- package/src/internal/object/relation.ts +0 -24
- package/src/internal/object/typename.ts +0 -61
- package/src/internal/object/version.ts +0 -22
- package/src/internal/projection/index.ts +0 -5
- package/src/internal/query/index.ts +0 -5
- package/src/internal/schema/stored-schema.ts +0 -26
- package/src/internal/testing/index.ts +0 -6
- package/src/internal/testing/types.ts +0 -144
- package/src/internal/types/types.test.ts +0 -48
- package/src/internal/types/types.ts +0 -176
- package/src/internal/types/util.ts +0 -9
- package/src/test/api.test.ts +0 -180
- package/src/testing/echo-schema.ts +0 -39
- package/src/testing/types.ts +0 -108
- /package/dist/types/src/internal/{ast → annotations}/annotations.test.d.ts +0 -0
- /package/dist/types/src/internal/{json → json-schema}/annotations.d.ts +0 -0
- /package/dist/types/src/internal/{json → json-schema}/effect-schema.test.d.ts +0 -0
- /package/dist/types/src/internal/{json → json-schema}/json-schema.test.d.ts +0 -0
- /package/dist/types/src/internal/{projection → schema}/compose.d.ts +0 -0
- /package/dist/types/src/internal/{projection → schema}/compose.test.d.ts +0 -0
- /package/dist/types/src/{test → testing}/api.test.d.ts +0 -0
- /package/src/internal/{json → json-schema}/effect-schema.test.ts +0 -0
- /package/src/{internal/query/query.ts → query/types.ts} +0 -0
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { invariant } from '@dxos/invariant';
|
|
6
|
-
import { DXN } from '@dxos/keys';
|
|
7
|
-
|
|
8
|
-
import { getSchemaTypename } from '../ast';
|
|
9
|
-
import { type BaseObject } from '../types';
|
|
10
|
-
|
|
11
|
-
import { getSchema } from './accessors';
|
|
12
|
-
import { TypeId } from './model';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Gets the typename of the object without the version.
|
|
16
|
-
* Returns only the name portion, not the DXN.
|
|
17
|
-
* @example "example.org/type/Contact"
|
|
18
|
-
*/
|
|
19
|
-
export const getTypename = (obj: BaseObject): string | undefined => {
|
|
20
|
-
const schema = getSchema(obj);
|
|
21
|
-
if (schema != null) {
|
|
22
|
-
// Try to extract typename from DXN.
|
|
23
|
-
return getSchemaTypename(schema);
|
|
24
|
-
} else {
|
|
25
|
-
const type = getType(obj);
|
|
26
|
-
return type?.asTypeDXN()?.type;
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @internal
|
|
32
|
-
*/
|
|
33
|
-
// TODO(dmaretskyi): Rename setTypeDXN.
|
|
34
|
-
export const setTypename = (obj: any, typename: DXN) => {
|
|
35
|
-
invariant(typename instanceof DXN, 'Invalid type.');
|
|
36
|
-
Object.defineProperty(obj, TypeId, {
|
|
37
|
-
value: typename,
|
|
38
|
-
writable: false,
|
|
39
|
-
enumerable: false,
|
|
40
|
-
configurable: false,
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @returns Object type as {@link DXN}.
|
|
46
|
-
* @returns undefined if the object doesn't have a type.
|
|
47
|
-
* @example `dxn:example.com/type/Person:1.0.0`
|
|
48
|
-
*/
|
|
49
|
-
export const getType = (obj: BaseObject): DXN | undefined => {
|
|
50
|
-
if (!obj) {
|
|
51
|
-
return undefined;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const type = (obj as any)[TypeId];
|
|
55
|
-
if (!type) {
|
|
56
|
-
return undefined;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
invariant(type instanceof DXN, 'Invalid object.');
|
|
60
|
-
return type;
|
|
61
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// NOTE: Those types must match the ones defined at @dxos/echo. We duplicated them murely due to technical constrains.
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Getter to get object version.
|
|
7
|
-
*/
|
|
8
|
-
//
|
|
9
|
-
// Copyright 2025 DXOS.org
|
|
10
|
-
//
|
|
11
|
-
|
|
12
|
-
export const VersionTypeId: unique symbol = Symbol('@dxos/echo/Version');
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Represent object version.
|
|
16
|
-
* May be backed by Automerge.
|
|
17
|
-
* Objects with no history are not versioned.
|
|
18
|
-
*/
|
|
19
|
-
export interface Version {
|
|
20
|
-
[VersionTypeId]: {};
|
|
21
|
-
automergeHeads?: string[];
|
|
22
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import * as Schema from 'effect/Schema';
|
|
6
|
-
|
|
7
|
-
import { SchemaVersion, Typename } from '../ast';
|
|
8
|
-
import { JsonSchemaType } from '../json-schema';
|
|
9
|
-
import { EchoObject } from '../object';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Persistent representation of a schema.
|
|
13
|
-
*/
|
|
14
|
-
export const StoredSchema = Schema.Struct({
|
|
15
|
-
name: Schema.optional(Schema.String),
|
|
16
|
-
typename: Typename,
|
|
17
|
-
version: SchemaVersion,
|
|
18
|
-
jsonSchema: JsonSchemaType,
|
|
19
|
-
}).pipe(
|
|
20
|
-
EchoObject({
|
|
21
|
-
typename: 'dxos.org/type/Schema',
|
|
22
|
-
version: '0.1.0',
|
|
23
|
-
}),
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
export type StoredSchema = Schema.Schema.Type<typeof StoredSchema>;
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import * as Schema from 'effect/Schema';
|
|
6
|
-
|
|
7
|
-
import { EchoObject, EchoRelation, TypedObject } from '../object';
|
|
8
|
-
import { Ref, type Ref$ } from '../ref';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @deprecated
|
|
12
|
-
*/
|
|
13
|
-
// TOOD(burdon): Reconcile with ../testing (remove this).
|
|
14
|
-
export namespace Testing {
|
|
15
|
-
//
|
|
16
|
-
// Primitives
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
const Circle = Schema.Struct({ type: Schema.Literal('circle'), radius: Schema.Number });
|
|
20
|
-
const Square = Schema.Struct({ type: Schema.Literal('square'), side: Schema.Number });
|
|
21
|
-
const Shape = Schema.Union(Circle, Square);
|
|
22
|
-
|
|
23
|
-
//
|
|
24
|
-
// Simple types
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
const TestNestedSchema = Schema.mutable(Schema.Struct({ field: Schema.String }));
|
|
28
|
-
export type TestNestedSchema = Schema.Schema.Type<typeof TestNestedSchema>;
|
|
29
|
-
export const TestNestedType = TestNestedSchema.pipe(
|
|
30
|
-
EchoObject({ typename: 'example.com/type/TestNested', version: '0.1.0' }),
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
//
|
|
34
|
-
// Complex types
|
|
35
|
-
// TODO(burdon): Change to Type.Obj.
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
export class EmptySchemaType extends TypedObject({
|
|
39
|
-
typename: 'example.com/type/Empty',
|
|
40
|
-
version: '0.1.0',
|
|
41
|
-
})({}) {}
|
|
42
|
-
|
|
43
|
-
const fields = {
|
|
44
|
-
string: Schema.String,
|
|
45
|
-
number: Schema.Number,
|
|
46
|
-
nullableShapeArray: Schema.mutable(Schema.Array(Schema.Union(Shape, Schema.Null))),
|
|
47
|
-
boolean: Schema.Boolean,
|
|
48
|
-
null: Schema.Null,
|
|
49
|
-
undefined: Schema.Undefined,
|
|
50
|
-
stringArray: Schema.mutable(Schema.Array(Schema.String)),
|
|
51
|
-
twoDimNumberArray: Schema.mutable(Schema.Array(Schema.mutable(Schema.Array(Schema.Number)))),
|
|
52
|
-
object: TestNestedSchema,
|
|
53
|
-
objectArray: Schema.mutable(Schema.Array(TestNestedSchema)),
|
|
54
|
-
nested: Schema.optional(Ref(TestNestedType)),
|
|
55
|
-
other: Schema.Any,
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export const TestSchema = Schema.mutable(Schema.partial(Schema.Struct(fields)));
|
|
59
|
-
export type TestSchema = Schema.Schema.Type<typeof TestSchema>;
|
|
60
|
-
|
|
61
|
-
export class TestSchemaType extends TypedObject({
|
|
62
|
-
typename: 'example.com/type/Test',
|
|
63
|
-
version: '0.1.0',
|
|
64
|
-
})(fields, { partial: true }) {} // TODO(burdon): Partial?
|
|
65
|
-
|
|
66
|
-
// TODO(burdon): Why do we use need this rather then TestSchemaType?
|
|
67
|
-
export const TestType = TestSchema.pipe(
|
|
68
|
-
EchoObject({
|
|
69
|
-
typename: 'example.com/type/Test',
|
|
70
|
-
version: '0.1.0',
|
|
71
|
-
}),
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
export class TestClass {
|
|
75
|
-
field = 'value';
|
|
76
|
-
toJSON() {
|
|
77
|
-
return { field: this.field };
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// TODO(dmaretskyi): Another top-level Schema.mutable call as a workaround for the regression in the last minor.
|
|
82
|
-
export const TestSchemaWithClass = Schema.mutable(
|
|
83
|
-
Schema.extend(
|
|
84
|
-
TestSchema,
|
|
85
|
-
Schema.mutable(
|
|
86
|
-
Schema.Struct({
|
|
87
|
-
classInstance: Schema.optional(Schema.instanceOf(TestClass)),
|
|
88
|
-
}),
|
|
89
|
-
),
|
|
90
|
-
),
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
export type TestSchemaWithClass = Schema.Schema.Type<typeof TestSchemaWithClass>;
|
|
94
|
-
|
|
95
|
-
export class Person extends TypedObject({
|
|
96
|
-
typename: 'example.com/type/Person',
|
|
97
|
-
version: '0.1.0',
|
|
98
|
-
})(
|
|
99
|
-
{
|
|
100
|
-
name: Schema.String,
|
|
101
|
-
username: Schema.String,
|
|
102
|
-
email: Schema.String,
|
|
103
|
-
phoneNumbers: Schema.optional(Schema.Array(Schema.Struct({ label: Schema.String, number: Schema.String }))),
|
|
104
|
-
tasks: Schema.suspend((): Schema.mutable<Schema.Array$<Ref$<Task>>> => Schema.mutable(Schema.Array(Ref(Task)))),
|
|
105
|
-
address: Schema.Struct({
|
|
106
|
-
city: Schema.optional(Schema.String),
|
|
107
|
-
state: Schema.optional(Schema.String),
|
|
108
|
-
zip: Schema.optional(Schema.String),
|
|
109
|
-
coordinates: Schema.Struct({
|
|
110
|
-
lat: Schema.optional(Schema.Number),
|
|
111
|
-
lng: Schema.optional(Schema.Number),
|
|
112
|
-
}),
|
|
113
|
-
}),
|
|
114
|
-
},
|
|
115
|
-
{ partial: true },
|
|
116
|
-
) {}
|
|
117
|
-
|
|
118
|
-
export class Task extends TypedObject({
|
|
119
|
-
typename: 'example.com/type/Task',
|
|
120
|
-
version: '0.1.0',
|
|
121
|
-
})(
|
|
122
|
-
{
|
|
123
|
-
title: Schema.optional(Schema.String),
|
|
124
|
-
completed: Schema.optional(Schema.Boolean),
|
|
125
|
-
assignee: Schema.optional(Ref(Person)),
|
|
126
|
-
previous: Schema.optional(Schema.suspend((): Ref$<Task> => Ref(Task))),
|
|
127
|
-
subTasks: Schema.optional(Schema.mutable(Schema.Array(Schema.suspend((): Ref$<Task> => Ref(Task))))),
|
|
128
|
-
description: Schema.optional(Schema.String),
|
|
129
|
-
},
|
|
130
|
-
{ partial: true },
|
|
131
|
-
) {}
|
|
132
|
-
|
|
133
|
-
export const HasManager = Schema.Struct({
|
|
134
|
-
since: Schema.optional(Schema.String),
|
|
135
|
-
}).pipe(
|
|
136
|
-
EchoRelation({
|
|
137
|
-
typename: 'example.com/type/HasManager',
|
|
138
|
-
version: '0.1.0',
|
|
139
|
-
source: Person,
|
|
140
|
-
target: Person,
|
|
141
|
-
}),
|
|
142
|
-
);
|
|
143
|
-
export interface HasManager extends Schema.Schema.Type<typeof HasManager> {}
|
|
144
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { describe, test } from 'vitest';
|
|
6
|
-
|
|
7
|
-
import { type JsonPath } from '@dxos/effect';
|
|
8
|
-
|
|
9
|
-
import { type BaseObject, getValue, setValue } from './types';
|
|
10
|
-
|
|
11
|
-
describe('Types', () => {
|
|
12
|
-
test('checks sanity', async ({ expect }) => {
|
|
13
|
-
const obj: BaseObject = {};
|
|
14
|
-
expect(obj).to.exist;
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('get/set deep', () => {
|
|
19
|
-
test('get/set operations', ({ expect }) => {
|
|
20
|
-
const obj = {
|
|
21
|
-
name: 'test',
|
|
22
|
-
items: ['a', 'b', 'c'],
|
|
23
|
-
nested: {
|
|
24
|
-
prop: 'value',
|
|
25
|
-
arr: [1, 2, 3],
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// Basic property access.
|
|
30
|
-
expect(getValue(obj, 'name' as JsonPath)).toBe('test');
|
|
31
|
-
|
|
32
|
-
// Array index access.
|
|
33
|
-
expect(getValue(obj, 'items[1]' as JsonPath)).toBe('b');
|
|
34
|
-
|
|
35
|
-
// Nested property access.
|
|
36
|
-
expect(getValue(obj, 'nested.prop' as JsonPath)).toBe('value');
|
|
37
|
-
|
|
38
|
-
// Nested array access.
|
|
39
|
-
expect(getValue(obj, 'nested.arr[2]' as JsonPath)).toBe(3);
|
|
40
|
-
|
|
41
|
-
// Setting values.
|
|
42
|
-
const updated1 = setValue(obj, 'items[1]' as JsonPath, 'x');
|
|
43
|
-
expect(updated1.items[1]).toBe('x');
|
|
44
|
-
|
|
45
|
-
const updated2 = setValue(obj, 'nested.arr[0]' as JsonPath, 99);
|
|
46
|
-
expect(updated2.nested.arr[0]).toBe(99);
|
|
47
|
-
});
|
|
48
|
-
});
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import * as Schema from 'effect/Schema';
|
|
6
|
-
import * as SchemaAST from 'effect/SchemaAST';
|
|
7
|
-
|
|
8
|
-
import { Reference } from '@dxos/echo-protocol';
|
|
9
|
-
import { type JsonPath, splitJsonPath } from '@dxos/effect';
|
|
10
|
-
import { DXN, type ObjectId } from '@dxos/keys';
|
|
11
|
-
import { getDeep, setDeep } from '@dxos/util';
|
|
12
|
-
|
|
13
|
-
import { getSchemaDXN } from '../ast';
|
|
14
|
-
import { type EntityKindId, type ObjectMeta, getType, getTypename } from '../object';
|
|
15
|
-
import { ATTR_META } from '../object/model';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Base type for all data objects (reactive, ECHO, and other raw objects).
|
|
19
|
-
* NOTE: This describes the base type for all database objects.
|
|
20
|
-
* It is stricter than `T extends {}` or `T extends object`.
|
|
21
|
-
*/
|
|
22
|
-
// TODO(dmaretskyi): Rename AnyProperties.
|
|
23
|
-
// TODO(dmaretskyi): Prefer `Record<string, unknown>` over `any`.
|
|
24
|
-
/**
|
|
25
|
-
* @deprecated use Obj.Any
|
|
26
|
-
*/
|
|
27
|
-
export type BaseObject = Record<string, any>;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Marker interface for object with an `id`.
|
|
31
|
-
*/
|
|
32
|
-
export interface HasId {
|
|
33
|
-
readonly id: ObjectId;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// TODO(dmaretskyi): Remove; this type effectively disables type safety due to `any`.
|
|
37
|
-
export type WithId<T extends BaseObject = BaseObject> = T & HasId;
|
|
38
|
-
|
|
39
|
-
export type ExcludeId<T extends BaseObject> = Omit<T, 'id'>;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Properties that are required for object creation.
|
|
43
|
-
*/
|
|
44
|
-
// TODO(dmaretskyi): Rename `MakeProps`?
|
|
45
|
-
export type CreationProps<T extends BaseObject> = Omit<T, 'id' | typeof EntityKindId>;
|
|
46
|
-
|
|
47
|
-
export type PropertyKey<T extends BaseObject> = Extract<keyof ExcludeId<T>, string>;
|
|
48
|
-
|
|
49
|
-
// TODO(dmaretskyi): Remove. This should be using the symbol type.
|
|
50
|
-
export type WithMeta = { [ATTR_META]?: ObjectMeta };
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* The raw object should not include the ECHO id, but may include metadata.
|
|
54
|
-
*/
|
|
55
|
-
export const RawObject = <S extends Schema.Schema.AnyNoContext>(
|
|
56
|
-
schema: S,
|
|
57
|
-
): Schema.Schema<ExcludeId<Schema.Schema.Type<S>> & WithMeta, Schema.Schema.Encoded<S>> => {
|
|
58
|
-
return Schema.make(SchemaAST.omit(schema.ast, ['id']));
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
//
|
|
62
|
-
// Utils
|
|
63
|
-
//
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Utility to split meta property from raw object.
|
|
67
|
-
* @deprecated Bad API.
|
|
68
|
-
*/
|
|
69
|
-
export const splitMeta = <T>(object: T & WithMeta): { object: T; meta?: ObjectMeta } => {
|
|
70
|
-
const meta = object[ATTR_META];
|
|
71
|
-
delete object[ATTR_META];
|
|
72
|
-
return { meta, object };
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
// TODO(burdon): Move to `@dxos/util`.
|
|
76
|
-
export const getValue = <T extends object>(obj: T, path: JsonPath): any => {
|
|
77
|
-
return getDeep(
|
|
78
|
-
obj,
|
|
79
|
-
splitJsonPath(path).map((p) => p.replace(/[[\]]/g, '')),
|
|
80
|
-
);
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
// TODO(burdon): Move to `@dxos/util`.
|
|
84
|
-
export const setValue = <T extends object>(obj: T, path: JsonPath, value: any): T => {
|
|
85
|
-
return setDeep(
|
|
86
|
-
obj,
|
|
87
|
-
splitJsonPath(path).map((p) => p.replace(/[[\]]/g, '')),
|
|
88
|
-
value,
|
|
89
|
-
);
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Returns a reference that will be used to point to a schema.
|
|
94
|
-
* @deprecated Use {@link getSchemaDXN} instead.
|
|
95
|
-
*/
|
|
96
|
-
export const getTypeReference = (schema: Schema.Schema.All | undefined): Reference | undefined => {
|
|
97
|
-
if (!schema) {
|
|
98
|
-
return undefined;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const schemaDXN = getSchemaDXN(schema);
|
|
102
|
-
if (!schemaDXN) {
|
|
103
|
-
return undefined;
|
|
104
|
-
}
|
|
105
|
-
return Reference.fromDXN(schemaDXN);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Returns a reference that will be used to point to a schema.
|
|
110
|
-
* @throws If it is not possible to reference this schema.
|
|
111
|
-
*
|
|
112
|
-
* @deprecated Use {@link getSchemaDXN} instead.
|
|
113
|
-
*/
|
|
114
|
-
export const requireTypeReference = (schema: Schema.Schema.AnyNoContext): Reference => {
|
|
115
|
-
const typeReference = getTypeReference(schema);
|
|
116
|
-
if (typeReference == null) {
|
|
117
|
-
// TODO(burdon): Catalog user-facing errors (this is too verbose).
|
|
118
|
-
throw new Error('Schema must be defined via TypedObject.');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return typeReference;
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
// TODO(burdon): Can we use `Schema.is`?
|
|
125
|
-
/**
|
|
126
|
-
* Checks if the object is an instance of the schema.
|
|
127
|
-
* Only typename is compared, the schema version is ignored.
|
|
128
|
-
*
|
|
129
|
-
* The following cases are considered to mean that the object is an instance of the schema:
|
|
130
|
-
* - Object was created with this exact schema.
|
|
131
|
-
* - Object was created with a different version of this schema.
|
|
132
|
-
* - Object was created with a different schema (maybe dynamic) that has the same typename.
|
|
133
|
-
*/
|
|
134
|
-
export const isInstanceOf = <Schema extends Schema.Schema.AnyNoContext>(
|
|
135
|
-
schema: Schema,
|
|
136
|
-
object: any,
|
|
137
|
-
): object is Schema.Schema.Type<Schema> => {
|
|
138
|
-
if (object == null) {
|
|
139
|
-
return false;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const schemaDXN = getSchemaDXN(schema);
|
|
143
|
-
if (!schemaDXN) {
|
|
144
|
-
throw new Error('Schema must have an object annotation.');
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const type = getType(object);
|
|
148
|
-
if (type && DXN.equals(type, schemaDXN)) {
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const typename = getTypename(object);
|
|
153
|
-
if (!typename) {
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const typeDXN = schemaDXN.asTypeDXN();
|
|
158
|
-
if (!typeDXN) {
|
|
159
|
-
return false;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return typeDXN.type === typename;
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Object that has an associated typename.
|
|
167
|
-
* The typename is retrievable using {@link getTypename}.
|
|
168
|
-
* The object can be used with {@link isInstanceOf} to check if it is an instance of a schema.
|
|
169
|
-
*/
|
|
170
|
-
export type HasTypename = {};
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Canonical type for all ECHO objects.
|
|
174
|
-
*/
|
|
175
|
-
// TODO(burdon): Reconcile with Obj.Any, Relation.Any.
|
|
176
|
-
export interface AnyEchoObject extends HasId, HasTypename {}
|
package/src/test/api.test.ts
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import * as Schema from 'effect/Schema';
|
|
6
|
-
import { describe, test } from 'vitest';
|
|
7
|
-
|
|
8
|
-
import { raise } from '@dxos/debug';
|
|
9
|
-
import { FormatAnnotation, FormatEnum } from '@dxos/echo/internal';
|
|
10
|
-
|
|
11
|
-
import { type Live, Obj, Ref, Relation, Type } from '../index';
|
|
12
|
-
|
|
13
|
-
namespace Testing {
|
|
14
|
-
export const Organization = Schema.Struct({
|
|
15
|
-
id: Type.ObjectId,
|
|
16
|
-
name: Schema.String,
|
|
17
|
-
}).pipe(
|
|
18
|
-
Type.Obj({
|
|
19
|
-
typename: 'example.com/type/Organization',
|
|
20
|
-
version: '0.1.0',
|
|
21
|
-
}),
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
export interface Organization extends Schema.Schema.Type<typeof Organization> {}
|
|
25
|
-
|
|
26
|
-
export const Person = Schema.Struct({
|
|
27
|
-
name: Schema.String,
|
|
28
|
-
dob: Schema.optional(Schema.String),
|
|
29
|
-
email: Schema.optional(Schema.String.pipe(FormatAnnotation.set(FormatEnum.Email))),
|
|
30
|
-
organization: Schema.optional(Type.Ref(Organization)),
|
|
31
|
-
}).pipe(
|
|
32
|
-
Type.Obj({
|
|
33
|
-
typename: 'example.com/type/Person',
|
|
34
|
-
version: '0.1.0',
|
|
35
|
-
}),
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
export interface Person extends Schema.Schema.Type<typeof Person> {}
|
|
39
|
-
|
|
40
|
-
// export const WorksFor = S.Struct({
|
|
41
|
-
// id: Type.ObjectId,
|
|
42
|
-
// since: S.String,
|
|
43
|
-
// jobTitle: S.String,
|
|
44
|
-
// ...Range({ from, to }),
|
|
45
|
-
// ...Provenance({ source: 'duckduckgo.com', confidence: 0.9 }), // keys
|
|
46
|
-
// ...Relation.make({ source: Contact, target: Organization }),
|
|
47
|
-
// }).pipe(
|
|
48
|
-
// Type.Relation({
|
|
49
|
-
// typename: 'example.com/relation/WorksFor',
|
|
50
|
-
// version: '0.1.0',
|
|
51
|
-
// }),
|
|
52
|
-
// );
|
|
53
|
-
|
|
54
|
-
// {
|
|
55
|
-
// const contact = db.add(create(Contact, { name: 'Test' }));
|
|
56
|
-
// const organization = db.add(create(Organization, { name: 'DXOS' }));
|
|
57
|
-
// db.add(create(WorksFor, { source: contact, target: organization }));
|
|
58
|
-
// }
|
|
59
|
-
|
|
60
|
-
export const WorksFor = Schema.Struct({
|
|
61
|
-
// id: Type.ObjectId,
|
|
62
|
-
role: Schema.String,
|
|
63
|
-
}).pipe(
|
|
64
|
-
Type.Relation({
|
|
65
|
-
typename: 'example.com/relation/WorksFor',
|
|
66
|
-
version: '0.1.0',
|
|
67
|
-
source: Person,
|
|
68
|
-
target: Organization,
|
|
69
|
-
}),
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
export interface WorksFor extends Schema.Schema.Type<typeof WorksFor> {}
|
|
73
|
-
|
|
74
|
-
// TODO(burdon): Fix (Type.Obj currently removes TypeLiteral that implements the `make` function).
|
|
75
|
-
// Property 'make' does not exist on type 'EchoObjectSchema<Struct<{ timestamp: PropertySignature<":", string, never, ":", string, true, never>; }>>'.ts(2339)
|
|
76
|
-
export const MessageStruct = Schema.Struct({
|
|
77
|
-
// TODO(burdon): Support S.Date; Custom Timestamp (with defaults).
|
|
78
|
-
// TODO(burdon): Support defaults (update create and create).
|
|
79
|
-
timestamp: Schema.String.pipe(
|
|
80
|
-
Schema.propertySignature,
|
|
81
|
-
Schema.withConstructorDefault(() => new Date().toISOString()),
|
|
82
|
-
),
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
export const Message = MessageStruct.pipe(
|
|
86
|
-
Type.Obj({
|
|
87
|
-
typename: 'example.com/type/Message',
|
|
88
|
-
version: '0.1.0',
|
|
89
|
-
}),
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
export interface Message extends Schema.Schema.Type<typeof Message> {}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
describe('Experimental API review', () => {
|
|
96
|
-
test('type checks', ({ expect }) => {
|
|
97
|
-
const contact = Obj.make(Testing.Person, { name: 'Test' });
|
|
98
|
-
const type: Schema.Schema<Testing.Person> = Obj.getSchema(contact) ?? raise(new Error('No schema found'));
|
|
99
|
-
|
|
100
|
-
expect(Type.getDXN(type)?.typename).to.eq(Testing.Person.typename);
|
|
101
|
-
expect(Type.getTypename(type)).to.eq('example.com/type/Person');
|
|
102
|
-
expect(Type.getVersion(type)).to.eq('0.1.0');
|
|
103
|
-
expect(Type.getMeta(type)).to.deep.eq({
|
|
104
|
-
kind: Type.Kind.Object,
|
|
105
|
-
typename: 'example.com/type/Person',
|
|
106
|
-
version: '0.1.0',
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
test('instance checks', ({ expect }) => {
|
|
111
|
-
const organization: Live<Testing.Organization> = Obj.make(Testing.Organization, { name: 'DXOS' });
|
|
112
|
-
const contact: Live<Testing.Person> = Obj.make(Testing.Person, {
|
|
113
|
-
name: 'Test',
|
|
114
|
-
organization: Ref.make(organization),
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
expect(Schema.is(Testing.Person)(contact)).to.be.true;
|
|
118
|
-
expect(Obj.instanceOf(Testing.Person, contact)).to.be.true;
|
|
119
|
-
expect(Obj.instanceOf(Testing.Organization, organization)).to.be.true;
|
|
120
|
-
|
|
121
|
-
const isPerson = Obj.instanceOf(Testing.Person);
|
|
122
|
-
const x: any = {};
|
|
123
|
-
if (isPerson(x)) {
|
|
124
|
-
x.name;
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
test('default props', ({ expect }) => {
|
|
129
|
-
const message = Obj.make(Testing.Message, Testing.MessageStruct.make({}));
|
|
130
|
-
expect(message.timestamp).to.exist;
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
test('Obj.isObject', ({ expect }) => {
|
|
134
|
-
const guy = Obj.make(Testing.Person, { name: 'Test' });
|
|
135
|
-
expect(Obj.isObject(guy)).to.be.true;
|
|
136
|
-
expect(Obj.isObject(null)).to.be.false;
|
|
137
|
-
expect(Obj.isObject(undefined)).to.be.false;
|
|
138
|
-
expect(Obj.isObject(1)).to.be.false;
|
|
139
|
-
expect(Obj.isObject('string')).to.be.false;
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
test('create relation', ({ expect }) => {
|
|
143
|
-
const person = Obj.make(Testing.Person, { name: 'Test' });
|
|
144
|
-
const organization = Obj.make(Testing.Organization, { name: 'DXOS' });
|
|
145
|
-
const worksFor = Relation.make(Testing.WorksFor, {
|
|
146
|
-
[Relation.Source]: person,
|
|
147
|
-
[Relation.Target]: organization,
|
|
148
|
-
role: 'CEO',
|
|
149
|
-
});
|
|
150
|
-
expect(Relation.getSource(worksFor)).to.eq(person);
|
|
151
|
-
expect(Relation.getTarget(worksFor)).to.eq(organization);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
test('version', ({ expect }) => {
|
|
155
|
-
const person = Obj.make(Testing.Person, { name: 'Test' });
|
|
156
|
-
const version = Obj.version(person);
|
|
157
|
-
expect(Obj.isVersion(version)).to.be.true;
|
|
158
|
-
expect(Obj.versionValid(version)).to.be.false;
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
test.skip('type narrowing', () => {
|
|
162
|
-
const any: Obj.Any | Relation.Any = null as any;
|
|
163
|
-
|
|
164
|
-
{
|
|
165
|
-
if (Obj.isObject(any)) {
|
|
166
|
-
any;
|
|
167
|
-
} else {
|
|
168
|
-
any;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
{
|
|
173
|
-
if (Relation.isRelation(any)) {
|
|
174
|
-
any;
|
|
175
|
-
} else {
|
|
176
|
-
any;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
});
|