@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
package/src/Obj.ts
CHANGED
|
@@ -5,32 +5,63 @@
|
|
|
5
5
|
import * as Function from 'effect/Function';
|
|
6
6
|
import * as Schema from 'effect/Schema';
|
|
7
7
|
|
|
8
|
+
import { type ForeignKey } from '@dxos/echo-protocol';
|
|
8
9
|
import { assertArgument, invariant } from '@dxos/invariant';
|
|
9
|
-
import { type DXN } from '@dxos/keys';
|
|
10
|
-
import
|
|
10
|
+
import { type DXN, ObjectId } from '@dxos/keys';
|
|
11
|
+
import { getSnapshot as getSnapshot$ } from '@dxos/live-object';
|
|
11
12
|
import { assumeType, deepMapValues } from '@dxos/util';
|
|
12
13
|
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
import * as Entity from './Entity';
|
|
15
|
+
import {
|
|
16
|
+
type AnyEchoObject,
|
|
17
|
+
type AnyProperties,
|
|
18
|
+
type InternalObjectProps,
|
|
19
|
+
MetaId,
|
|
20
|
+
type ObjectJSON,
|
|
21
|
+
type ObjectMeta,
|
|
22
|
+
ObjectVersionId,
|
|
23
|
+
VersionTypeId,
|
|
24
|
+
getDescription as getDescription$,
|
|
25
|
+
getLabel as getLabel$,
|
|
26
|
+
getMeta as getMeta$,
|
|
27
|
+
getObjectDXN,
|
|
28
|
+
getSchema as getSchema$,
|
|
29
|
+
getSchemaTypename,
|
|
30
|
+
getTypeAnnotation,
|
|
31
|
+
getTypeDXN as getTypeDXN$,
|
|
32
|
+
isDeleted as isDeleted$,
|
|
33
|
+
isInstanceOf,
|
|
34
|
+
makeObject,
|
|
35
|
+
objectFromJSON,
|
|
36
|
+
objectToJSON,
|
|
37
|
+
setDescription as setDescription$,
|
|
38
|
+
setLabel as setLabel$,
|
|
39
|
+
} from './internal';
|
|
40
|
+
import * as Ref from './Ref';
|
|
17
41
|
import * as Type from './Type';
|
|
18
42
|
|
|
19
43
|
/**
|
|
20
|
-
*
|
|
44
|
+
* Base type for all ECHO objects.
|
|
45
|
+
* @private
|
|
21
46
|
*/
|
|
22
|
-
interface BaseObj extends
|
|
47
|
+
interface BaseObj extends AnyEchoObject, Entity.OfKind<typeof Entity.Kind.Object> {}
|
|
23
48
|
|
|
24
49
|
/**
|
|
25
|
-
*
|
|
50
|
+
* Base type for all Obj objects.
|
|
26
51
|
*/
|
|
27
|
-
export
|
|
52
|
+
export interface Any extends BaseObj {}
|
|
53
|
+
|
|
54
|
+
export const Any = Schema.Struct({}).pipe(
|
|
55
|
+
Type.Obj({
|
|
56
|
+
typename: 'dxos.org/type/Any',
|
|
57
|
+
version: '0.1.0',
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
28
60
|
|
|
29
61
|
/**
|
|
30
|
-
*
|
|
31
|
-
* This type does not define any properties.
|
|
62
|
+
* Object type with specific properties.
|
|
32
63
|
*/
|
|
33
|
-
export
|
|
64
|
+
export type Obj<Props> = BaseObj & Props;
|
|
34
65
|
|
|
35
66
|
/**
|
|
36
67
|
* Object with arbitrary properties.
|
|
@@ -38,31 +69,25 @@ export interface Any extends BaseObj {}
|
|
|
38
69
|
* NOTE: Due to how typescript works, this type is not assignable to a specific schema type.
|
|
39
70
|
* In that case, use `Obj.instanceOf` to check if an object is of a specific type.
|
|
40
71
|
*/
|
|
41
|
-
export interface AnyProps extends BaseObj {
|
|
42
|
-
[key: string]: any;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const Any = Schema.Struct({}).pipe(
|
|
46
|
-
Type.Obj({
|
|
47
|
-
typename: 'dxos.org/types/Any',
|
|
48
|
-
version: '0.1.0',
|
|
49
|
-
}),
|
|
50
|
-
);
|
|
72
|
+
export interface AnyProps extends BaseObj, AnyProperties {}
|
|
51
73
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
export type MakeProps<T extends Type.Obj.Any> = NoInfer<Props<Schema.Schema.Type<T>>> & {
|
|
55
|
-
[Meta]?: Partial<EchoSchema.ObjectMeta>;
|
|
74
|
+
const defaultMeta: ObjectMeta = {
|
|
75
|
+
keys: [],
|
|
56
76
|
};
|
|
57
77
|
|
|
58
|
-
|
|
78
|
+
type Props<T = any> = {
|
|
79
|
+
id?: ObjectId;
|
|
80
|
+
[Meta]?: Partial<ObjectMeta>;
|
|
81
|
+
} & Type.Properties<T>;
|
|
59
82
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
83
|
+
// TODO(burdon): Should we allow the caller to set the id?
|
|
84
|
+
export type MakeProps<T extends Schema.Schema.AnyNoContext> = {
|
|
85
|
+
id?: ObjectId;
|
|
86
|
+
[Meta]?: Partial<ObjectMeta>;
|
|
87
|
+
} & NoInfer<Props<Schema.Schema.Type<T>>>;
|
|
63
88
|
|
|
64
89
|
/**
|
|
65
|
-
* Creates new object.
|
|
90
|
+
* Creates a new object of the given types.
|
|
66
91
|
* @param schema - Object schema.
|
|
67
92
|
* @param props - Object properties.
|
|
68
93
|
* @param meta - Object metadata (deprecated) -- pass with Obj.Meta.
|
|
@@ -74,40 +99,96 @@ const DEFAULT_META: EchoSchema.ObjectMeta = {
|
|
|
74
99
|
* const obj = Obj.make(Person, { [Obj.Meta]: { keys: [...] }, name: 'John' });
|
|
75
100
|
* ```
|
|
76
101
|
*/
|
|
77
|
-
export const make = <S extends
|
|
102
|
+
export const make = <S extends Schema.Schema.AnyNoContext>(
|
|
78
103
|
schema: S,
|
|
79
104
|
props: MakeProps<S>,
|
|
80
|
-
meta?: Partial<
|
|
81
|
-
):
|
|
82
|
-
assertArgument(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (props[EchoSchema.MetaId] != null) {
|
|
89
|
-
// Set default fields on meta on creation.
|
|
90
|
-
meta = { ...structuredClone(DEFAULT_META), ...props[EchoSchema.MetaId] };
|
|
91
|
-
delete props[EchoSchema.MetaId];
|
|
105
|
+
meta?: Partial<ObjectMeta>,
|
|
106
|
+
): Obj<Schema.Schema.Type<S>> => {
|
|
107
|
+
assertArgument(getTypeAnnotation(schema)?.kind === Entity.Kind.Object, 'schema', 'Expected an object schema');
|
|
108
|
+
|
|
109
|
+
// Set default fields on meta on creation.
|
|
110
|
+
if (props[MetaId] != null) {
|
|
111
|
+
meta = { ...structuredClone(defaultMeta), ...props[MetaId] };
|
|
112
|
+
delete props[MetaId];
|
|
92
113
|
}
|
|
93
114
|
|
|
115
|
+
// Filter undefined values.
|
|
94
116
|
const filterUndefined = Object.fromEntries(Object.entries(props).filter(([_, v]) => v !== undefined));
|
|
95
117
|
|
|
96
|
-
return
|
|
118
|
+
return makeObject<Schema.Schema.Type<S>>(schema, filterUndefined as any, {
|
|
119
|
+
...defaultMeta,
|
|
120
|
+
...meta,
|
|
121
|
+
});
|
|
97
122
|
};
|
|
98
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Determine if object is an ECHO object.
|
|
126
|
+
*/
|
|
99
127
|
export const isObject = (obj: unknown): obj is Any => {
|
|
100
|
-
assumeType<
|
|
101
|
-
return typeof obj === 'object' && obj !== null && obj[
|
|
128
|
+
assumeType<InternalObjectProps>(obj);
|
|
129
|
+
return typeof obj === 'object' && obj !== null && obj[Entity.KindId] === Entity.Kind.Object;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
//
|
|
133
|
+
// Snapshot
|
|
134
|
+
//
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Returns an immutable snapshot of an object.
|
|
138
|
+
*/
|
|
139
|
+
export const getSnapshot: <T extends Any>(obj: Obj<T>) => T = getSnapshot$;
|
|
140
|
+
|
|
141
|
+
export type CloneOptions = {
|
|
142
|
+
/**
|
|
143
|
+
* Retain the original object's ID.
|
|
144
|
+
* @default false
|
|
145
|
+
*/
|
|
146
|
+
retainId?: boolean;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Clones an object or relation.
|
|
151
|
+
* This does not clone referenced objects, only the properties in the object.
|
|
152
|
+
* @returns A new object with the same schema and properties.
|
|
153
|
+
*/
|
|
154
|
+
export const clone = <T extends Any>(obj: T, opts?: CloneOptions): T => {
|
|
155
|
+
const { id, ...data } = obj;
|
|
156
|
+
const schema = getSchema$(obj);
|
|
157
|
+
invariant(schema != null, 'Object should have a schema');
|
|
158
|
+
const props: any = deepMapValues(data, (value, recurse) => {
|
|
159
|
+
if (Ref.isRef(value)) {
|
|
160
|
+
return value;
|
|
161
|
+
}
|
|
162
|
+
return recurse(value);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
if (opts?.retainId) {
|
|
166
|
+
props.id = id;
|
|
167
|
+
}
|
|
168
|
+
const meta = getMeta(obj);
|
|
169
|
+
props[MetaId] = deepMapValues(meta, (value, recurse) => {
|
|
170
|
+
if (Ref.isRef(value)) {
|
|
171
|
+
return value;
|
|
172
|
+
}
|
|
173
|
+
return recurse(value);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
return make(schema as Type.Obj.Any, props);
|
|
102
177
|
};
|
|
103
178
|
|
|
179
|
+
//
|
|
180
|
+
// Type
|
|
181
|
+
//
|
|
182
|
+
|
|
183
|
+
// TODO(burdon): To discuss: prefer over ObjectId or Key.ObjectId or Type.ID?
|
|
184
|
+
export const ID = ObjectId;
|
|
185
|
+
export type ID = ObjectId;
|
|
186
|
+
|
|
104
187
|
/**
|
|
105
188
|
* Test if object or relation is an instance of a schema.
|
|
106
189
|
* @example
|
|
107
190
|
* ```ts
|
|
108
191
|
* const john = Obj.make(Person, { name: 'John' });
|
|
109
|
-
* const johnIsPerson = Obj.instanceOf(Person)(john);
|
|
110
|
-
*
|
|
111
192
|
* const isPerson = Obj.instanceOf(Person);
|
|
112
193
|
* if (isPerson(john)) {
|
|
113
194
|
* // john is Person
|
|
@@ -115,24 +196,20 @@ export const isObject = (obj: unknown): obj is Any => {
|
|
|
115
196
|
* ```
|
|
116
197
|
*/
|
|
117
198
|
export const instanceOf: {
|
|
118
|
-
<S extends Type.
|
|
119
|
-
<S extends Type.
|
|
120
|
-
} = ((
|
|
121
|
-
...args: [schema: Type.Relation.Any | Type.Obj.Any, value: unknown] | [schema: Type.Relation.Any | Type.Obj.Any]
|
|
122
|
-
) => {
|
|
199
|
+
<S extends Type.Entity.Any>(schema: S): (value: unknown) => value is Schema.Schema.Type<S>;
|
|
200
|
+
<S extends Type.Entity.Any>(schema: S, value: unknown): value is Schema.Schema.Type<S>;
|
|
201
|
+
} = ((...args: [schema: Type.Entity.Any, value: unknown] | [schema: Type.Entity.Any]) => {
|
|
123
202
|
if (args.length === 1) {
|
|
124
|
-
return (
|
|
203
|
+
return (entity: unknown) => isInstanceOf(args[0], entity);
|
|
125
204
|
}
|
|
126
205
|
|
|
127
|
-
return
|
|
206
|
+
return isInstanceOf(args[0], args[1]);
|
|
128
207
|
}) as any;
|
|
129
208
|
|
|
130
|
-
export const getSchema = EchoSchema.getSchema;
|
|
131
|
-
|
|
132
209
|
// TODO(dmaretskyi): Allow returning undefined.
|
|
133
|
-
export const getDXN = (
|
|
134
|
-
assertArgument(!Schema.isSchema(
|
|
135
|
-
const dxn =
|
|
210
|
+
export const getDXN = (entity: Entity.Unknown): DXN => {
|
|
211
|
+
assertArgument(!Schema.isSchema(entity), 'obj', 'Object should not be a schema.');
|
|
212
|
+
const dxn = getObjectDXN(entity);
|
|
136
213
|
invariant(dxn != null, 'Invalid object.');
|
|
137
214
|
return dxn;
|
|
138
215
|
};
|
|
@@ -141,26 +218,38 @@ export const getDXN = (obj: Any | Relation.Any): DXN => {
|
|
|
141
218
|
* @returns The DXN of the object's type.
|
|
142
219
|
* @example dxn:example.com/type/Person:1.0.0
|
|
143
220
|
*/
|
|
144
|
-
// TODO(burdon):
|
|
145
|
-
export const getTypeDXN =
|
|
221
|
+
// TODO(burdon): Must define and return type for expando.
|
|
222
|
+
export const getTypeDXN = getTypeDXN$;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Get the schema of the object.
|
|
226
|
+
*/
|
|
227
|
+
export const getSchema = getSchema$;
|
|
146
228
|
|
|
147
229
|
/**
|
|
148
230
|
* @returns The typename of the object's type.
|
|
149
231
|
* @example `example.com/type/Person`
|
|
150
232
|
*/
|
|
151
|
-
export const getTypename = (
|
|
152
|
-
const schema = getSchema(
|
|
233
|
+
export const getTypename = (entity: Entity.Unknown): string | undefined => {
|
|
234
|
+
const schema = getSchema$(entity);
|
|
153
235
|
if (schema == null) {
|
|
154
236
|
// Try to extract typename from DXN.
|
|
155
|
-
return
|
|
237
|
+
return getTypeDXN$(entity)?.asTypeDXN()?.type;
|
|
156
238
|
}
|
|
157
239
|
|
|
158
|
-
return
|
|
240
|
+
return getSchemaTypename(schema);
|
|
159
241
|
};
|
|
160
242
|
|
|
243
|
+
//
|
|
244
|
+
// Meta
|
|
245
|
+
//
|
|
246
|
+
|
|
247
|
+
export const Meta: unique symbol = MetaId as any;
|
|
248
|
+
|
|
249
|
+
// TODO(burdon): Narrow type.
|
|
161
250
|
// TODO(dmaretskyi): Allow returning undefined.
|
|
162
|
-
export const getMeta = (
|
|
163
|
-
const meta =
|
|
251
|
+
export const getMeta = (entity: AnyProperties): ObjectMeta => {
|
|
252
|
+
const meta = getMeta$(entity);
|
|
164
253
|
invariant(meta != null, 'Invalid object.');
|
|
165
254
|
return meta;
|
|
166
255
|
};
|
|
@@ -169,21 +258,21 @@ export const getMeta = (obj: Any | Relation.Any): EchoSchema.ObjectMeta => {
|
|
|
169
258
|
* @returns Foreign keys for the object from the specified source.
|
|
170
259
|
*/
|
|
171
260
|
export const getKeys: {
|
|
172
|
-
(
|
|
173
|
-
(source: string): (
|
|
174
|
-
} = Function.dual(2, (
|
|
175
|
-
const meta =
|
|
261
|
+
(entity: Entity.Unknown, source: string): ForeignKey[];
|
|
262
|
+
(source: string): (entity: Entity.Unknown) => ForeignKey[];
|
|
263
|
+
} = Function.dual(2, (entity: Entity.Unknown, source?: string): ForeignKey[] => {
|
|
264
|
+
const meta = getMeta(entity);
|
|
176
265
|
invariant(meta != null, 'Invalid object.');
|
|
177
266
|
return meta.keys.filter((key) => key.source === source);
|
|
178
267
|
});
|
|
179
268
|
|
|
180
269
|
/**
|
|
181
270
|
* Delete all keys from the object for the specified source.
|
|
182
|
-
* @param
|
|
271
|
+
* @param entity
|
|
183
272
|
* @param source
|
|
184
273
|
*/
|
|
185
|
-
export const deleteKeys = (
|
|
186
|
-
const meta =
|
|
274
|
+
export const deleteKeys = (entity: Entity.Unknown, source: string) => {
|
|
275
|
+
const meta = getMeta(entity);
|
|
187
276
|
for (let i = 0; i < meta.keys.length; i++) {
|
|
188
277
|
if (meta.keys[i].source === source) {
|
|
189
278
|
meta.keys.splice(i, 1);
|
|
@@ -192,59 +281,95 @@ export const deleteKeys = (obj: Any | Relation.Any, source: string) => {
|
|
|
192
281
|
}
|
|
193
282
|
};
|
|
194
283
|
|
|
284
|
+
export const addTag = (entity: Entity.Unknown, tag: string) => {
|
|
285
|
+
const meta = getMeta(entity);
|
|
286
|
+
meta.tags ??= [];
|
|
287
|
+
meta.tags.push(tag);
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
export const removeTag = (entity: Entity.Unknown, tag: string) => {
|
|
291
|
+
const meta = getMeta(entity);
|
|
292
|
+
if (!meta.tags) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
for (let i = 0; i < meta.tags.length; i++) {
|
|
296
|
+
if (meta.tags[i] === tag) {
|
|
297
|
+
meta.tags.splice(i, 1);
|
|
298
|
+
i--;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
|
|
195
303
|
// TODO(dmaretskyi): Default to `false`.
|
|
196
|
-
export const isDeleted = (
|
|
197
|
-
const deleted =
|
|
304
|
+
export const isDeleted = (entity: Entity.Unknown): boolean => {
|
|
305
|
+
const deleted = isDeleted$(entity);
|
|
198
306
|
invariant(typeof deleted === 'boolean', 'Invalid object.');
|
|
199
307
|
return deleted;
|
|
200
308
|
};
|
|
201
309
|
|
|
202
|
-
|
|
203
|
-
|
|
310
|
+
//
|
|
311
|
+
// Annotations
|
|
312
|
+
//
|
|
313
|
+
|
|
314
|
+
export const getLabel = (entity: Entity.Unknown): string | undefined => {
|
|
315
|
+
const schema = getSchema$(entity);
|
|
204
316
|
if (schema != null) {
|
|
205
|
-
return
|
|
317
|
+
return getLabel$(schema, entity);
|
|
206
318
|
}
|
|
207
319
|
};
|
|
208
320
|
|
|
209
|
-
export const setLabel = (
|
|
210
|
-
const schema = getSchema(
|
|
321
|
+
export const setLabel = (entity: Entity.Unknown, label: string) => {
|
|
322
|
+
const schema = getSchema$(entity);
|
|
211
323
|
if (schema != null) {
|
|
212
|
-
|
|
324
|
+
setLabel$(schema, entity, label);
|
|
213
325
|
}
|
|
214
326
|
};
|
|
215
327
|
|
|
216
|
-
export const getDescription = (
|
|
217
|
-
const schema = getSchema(
|
|
328
|
+
export const getDescription = (entity: Entity.Unknown): string | undefined => {
|
|
329
|
+
const schema = getSchema$(entity);
|
|
218
330
|
if (schema != null) {
|
|
219
|
-
return
|
|
331
|
+
return getDescription$(schema, entity);
|
|
220
332
|
}
|
|
221
333
|
};
|
|
222
334
|
|
|
223
|
-
export const setDescription = (
|
|
224
|
-
const schema = getSchema(
|
|
335
|
+
export const setDescription = (entity: Entity.Unknown, description: string) => {
|
|
336
|
+
const schema = getSchema$(entity);
|
|
225
337
|
if (schema != null) {
|
|
226
|
-
|
|
338
|
+
setDescription$(schema, entity, description);
|
|
227
339
|
}
|
|
228
340
|
};
|
|
229
341
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
meta.tags.push(tag);
|
|
234
|
-
};
|
|
342
|
+
//
|
|
343
|
+
// JSON
|
|
344
|
+
//
|
|
235
345
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
346
|
+
/**
|
|
347
|
+
* JSON representation of an object.
|
|
348
|
+
*/
|
|
349
|
+
export type JSON = ObjectJSON;
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Converts object to its JSON representation.
|
|
353
|
+
*
|
|
354
|
+
* The same algorithm is used when calling the standard `JSON.stringify(obj)` function.
|
|
355
|
+
*/
|
|
356
|
+
export const toJSON = (entity: Entity.Unknown): JSON => objectToJSON(entity);
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Creates an object from its json representation, performing schema validation.
|
|
360
|
+
* References and schemas will be resolvable if the `refResolver` is provided.
|
|
361
|
+
*
|
|
362
|
+
* The function must be async to support resolving the schema as well as the relation endpoints.
|
|
363
|
+
*
|
|
364
|
+
* @param options.refResolver - Resolver for references. Produces hydrated references that can be resolved.
|
|
365
|
+
* @param options.dxn - Override object DXN. Changes the result of `Obj.getDXN`.
|
|
366
|
+
*/
|
|
367
|
+
export const fromJSON: (json: unknown, options?: { refResolver?: Ref.Resolver; dxn?: DXN }) => Promise<Any> =
|
|
368
|
+
objectFromJSON as any;
|
|
369
|
+
|
|
370
|
+
//
|
|
371
|
+
// Sorting
|
|
372
|
+
//
|
|
248
373
|
|
|
249
374
|
const compare = (a?: string, b?: string) => {
|
|
250
375
|
if (a == null) {
|
|
@@ -258,12 +383,13 @@ const compare = (a?: string, b?: string) => {
|
|
|
258
383
|
return a.localeCompare(b);
|
|
259
384
|
};
|
|
260
385
|
|
|
261
|
-
export type Comparator = (a:
|
|
386
|
+
export type Comparator = (a: Entity.Unknown, b: Entity.Unknown) => number;
|
|
262
387
|
|
|
263
|
-
export const sortByLabel: Comparator = (a:
|
|
264
|
-
export const sortByTypename: Comparator = (a:
|
|
388
|
+
export const sortByLabel: Comparator = (a: Entity.Unknown, b: Entity.Unknown) => compare(getLabel(a), getLabel(b));
|
|
389
|
+
export const sortByTypename: Comparator = (a: Entity.Unknown, b: Entity.Unknown) =>
|
|
390
|
+
compare(getTypename(a), getTypename(b));
|
|
265
391
|
export const sort = (...comparators: Comparator[]): Comparator => {
|
|
266
|
-
return (a:
|
|
392
|
+
return (a: Entity.Unknown, b: Entity.Unknown) => {
|
|
267
393
|
for (const comparator of comparators) {
|
|
268
394
|
const result = comparator(a, b);
|
|
269
395
|
if (result !== 0) {
|
|
@@ -275,74 +401,14 @@ export const sort = (...comparators: Comparator[]): Comparator => {
|
|
|
275
401
|
};
|
|
276
402
|
};
|
|
277
403
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
export type JSON = EchoSchema.ObjectJSON;
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Converts object to its JSON representation.
|
|
285
|
-
*
|
|
286
|
-
* The same algorithm is used when calling the standard `JSON.stringify(obj)` function.
|
|
287
|
-
*/
|
|
288
|
-
// TODO(burdon): Base util type for Obj/Relation?
|
|
289
|
-
export const toJSON = (obj: Any | Relation.Any): JSON => EchoSchema.objectToJSON(obj);
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* Creates an object from its json representation, performing schema validation.
|
|
293
|
-
* References and schemas will be resolvable if the `refResolver` is provided.
|
|
294
|
-
*
|
|
295
|
-
* The function need to be async to support resolving the schema as well as the relation endpoints.
|
|
296
|
-
*
|
|
297
|
-
* @param options.refResolver - Resolver for references. Produces hydrated references that can be resolved.
|
|
298
|
-
* @param options.dxn - Override object DXN. Changes the result of `Obj.getDXN`.
|
|
299
|
-
*/
|
|
300
|
-
export const fromJSON: (json: unknown, options?: { refResolver?: Ref.Resolver; dxn?: DXN }) => Promise<Any> =
|
|
301
|
-
EchoSchema.objectFromJSON as any;
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Returns an immutable snapshot of an object.
|
|
305
|
-
*/
|
|
306
|
-
export const getSnapshot: <T extends Any>(obj: Obj<T>) => T = LiveObject.getSnapshot;
|
|
307
|
-
|
|
308
|
-
export type CloneOptions = {
|
|
309
|
-
/**
|
|
310
|
-
* Retain the original object's ID.
|
|
311
|
-
* @default false
|
|
312
|
-
*/
|
|
313
|
-
retainId?: boolean;
|
|
314
|
-
};
|
|
404
|
+
//
|
|
405
|
+
// Version
|
|
406
|
+
//
|
|
315
407
|
|
|
316
408
|
/**
|
|
317
|
-
*
|
|
318
|
-
* This does not clone referenced objects, only the properties in the object.
|
|
319
|
-
* @returns A new object with the same schema and properties.
|
|
409
|
+
* Unique symbol for version type identification.
|
|
320
410
|
*/
|
|
321
|
-
export
|
|
322
|
-
const { id, ...data } = obj;
|
|
323
|
-
const schema = getSchema(obj);
|
|
324
|
-
invariant(schema != null, 'Object should have a schema');
|
|
325
|
-
const props: any = deepMapValues(data, (value, recurse) => {
|
|
326
|
-
if (EchoSchema.Ref.isRef(value)) {
|
|
327
|
-
return value;
|
|
328
|
-
}
|
|
329
|
-
return recurse(value);
|
|
330
|
-
});
|
|
331
|
-
if (opts?.retainId) {
|
|
332
|
-
props.id = id;
|
|
333
|
-
}
|
|
334
|
-
const meta = getMeta(obj);
|
|
335
|
-
props[EchoSchema.MetaId] = deepMapValues(meta, (value, recurse) => {
|
|
336
|
-
if (EchoSchema.Ref.isRef(value)) {
|
|
337
|
-
return value;
|
|
338
|
-
}
|
|
339
|
-
return recurse(value);
|
|
340
|
-
});
|
|
341
|
-
return make(schema, props);
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
export const VersionTypeId = EchoSchema.VersionTypeId;
|
|
345
|
-
export type VersionType = typeof VersionTypeId;
|
|
411
|
+
export { VersionTypeId };
|
|
346
412
|
|
|
347
413
|
/**
|
|
348
414
|
* Represent object version.
|
|
@@ -371,18 +437,19 @@ const unversioned: Version = {
|
|
|
371
437
|
/**
|
|
372
438
|
* Checks that `obj` is a version object.
|
|
373
439
|
*/
|
|
374
|
-
export const isVersion = (
|
|
375
|
-
return
|
|
440
|
+
export const isVersion = (entity: unknown): entity is Version => {
|
|
441
|
+
return entity != null && typeof entity === 'object' && VersionTypeId in entity;
|
|
376
442
|
};
|
|
377
443
|
|
|
378
444
|
/**
|
|
379
445
|
* Returns the version of the object.
|
|
380
446
|
*/
|
|
381
|
-
export const version = (
|
|
382
|
-
const version = (
|
|
447
|
+
export const version = (entity: Entity.Unknown): Version => {
|
|
448
|
+
const version = (entity as any)[ObjectVersionId];
|
|
383
449
|
if (version === undefined) {
|
|
384
450
|
return unversioned;
|
|
385
451
|
}
|
|
452
|
+
|
|
386
453
|
return version;
|
|
387
454
|
};
|
|
388
455
|
|
package/src/Ref.ts
CHANGED
|
@@ -2,27 +2,27 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import { Ref as Ref$, RefArray, type RefResolver } from './internal';
|
|
6
6
|
import type * as Obj from './Obj';
|
|
7
7
|
|
|
8
|
-
export type Ref<T> =
|
|
9
|
-
export type Any =
|
|
8
|
+
export type Ref<T> = Ref$<T>;
|
|
9
|
+
export type Any = Ref$<Obj.Any>;
|
|
10
10
|
|
|
11
|
-
export const Array =
|
|
11
|
+
export const Array = RefArray;
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Extract reference target.
|
|
15
15
|
*/
|
|
16
|
-
export type Target<R extends Any> = R extends
|
|
16
|
+
export type Target<R extends Any> = R extends Ref$<infer T> ? T : never;
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Reference resolver.
|
|
20
20
|
*/
|
|
21
|
-
export type Resolver =
|
|
21
|
+
export type Resolver = RefResolver;
|
|
22
22
|
|
|
23
|
-
export const isRef: (value: unknown) => value is Any =
|
|
23
|
+
export const isRef: (value: unknown) => value is Any = Ref$.isRef;
|
|
24
24
|
|
|
25
|
-
export const make =
|
|
25
|
+
export const make = Ref$.make;
|
|
26
26
|
|
|
27
27
|
// TODO(dmaretskyi): Consider just allowing `make` to accept DXN.
|
|
28
|
-
export const fromDXN =
|
|
28
|
+
export const fromDXN = Ref$.fromDXN;
|