@coasys/ad4m 0.13.0-test-interpretation-1 → 0.13.0-test-model-layer
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/lib/index.cjs +2409 -625
- package/lib/index.js +2406 -626
- package/lib/src/Literal.js +8 -0
- package/lib/src/ai/AIClient.js +6 -3
- package/lib/src/apiClient.d.ts +40 -4
- package/lib/src/apiClient.js +149 -9
- package/lib/src/generated/api/RunInterpretationRequest.d.ts +16 -0
- package/lib/src/index.d.ts +3 -1
- package/lib/src/index.js +2 -0
- package/lib/src/model/Ad4mModel.d.ts +34 -16
- package/lib/src/model/Ad4mModel.js +302 -63
- package/lib/src/model/Ad4mModel.test.js +480 -735
- package/lib/src/model/ModelQueryBuilder.js +2 -2
- package/lib/src/model/decorators.d.ts +199 -12
- package/lib/src/model/decorators.js +88 -32
- package/lib/src/model/json-schema.js +7 -21
- package/lib/src/model/model-manifest.d.ts +41 -0
- package/lib/src/model/model-manifest.js +11 -0
- package/lib/src/model/query-common.d.ts +2 -2
- package/lib/src/model/query-sparql.d.ts +62 -0
- package/lib/src/model/query-sparql.js +78 -15
- package/lib/src/model/query-sparql.test.js +92 -10
- package/lib/src/model/query-utils.d.ts +7 -2
- package/lib/src/model/query-utils.js +21 -11
- package/lib/src/model/shacl-gen.js +77 -15
- package/lib/src/model/types.d.ts +32 -8
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +33 -33
- package/lib/src/perspectives/AutoProcessor.d.ts +198 -2
- package/lib/src/perspectives/FlowInstance.d.ts +187 -0
- package/lib/src/perspectives/FlowInstance.js +336 -0
- package/lib/src/perspectives/FlowModels.d.ts +94 -0
- package/lib/src/perspectives/FlowModels.js +182 -0
- package/lib/src/perspectives/InterpretationModels.d.ts +41 -3
- package/lib/src/perspectives/InterpretationModels.js +60 -26
- package/lib/src/perspectives/PerspectiveClient.d.ts +88 -7
- package/lib/src/perspectives/PerspectiveClient.js +138 -11
- package/lib/src/perspectives/PerspectiveProxy.d.ts +150 -57
- package/lib/src/perspectives/PerspectiveProxy.js +411 -287
- package/lib/src/perspectives/SparqlBindings.d.ts +24 -3
- package/lib/src/perspectives/SparqlBindings.js +57 -6
- package/lib/src/shacl/SHACLFlow.d.ts +273 -47
- package/lib/src/shacl/SHACLFlow.js +397 -88
- package/lib/src/shacl/SHACLShape.d.ts +19 -3
- package/lib/src/shacl/SHACLShape.js +30 -1
- package/lib/src/shacl/SHACLShape.test.js +82 -0
- package/lib/src/shacl/index.d.ts +1 -1
- package/lib/src/shacl/index.js +1 -1
- package/package.json +3 -3
- package/src/Literal.ts +7 -0
- package/src/ai/AIClient.ts +6 -3
- package/src/apiClient.test.ts +227 -0
- package/src/apiClient.ts +185 -9
- package/src/generated/api/RunInterpretationRequest.ts +19 -3
- package/src/index.ts +3 -1
- package/src/model/Ad4mModel.test.ts +729 -22
- package/src/model/Ad4mModel.ts +305 -66
- package/src/model/ModelQueryBuilder.ts +2 -2
- package/src/model/decorators.ts +278 -44
- package/src/model/json-schema.ts +10 -25
- package/src/model/query-common.ts +2 -2
- package/src/model/query-sparql.test.ts +114 -13
- package/src/model/query-sparql.ts +83 -14
- package/src/model/query-utils.ts +22 -12
- package/src/model/relation-filtering.test.ts +70 -20
- package/src/model/shacl-gen.ts +78 -13
- package/src/model/shacl-roundtrip.test.ts +134 -2
- package/src/model/types.ts +30 -8
- package/src/perspectives/AutoProcessor.ts +204 -2
- package/src/perspectives/FlowInstance.ts +369 -0
- package/src/perspectives/FlowModels.ts +157 -0
- package/src/perspectives/InterpretationModels.ts +91 -21
- package/src/perspectives/PerspectiveClient.test.ts +67 -6
- package/src/perspectives/PerspectiveClient.ts +190 -14
- package/src/perspectives/PerspectiveProxy.test.ts +406 -0
- package/src/perspectives/PerspectiveProxy.ts +494 -308
- package/src/perspectives/SparqlBindings.test.ts +134 -0
- package/src/perspectives/SparqlBindings.ts +57 -6
- package/src/perspectives/shacl-rpc.test.ts +258 -0
- package/src/shacl/SHACLFlow.test.ts +762 -35
- package/src/shacl/SHACLFlow.ts +680 -112
- package/src/shacl/SHACLShape.test.ts +30 -4
- package/src/shacl/SHACLShape.ts +59 -6
- package/src/shacl/index.ts +1 -1
- package/lib/src/generated/rest/AIModelLoadingStatus.d.ts +0 -7
- package/lib/src/generated/rest/AIModelLoadingStatus.js +0 -2
- package/lib/src/generated/rest/AddAgentInfosRequest.d.ts +0 -3
- package/lib/src/generated/rest/AddAgentInfosRequest.js +0 -2
- package/lib/src/generated/rest/AddLinkExpressionRequest.d.ts +0 -6
- package/lib/src/generated/rest/AddLinkExpressionRequest.js +0 -1
- package/lib/src/generated/rest/AddLinkRequest.d.ts +0 -6
- package/lib/src/generated/rest/AddLinkRequest.js +0 -1
- package/lib/src/generated/rest/AddLinksBulkRequest.d.ts +0 -6
- package/lib/src/generated/rest/AddLinksBulkRequest.js +0 -1
- package/lib/src/generated/rest/AddSdnaRequest.d.ts +0 -6
- package/lib/src/generated/rest/AddSdnaRequest.js +0 -2
- package/lib/src/generated/rest/ApplyTemplateRequest.d.ts +0 -4
- package/lib/src/generated/rest/ApplyTemplateRequest.js +0 -2
- package/lib/src/generated/rest/AuthInfoInput.d.ts +0 -10
- package/lib/src/generated/rest/AuthInfoInput.js +0 -1
- package/lib/src/generated/rest/BroadcastRequest.d.ts +0 -6
- package/lib/src/generated/rest/BroadcastRequest.js +0 -1
- package/lib/src/generated/rest/CapabilityInput.d.ts +0 -5
- package/lib/src/generated/rest/CapabilityInput.js +0 -1
- package/lib/src/generated/rest/CommitBatchRequest.d.ts +0 -3
- package/lib/src/generated/rest/CommitBatchRequest.js +0 -2
- package/lib/src/generated/rest/ComputeLogEntry.d.ts +0 -9
- package/lib/src/generated/rest/ComputeLogEntry.js +0 -2
- package/lib/src/generated/rest/CreateExpressionRequest.d.ts +0 -4
- package/lib/src/generated/rest/CreateExpressionRequest.js +0 -2
- package/lib/src/generated/rest/CreatePerspectiveRequest.d.ts +0 -3
- package/lib/src/generated/rest/CreatePerspectiveRequest.js +0 -2
- package/lib/src/generated/rest/CreateSubjectRequest.d.ts +0 -6
- package/lib/src/generated/rest/CreateSubjectRequest.js +0 -2
- package/lib/src/generated/rest/CreateUserRequest.d.ts +0 -4
- package/lib/src/generated/rest/CreateUserRequest.js +0 -2
- package/lib/src/generated/rest/CreateUserResponse.d.ts +0 -5
- package/lib/src/generated/rest/CreateUserResponse.js +0 -2
- package/lib/src/generated/rest/DecoratedExpressionProof.d.ts +0 -6
- package/lib/src/generated/rest/DecoratedExpressionProof.js +0 -2
- package/lib/src/generated/rest/DecoratedLinkExpression.d.ts +0 -10
- package/lib/src/generated/rest/DecoratedLinkExpression.js +0 -1
- package/lib/src/generated/rest/DisposeQueryRequest.d.ts +0 -3
- package/lib/src/generated/rest/DisposeQueryRequest.js +0 -2
- package/lib/src/generated/rest/EmailTestRequest.d.ts +0 -6
- package/lib/src/generated/rest/EmailTestRequest.js +0 -2
- package/lib/src/generated/rest/EmbedRequest.d.ts +0 -4
- package/lib/src/generated/rest/EmbedRequest.js +0 -2
- package/lib/src/generated/rest/EntanglementProofInput.d.ts +0 -6
- package/lib/src/generated/rest/EntanglementProofInput.js +0 -2
- package/lib/src/generated/rest/EntanglementProofPreflightRequest.d.ts +0 -4
- package/lib/src/generated/rest/EntanglementProofPreflightRequest.js +0 -2
- package/lib/src/generated/rest/ExecuteCommandsRequest.d.ts +0 -4
- package/lib/src/generated/rest/ExecuteCommandsRequest.js +0 -2
- package/lib/src/generated/rest/ExportRequest.d.ts +0 -5
- package/lib/src/generated/rest/ExportRequest.js +0 -2
- package/lib/src/generated/rest/ExpressionManyRequest.d.ts +0 -3
- package/lib/src/generated/rest/ExpressionManyRequest.js +0 -2
- package/lib/src/generated/rest/ExpressionProof.d.ts +0 -4
- package/lib/src/generated/rest/ExpressionProof.js +0 -2
- package/lib/src/generated/rest/ExpressionProofInput.d.ts +0 -6
- package/lib/src/generated/rest/ExpressionProofInput.js +0 -2
- package/lib/src/generated/rest/FriendSendMessageRequest.d.ts +0 -3
- package/lib/src/generated/rest/FriendSendMessageRequest.js +0 -2
- package/lib/src/generated/rest/FriendsListRequest.d.ts +0 -3
- package/lib/src/generated/rest/FriendsListRequest.js +0 -2
- package/lib/src/generated/rest/GenerateAgentRequest.d.ts +0 -3
- package/lib/src/generated/rest/GenerateAgentRequest.js +0 -2
- package/lib/src/generated/rest/GenerateJwtRequest.d.ts +0 -4
- package/lib/src/generated/rest/GenerateJwtRequest.js +0 -2
- package/lib/src/generated/rest/GetSubjectDataRequest.d.ts +0 -4
- package/lib/src/generated/rest/GetSubjectDataRequest.js +0 -2
- package/lib/src/generated/rest/HostingInfoResponse.d.ts +0 -5
- package/lib/src/generated/rest/HostingInfoResponse.js +0 -2
- package/lib/src/generated/rest/HostingWalletResponse.d.ts +0 -4
- package/lib/src/generated/rest/HostingWalletResponse.js +0 -2
- package/lib/src/generated/rest/ImportAgentRequest.d.ts +0 -6
- package/lib/src/generated/rest/ImportAgentRequest.js +0 -2
- package/lib/src/generated/rest/ImportRequest.d.ts +0 -4
- package/lib/src/generated/rest/ImportRequest.js +0 -2
- package/lib/src/generated/rest/JoinNeighbourhoodRequest.d.ts +0 -3
- package/lib/src/generated/rest/JoinNeighbourhoodRequest.js +0 -2
- package/lib/src/generated/rest/KeepAliveQueryRequest.d.ts +0 -3
- package/lib/src/generated/rest/KeepAliveQueryRequest.js +0 -2
- package/lib/src/generated/rest/LanguageMetaInput.d.ts +0 -6
- package/lib/src/generated/rest/LanguageMetaInput.js +0 -2
- package/lib/src/generated/rest/Link.d.ts +0 -5
- package/lib/src/generated/rest/Link.js +0 -2
- package/lib/src/generated/rest/LinkExpression.d.ts +0 -10
- package/lib/src/generated/rest/LinkExpression.js +0 -1
- package/lib/src/generated/rest/LinkExpressionInput.d.ts +0 -10
- package/lib/src/generated/rest/LinkExpressionInput.js +0 -1
- package/lib/src/generated/rest/LinkInput.d.ts +0 -5
- package/lib/src/generated/rest/LinkInput.js +0 -2
- package/lib/src/generated/rest/LinkLanguageTemplatesRequest.d.ts +0 -3
- package/lib/src/generated/rest/LinkLanguageTemplatesRequest.js +0 -2
- package/lib/src/generated/rest/LinkMutationRequest.d.ts +0 -12
- package/lib/src/generated/rest/LinkMutationRequest.js +0 -1
- package/lib/src/generated/rest/LinkMutationResponse.d.ts +0 -9
- package/lib/src/generated/rest/LinkMutationResponse.js +0 -1
- package/lib/src/generated/rest/LinkMutations.d.ts +0 -6
- package/lib/src/generated/rest/LinkMutations.js +0 -1
- package/lib/src/generated/rest/LinkMutationsRequest.d.ts +0 -5
- package/lib/src/generated/rest/LinkMutationsRequest.js +0 -1
- package/lib/src/generated/rest/LinkStatus.d.ts +0 -1
- package/lib/src/generated/rest/LinkStatus.js +0 -2
- package/lib/src/generated/rest/LinkUpdateInput.d.ts +0 -5
- package/lib/src/generated/rest/LinkUpdateInput.js +0 -1
- package/lib/src/generated/rest/LockAgentRequest.d.ts +0 -3
- package/lib/src/generated/rest/LockAgentRequest.js +0 -2
- package/lib/src/generated/rest/LoginUserRequest.d.ts +0 -5
- package/lib/src/generated/rest/LoginUserRequest.js +0 -2
- package/lib/src/generated/rest/ModelType.d.ts +0 -1
- package/lib/src/generated/rest/ModelType.js +0 -2
- package/lib/src/generated/rest/NotificationGrantRequest.d.ts +0 -3
- package/lib/src/generated/rest/NotificationGrantRequest.js +0 -2
- package/lib/src/generated/rest/NotificationInput.d.ts +0 -10
- package/lib/src/generated/rest/NotificationInput.js +0 -2
- package/lib/src/generated/rest/OpenLinkRequest.d.ts +0 -3
- package/lib/src/generated/rest/OpenLinkRequest.js +0 -2
- package/lib/src/generated/rest/PermitCapabilityRequest.d.ts +0 -3
- package/lib/src/generated/rest/PermitCapabilityRequest.js +0 -2
- package/lib/src/generated/rest/PerspectiveInput.d.ts +0 -4
- package/lib/src/generated/rest/PerspectiveInput.js +0 -1
- package/lib/src/generated/rest/PromptRequest.d.ts +0 -4
- package/lib/src/generated/rest/PromptRequest.js +0 -2
- package/lib/src/generated/rest/PublicPerspectiveInput.d.ts +0 -4
- package/lib/src/generated/rest/PublicPerspectiveInput.js +0 -1
- package/lib/src/generated/rest/PublishLanguageRequest.d.ts +0 -5
- package/lib/src/generated/rest/PublishLanguageRequest.js +0 -1
- package/lib/src/generated/rest/PublishNeighbourhoodRequest.d.ts +0 -6
- package/lib/src/generated/rest/PublishNeighbourhoodRequest.js +0 -1
- package/lib/src/generated/rest/QueryRequest.d.ts +0 -7
- package/lib/src/generated/rest/QueryRequest.js +0 -2
- package/lib/src/generated/rest/RemoveLinkRequest.d.ts +0 -5
- package/lib/src/generated/rest/RemoveLinkRequest.js +0 -1
- package/lib/src/generated/rest/RemoveLinksBulkRequest.d.ts +0 -5
- package/lib/src/generated/rest/RemoveLinksBulkRequest.js +0 -1
- package/lib/src/generated/rest/RequestCapabilityRequest.d.ts +0 -4
- package/lib/src/generated/rest/RequestCapabilityRequest.js +0 -1
- package/lib/src/generated/rest/RequestPaymentRequest.d.ts +0 -3
- package/lib/src/generated/rest/RequestPaymentRequest.js +0 -2
- package/lib/src/generated/rest/RequestVerificationRequest.d.ts +0 -4
- package/lib/src/generated/rest/RequestVerificationRequest.js +0 -2
- package/lib/src/generated/rest/ResourceInput.d.ts +0 -4
- package/lib/src/generated/rest/ResourceInput.js +0 -2
- package/lib/src/generated/rest/SetDefaultModelRequest.d.ts +0 -4
- package/lib/src/generated/rest/SetDefaultModelRequest.js +0 -1
- package/lib/src/generated/rest/SetFreeHostingEnabledRequest.d.ts +0 -3
- package/lib/src/generated/rest/SetFreeHostingEnabledRequest.js +0 -2
- package/lib/src/generated/rest/SetHotWalletAddressRequest.d.ts +0 -3
- package/lib/src/generated/rest/SetHotWalletAddressRequest.js +0 -2
- package/lib/src/generated/rest/SetMultiUserRequest.d.ts +0 -3
- package/lib/src/generated/rest/SetMultiUserRequest.js +0 -2
- package/lib/src/generated/rest/SetOnlineStatusRequest.d.ts +0 -5
- package/lib/src/generated/rest/SetOnlineStatusRequest.js +0 -1
- package/lib/src/generated/rest/SetStatusRequest.d.ts +0 -3
- package/lib/src/generated/rest/SetStatusRequest.js +0 -2
- package/lib/src/generated/rest/SetUserFreeAccessRequest.d.ts +0 -4
- package/lib/src/generated/rest/SetUserFreeAccessRequest.js +0 -2
- package/lib/src/generated/rest/SignMessageRequest.d.ts +0 -3
- package/lib/src/generated/rest/SignMessageRequest.js +0 -2
- package/lib/src/generated/rest/SignalRequest.d.ts +0 -6
- package/lib/src/generated/rest/SignalRequest.js +0 -1
- package/lib/src/generated/rest/SubscribeQueryRequest.d.ts +0 -3
- package/lib/src/generated/rest/SubscribeQueryRequest.js +0 -2
- package/lib/src/generated/rest/SubscribeQueryResponse.d.ts +0 -4
- package/lib/src/generated/rest/SubscribeQueryResponse.js +0 -2
- package/lib/src/generated/rest/TrustedAgentsRequest.d.ts +0 -3
- package/lib/src/generated/rest/TrustedAgentsRequest.js +0 -2
- package/lib/src/generated/rest/UnlockAgentRequest.d.ts +0 -4
- package/lib/src/generated/rest/UnlockAgentRequest.js +0 -2
- package/lib/src/generated/rest/UpdateLinkRequest.d.ts +0 -7
- package/lib/src/generated/rest/UpdateLinkRequest.js +0 -1
- package/lib/src/generated/rest/UpdatePerspectiveRequest.d.ts +0 -3
- package/lib/src/generated/rest/UpdatePerspectiveRequest.js +0 -2
- package/lib/src/generated/rest/UpdateProfileRequest.d.ts +0 -5
- package/lib/src/generated/rest/UpdateProfileRequest.js +0 -1
- package/lib/src/generated/rest/UserStatistics.d.ts +0 -9
- package/lib/src/generated/rest/UserStatistics.js +0 -2
- package/lib/src/generated/rest/VerificationRequestResult.d.ts +0 -6
- package/lib/src/generated/rest/VerificationRequestResult.js +0 -2
- package/lib/src/generated/rest/VerifyEmailRequest.d.ts +0 -6
- package/lib/src/generated/rest/VerifyEmailRequest.js +0 -2
- package/lib/src/generated/rest/VerifySignatureRequest.d.ts +0 -5
- package/lib/src/generated/rest/VerifySignatureRequest.js +0 -2
- package/lib/src/generated/rest/WriteSettingsRequest.d.ts +0 -3
- package/lib/src/generated/rest/WriteSettingsRequest.js +0 -2
- package/lib/src/generated/rest/index.d.ts +0 -73
- package/lib/src/generated/rest/index.js +0 -8
- package/lib/src/generated/rest/routes.d.ts +0 -587
- package/lib/src/generated/rest/routes.js +0 -3
- package/lib/src/model/query-surreal.d.ts +0 -44
- package/lib/src/model/query-surreal.js +0 -249
- package/lib/src/model/surreal-utils.d.ts +0 -42
- package/lib/src/model/surreal-utils.js +0 -128
- package/lib/src/perspectives/PerspectiveProxy.test.d.ts +0 -1
- package/lib/src/perspectives/PerspectiveProxy.test.js +0 -34
- package/lib/src/restClient.d.ts +0 -52
- package/lib/src/restClient.js +0 -227
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { Literal } from "../Literal";
|
|
2
1
|
import { Link } from "../links/Links";
|
|
3
2
|
import { LinkQuery } from "../perspectives/LinkQuery";
|
|
4
3
|
import { makeRandomId } from "./util";
|
|
5
4
|
import { getPropertiesMetadata, getRelationsMetadata, setPropertyRegistryEntry, setRelationRegistryEntry, Model } from "./decorators";
|
|
6
5
|
import { resolveParentPredicate } from "./query-common";
|
|
7
6
|
import { isArrayType, determinePredicate, determineNamespace, buildModelFromJSONSchema } from "./json-schema";
|
|
8
|
-
import { buildSPARQLQuery } from "./query-sparql";
|
|
7
|
+
import { buildSPARQLQuery, valueToLiteralIri, effectiveLiteralStorage } from "./query-sparql";
|
|
9
8
|
import { ModelQueryBuilder } from "./ModelQueryBuilder";
|
|
10
9
|
import { normalizeValue, } from "./hydration";
|
|
11
10
|
// ---------------------------------------------------------------------------
|
|
@@ -16,6 +15,110 @@ import { normalizeValue, } from "./hydration";
|
|
|
16
15
|
* Rust endpoint. Recursively converts included relation values (which come
|
|
17
16
|
* back as plain JSON objects) into proper model class instances.
|
|
18
17
|
*/
|
|
18
|
+
/**
|
|
19
|
+
* Key under which the executor returns a polymorphically-hydrated instance's
|
|
20
|
+
* concrete class.
|
|
21
|
+
*
|
|
22
|
+
* Half of a wire contract: the writer is `SUBJECT_CLASS_KEY` in
|
|
23
|
+
* `rust-executor/src/perspectives/model_query/relations.rs`, and the two are
|
|
24
|
+
* separate literals in separate languages. Renaming one alone does not fail to
|
|
25
|
+
* compile — it degrades to every instance staying plain JSON, which looks
|
|
26
|
+
* identical to a relation that declared no classes to build.
|
|
27
|
+
* `tests/js/tests/model/model-polymorphic.test.ts` drives both ends against one
|
|
28
|
+
* executor, so the drift fails a test rather than a user's query.
|
|
29
|
+
*
|
|
30
|
+
* The executor also returns `__subjectClasses`, the whole set this one names the
|
|
31
|
+
* head of. Nothing here reads it — it rides onto the instance with every other
|
|
32
|
+
* JSON key, for a caller that wants to know a choice was made.
|
|
33
|
+
*/
|
|
34
|
+
const SUBJECT_CLASS_KEY = '__subjectClass';
|
|
35
|
+
/**
|
|
36
|
+
* Turn on `polymorphic` for every relation that declares it, at every depth of
|
|
37
|
+
* an include map.
|
|
38
|
+
*
|
|
39
|
+
* A relation being heterogeneous is a fact about the data, not about one query,
|
|
40
|
+
* so the declaration lives on the model and the call site writes
|
|
41
|
+
* `include: { children: true }`. That has to hold just as much one level down:
|
|
42
|
+
* in `include: { posts: { include: { children: true } } }`, `children` is a
|
|
43
|
+
* relation on `Post`, so the walk carries the class it is reading against down
|
|
44
|
+
* with it. Without that, a nested include of an untyped polymorphic relation
|
|
45
|
+
* reaches the executor with no shape to resolve and fails the query outright.
|
|
46
|
+
*
|
|
47
|
+
* An explicit `polymorphic: false` at any level still wins — `undefined` is the
|
|
48
|
+
* only state the default fills in.
|
|
49
|
+
*
|
|
50
|
+
* The walk stops descending *through* a polymorphic relation, because there is
|
|
51
|
+
* nothing to descend into: its targets are of several classes by definition, so
|
|
52
|
+
* no single class's metadata could say what a deeper include means. The
|
|
53
|
+
* executor resolves those nested includes against each concrete class instead.
|
|
54
|
+
*/
|
|
55
|
+
function applyPolymorphicIncludeDefaults(includes, ctor) {
|
|
56
|
+
const relMeta = getRelationsMetadata(ctor);
|
|
57
|
+
for (const [relName, val] of Object.entries(includes)) {
|
|
58
|
+
// `$`-prefixed keys are projections, not relations.
|
|
59
|
+
if (relName.startsWith('$'))
|
|
60
|
+
continue;
|
|
61
|
+
const meta = relMeta[relName];
|
|
62
|
+
if (!meta)
|
|
63
|
+
continue;
|
|
64
|
+
let subQuery = val;
|
|
65
|
+
if (meta.polymorphic) {
|
|
66
|
+
if (val === true) {
|
|
67
|
+
subQuery = { polymorphic: true };
|
|
68
|
+
includes[relName] = subQuery;
|
|
69
|
+
}
|
|
70
|
+
else if (typeof val === 'object' && val !== null && subQuery.polymorphic === undefined) {
|
|
71
|
+
subQuery.polymorphic = true;
|
|
72
|
+
}
|
|
73
|
+
// The classes this call site can build are also the classes it wants its
|
|
74
|
+
// targets read as, so the declaration answers both questions and the call
|
|
75
|
+
// site repeats neither. Sent as part of the query rather than applied to
|
|
76
|
+
// the results: a preference in the request is reproducible by anyone who
|
|
77
|
+
// sends it, where one applied afterwards would make the same data read
|
|
78
|
+
// differently depending on which classes the caller happened to import.
|
|
79
|
+
//
|
|
80
|
+
// It ranks and does not narrow — a target matching none of them still
|
|
81
|
+
// arrives — so declaring what you can construct never costs you a member
|
|
82
|
+
// of a relation that is heterogeneous by definition.
|
|
83
|
+
if (typeof subQuery === 'object' &&
|
|
84
|
+
subQuery !== null &&
|
|
85
|
+
subQuery.polymorphic &&
|
|
86
|
+
subQuery.preferClasses === undefined &&
|
|
87
|
+
meta.instantiateAs) {
|
|
88
|
+
const names = (meta.instantiateAs() ?? [])
|
|
89
|
+
.map((cls) => cls?.className)
|
|
90
|
+
.filter((name) => typeof name === 'string');
|
|
91
|
+
if (names.length)
|
|
92
|
+
subQuery.preferClasses = names;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (typeof subQuery !== 'object' || subQuery === null)
|
|
96
|
+
continue;
|
|
97
|
+
const nested = subQuery.include;
|
|
98
|
+
if (!nested || subQuery.polymorphic)
|
|
99
|
+
continue;
|
|
100
|
+
// The thunk is only evaluated here, at query time, so a self-referential or
|
|
101
|
+
// circularly-imported target that is not yet defined costs the nested
|
|
102
|
+
// default rather than the whole query.
|
|
103
|
+
let TargetClass;
|
|
104
|
+
try {
|
|
105
|
+
TargetClass = meta.target?.();
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
// A class not yet initialised throws `ReferenceError` from the temporal
|
|
109
|
+
// dead zone, which is the expected cost of an import cycle and not worth
|
|
110
|
+
// saying anything about. Anything else — a thunk closing over nothing,
|
|
111
|
+
// typically — is a broken declaration that would otherwise cost the nested
|
|
112
|
+
// default in silence, so it gets said out loud.
|
|
113
|
+
const circularImport = e instanceof ReferenceError;
|
|
114
|
+
const say = circularImport ? console.debug : console.warn;
|
|
115
|
+
say(`prepareModelQueryParams: target class unavailable for include '${relName}':`, e);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (TargetClass)
|
|
119
|
+
applyPolymorphicIncludeDefaults(nested, TargetClass);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
19
122
|
function jsonToModelInstance(ModelClass, perspective, json, include, properties) {
|
|
20
123
|
const instance = new ModelClass(perspective, json.id || json.baseExpression);
|
|
21
124
|
// When a properties projection is active, remove own properties set by the
|
|
@@ -62,9 +165,62 @@ function jsonToModelInstance(ModelClass, perspective, json, include, properties)
|
|
|
62
165
|
if (!includeVal)
|
|
63
166
|
continue;
|
|
64
167
|
const meta = relMeta[relName];
|
|
65
|
-
|
|
168
|
+
// A polymorphic relation may legitimately declare no target at all —
|
|
169
|
+
// that is the case it exists for — so it must not be gated on one.
|
|
170
|
+
if (!meta)
|
|
66
171
|
continue;
|
|
67
|
-
|
|
172
|
+
if (!meta.target && !meta.polymorphic)
|
|
173
|
+
continue;
|
|
174
|
+
const TargetClass = meta.target?.();
|
|
175
|
+
// For a polymorphic relation the executor sends each child's concrete
|
|
176
|
+
// class back on the instance, because it had to know it in order to
|
|
177
|
+
// hydrate against the right shape at all. What it cannot send is the
|
|
178
|
+
// constructor, so `instantiateAs` names the classes this side can build
|
|
179
|
+
// and the name → class map is derived from them: `@Model` records each
|
|
180
|
+
// class's name on the class itself, so listing the classes and listing
|
|
181
|
+
// their names would be the same list written twice, free to disagree.
|
|
182
|
+
//
|
|
183
|
+
// Built on first use rather than at decoration time, because the thunk
|
|
184
|
+
// exists to be called late: a class defined further round a circular
|
|
185
|
+
// import graph is not there yet when the decorator runs.
|
|
186
|
+
let byClassName;
|
|
187
|
+
// May resolve to nothing: a polymorphic relation is allowed to declare no
|
|
188
|
+
// target at all, and the list may not name the class that arrived — it
|
|
189
|
+
// says what this call site can construct, not what the relation may hold.
|
|
190
|
+
// The item then stays plain JSON — correct data, carrying its class name
|
|
191
|
+
// for a caller that wants to dispatch on it — rather than being forced
|
|
192
|
+
// through a constructor that does not exist.
|
|
193
|
+
const resolveChildClass = (item) => {
|
|
194
|
+
if (!meta.polymorphic)
|
|
195
|
+
return TargetClass;
|
|
196
|
+
const concrete = item?.[SUBJECT_CLASS_KEY];
|
|
197
|
+
// No concrete class means this was not read polymorphically after all —
|
|
198
|
+
// an explicit `polymorphic: false` at the call site — so the relation's
|
|
199
|
+
// own declared target is the right shape.
|
|
200
|
+
if (typeof concrete !== 'string')
|
|
201
|
+
return TargetClass;
|
|
202
|
+
if (meta.instantiateAs) {
|
|
203
|
+
if (!byClassName) {
|
|
204
|
+
byClassName = {};
|
|
205
|
+
for (const cls of meta.instantiateAs() ?? []) {
|
|
206
|
+
const name = cls?.className;
|
|
207
|
+
if (typeof name === 'string')
|
|
208
|
+
byClassName[name] = cls;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (byClassName[concrete])
|
|
212
|
+
return byClassName[concrete];
|
|
213
|
+
}
|
|
214
|
+
// The declared target is not a fallback, and having named no classes to
|
|
215
|
+
// build does not make it one. Where a relation both declares a target
|
|
216
|
+
// and reads polymorphically, a child of some third class was hydrated
|
|
217
|
+
// against *its own* shape, so constructing the declared class over that
|
|
218
|
+
// JSON produces an instance that answers `instanceof` for a class it is
|
|
219
|
+
// not, carrying fields that class never declared — the exact
|
|
220
|
+
// mislabelling polymorphic reads exist to prevent. It fits only when it
|
|
221
|
+
// is the class the executor named.
|
|
222
|
+
return TargetClass?.className === concrete ? TargetClass : undefined;
|
|
223
|
+
};
|
|
68
224
|
const nestedInclude = typeof includeVal === 'object' && includeVal !== null
|
|
69
225
|
? includeVal.include
|
|
70
226
|
: undefined;
|
|
@@ -74,14 +230,18 @@ function jsonToModelInstance(ModelClass, perspective, json, include, properties)
|
|
|
74
230
|
const raw = instance[relName];
|
|
75
231
|
if (Array.isArray(raw)) {
|
|
76
232
|
instance[relName] = raw.map((item) => {
|
|
77
|
-
|
|
78
|
-
|
|
233
|
+
const ChildClass = resolveChildClass(item);
|
|
234
|
+
if (ChildClass && typeof item === 'object' && item !== null && item.id) {
|
|
235
|
+
return jsonToModelInstance(ChildClass, perspective, item, nestedInclude, nestedProperties);
|
|
79
236
|
}
|
|
80
237
|
return item;
|
|
81
238
|
});
|
|
82
239
|
}
|
|
83
240
|
else if (typeof raw === 'object' && raw !== null && raw.id) {
|
|
84
|
-
|
|
241
|
+
const ChildClass = resolveChildClass(raw);
|
|
242
|
+
if (ChildClass) {
|
|
243
|
+
instance[relName] = jsonToModelInstance(ChildClass, perspective, raw, nestedInclude, nestedProperties);
|
|
244
|
+
}
|
|
85
245
|
}
|
|
86
246
|
}
|
|
87
247
|
}
|
|
@@ -108,12 +268,28 @@ function jsonToModelInstance(ModelClass, perspective, json, include, properties)
|
|
|
108
268
|
* // Define a recipe model
|
|
109
269
|
* @Model({ name: "Recipe" })
|
|
110
270
|
* class Recipe extends Ad4mModel {
|
|
111
|
-
* //
|
|
271
|
+
* // Plain property — no `resolveLanguage` → deterministic typed literal
|
|
272
|
+
* // storage (fast POS-index path). This is the perf default.
|
|
273
|
+
* @Property({ through: "recipe://name" })
|
|
274
|
+
* name: string = "";
|
|
275
|
+
*
|
|
276
|
+
* // Property that needs per-value provenance (e.g. a signed message body):
|
|
277
|
+
* // `resolveLanguage: "literal"` routes values through expression_create on
|
|
278
|
+
* // the built-in literal language, producing a signed envelope URI
|
|
279
|
+
* // (author/timestamp/proof) instead of a bare typed literal.
|
|
112
280
|
* @Property({
|
|
113
|
-
* through: "recipe://
|
|
281
|
+
* through: "recipe://signed_note",
|
|
114
282
|
* resolveLanguage: "literal"
|
|
115
283
|
* })
|
|
116
|
-
*
|
|
284
|
+
* signedNote: string = "";
|
|
285
|
+
*
|
|
286
|
+
* // Property resolved through a custom language: values are routed through
|
|
287
|
+
* // expression_create on that language (signed expression URIs).
|
|
288
|
+
* @Optional({
|
|
289
|
+
* through: "recipe://photo",
|
|
290
|
+
* resolveLanguage: "QmFileStorageLanguageAddress..."
|
|
291
|
+
* })
|
|
292
|
+
* photo: string = "";
|
|
117
293
|
*
|
|
118
294
|
* // Optional property with custom initial value
|
|
119
295
|
* @Optional({
|
|
@@ -253,7 +429,7 @@ export class Ad4mModel {
|
|
|
253
429
|
* ```typescript
|
|
254
430
|
* @Model({ name: "Recipe" })
|
|
255
431
|
* class Recipe extends Ad4mModel {
|
|
256
|
-
* @Property({ through: "recipe://name"
|
|
432
|
+
* @Property({ through: "recipe://name" })
|
|
257
433
|
* name: string = "";
|
|
258
434
|
*
|
|
259
435
|
* @HasMany({ through: "recipe://ingredient" })
|
|
@@ -313,6 +489,7 @@ export class Ad4mModel {
|
|
|
313
489
|
...(options.target !== undefined && { target: options.target }),
|
|
314
490
|
...(options.filter !== undefined && { filter: options.filter }),
|
|
315
491
|
...(options.where !== undefined && { where: options.where }),
|
|
492
|
+
...(options.ordering !== undefined && { ordering: options.ordering }),
|
|
316
493
|
};
|
|
317
494
|
}
|
|
318
495
|
// Fallback: If both structures are empty but a JSON schema is attached, derive from it
|
|
@@ -342,7 +519,7 @@ export class Ad4mModel {
|
|
|
342
519
|
predicate: predicate,
|
|
343
520
|
required: isRequired,
|
|
344
521
|
readOnly: propertySchema["x-ad4m"]?.writable === false,
|
|
345
|
-
...(propertySchema["x-ad4m"]?.resolveLanguage && { resolveLanguage: propertySchema["x-ad4m"].resolveLanguage }),
|
|
522
|
+
...(propertySchema["x-ad4m"]?.resolveLanguage !== undefined && { resolveLanguage: propertySchema["x-ad4m"].resolveLanguage }),
|
|
346
523
|
...(propertySchema["x-ad4m"]?.initial && { initial: propertySchema["x-ad4m"].initial }),
|
|
347
524
|
...(propertySchema["x-ad4m"]?.local !== undefined && { local: propertySchema["x-ad4m"].local })
|
|
348
525
|
};
|
|
@@ -361,7 +538,9 @@ export class Ad4mModel {
|
|
|
361
538
|
*
|
|
362
539
|
* @param perspective - The perspective where this model will be stored
|
|
363
540
|
* @param baseExpression - Optional expression URI for this instance.
|
|
364
|
-
* If omitted, a random
|
|
541
|
+
* If omitted, a random `ad4m://obj/<id>` IRI is generated,
|
|
542
|
+
* independent of any property content — this is what keeps
|
|
543
|
+
* two instances with identical property values distinct.
|
|
365
544
|
* @param source - Optional source expression this instance is linked to
|
|
366
545
|
*
|
|
367
546
|
* @example
|
|
@@ -370,12 +549,21 @@ export class Ad4mModel {
|
|
|
370
549
|
* const recipe = new Recipe(perspective);
|
|
371
550
|
*
|
|
372
551
|
* // Create with specific base expression
|
|
373
|
-
* const recipe = new Recipe(perspective, "
|
|
552
|
+
* const recipe = new Recipe(perspective, "ad4m://obj/existing-id");
|
|
374
553
|
* ```
|
|
375
554
|
*/
|
|
376
555
|
constructor(perspective, baseExpression) {
|
|
377
556
|
this._snapshot = null;
|
|
378
|
-
|
|
557
|
+
// Use a dedicated `ad4m://obj/<id>` scheme for auto-generated
|
|
558
|
+
// baseExpressions instead of `Literal.from(...).toUrl()`'s
|
|
559
|
+
// `literal:string:<id>`. After typed-literal storage lands, the storage
|
|
560
|
+
// layer translates any `literal:string:X` target into a typed
|
|
561
|
+
// `"X"^^xsd:string` literal — which is correct for property values but
|
|
562
|
+
// wrong for relation targets (which must remain NamedNode IRIs). Using
|
|
563
|
+
// a distinct scheme keeps auto-generated instance IDs unambiguously
|
|
564
|
+
// IRIs, so relation links like `<post> --has_comment--> <comment-id>`
|
|
565
|
+
// round-trip correctly through the SPARQL store.
|
|
566
|
+
this._baseExpression = baseExpression ? baseExpression : `ad4m://obj/${makeRandomId(24)}`;
|
|
379
567
|
this._perspective = perspective;
|
|
380
568
|
}
|
|
381
569
|
/**
|
|
@@ -569,10 +757,22 @@ export class Ad4mModel {
|
|
|
569
757
|
const current = normalizeValue(this[field]);
|
|
570
758
|
const original = this._snapshot[field];
|
|
571
759
|
if (Array.isArray(current) || Array.isArray(original)) {
|
|
572
|
-
//
|
|
573
|
-
//
|
|
574
|
-
|
|
575
|
-
|
|
760
|
+
// For an ordered relation the sequence *is* the state, so a reorder is
|
|
761
|
+
// the whole change and must mark the field dirty — sorting first would
|
|
762
|
+
// make `save()` a no-op for the one edit the ordering feature exists to
|
|
763
|
+
// support.
|
|
764
|
+
//
|
|
765
|
+
// Everything else compares order-insensitively, because an unordered
|
|
766
|
+
// relation is a set: the executor returns its members by link timestamp,
|
|
767
|
+
// and a caller who assigned the same members in another order has not
|
|
768
|
+
// changed anything.
|
|
769
|
+
const ordered = !!metadata.relations[field]?.ordering;
|
|
770
|
+
const a = Array.isArray(current) ? [...current] : [];
|
|
771
|
+
const b = Array.isArray(original) ? [...original] : [];
|
|
772
|
+
if (!ordered) {
|
|
773
|
+
a.sort();
|
|
774
|
+
b.sort();
|
|
775
|
+
}
|
|
576
776
|
if (a.length !== b.length || a.some((v, i) => v !== b[i])) {
|
|
577
777
|
changed.push(field);
|
|
578
778
|
}
|
|
@@ -712,8 +912,17 @@ export class Ad4mModel {
|
|
|
712
912
|
normalIncludes[key] = val;
|
|
713
913
|
}
|
|
714
914
|
}
|
|
715
|
-
|
|
915
|
+
// A relation declared `polymorphic` reads that way by default, so the
|
|
916
|
+
// caller writes `include: { children: true }` and still gets each child
|
|
917
|
+
// hydrated as the class it actually is. Declaring it on the model rather
|
|
918
|
+
// than repeating it at every call site is the point — the relation being
|
|
919
|
+
// heterogeneous is a fact about the data, not about one query — but an
|
|
920
|
+
// explicit `polymorphic: false` at the call site still wins. Applied at
|
|
921
|
+
// every depth, since a nested include is a fact about the data too.
|
|
922
|
+
if (Object.keys(normalIncludes).length > 0) {
|
|
923
|
+
applyPolymorphicIncludeDefaults(normalIncludes, this);
|
|
716
924
|
queryInput.include = normalIncludes;
|
|
925
|
+
}
|
|
717
926
|
if (Object.keys(projections).length > 0) {
|
|
718
927
|
// Tag each projection with its target class name so the executor can
|
|
719
928
|
// resolve the target shape through its in-memory cache when applying
|
|
@@ -779,11 +988,11 @@ export class Ad4mModel {
|
|
|
779
988
|
*
|
|
780
989
|
* @internal
|
|
781
990
|
*/
|
|
782
|
-
static async executeModelQuery(perspective, query = {}, classNameOverride) {
|
|
991
|
+
static async executeModelQuery(perspective, query = {}, classNameOverride, options) {
|
|
783
992
|
// Delegate query input building to the shared prepareModelQueryParams
|
|
784
993
|
// helper. The executor resolves the shape from SHACL server-side.
|
|
785
994
|
const { className, queryJson } = this.prepareModelQueryParams(query, classNameOverride);
|
|
786
|
-
const result = await perspective.modelQuery(className, queryJson);
|
|
995
|
+
const result = await perspective.modelQuery(className, queryJson, options);
|
|
787
996
|
// Convert JSON instances to model class instances, recursively constructing
|
|
788
997
|
// class instances for any included relations resolved by Rust.
|
|
789
998
|
const instances = result.instances.map((json) => {
|
|
@@ -824,12 +1033,12 @@ export class Ad4mModel {
|
|
|
824
1033
|
* });
|
|
825
1034
|
* ```
|
|
826
1035
|
*/
|
|
827
|
-
static async findAll(perspective, query) {
|
|
1036
|
+
static async findAll(perspective, query, options) {
|
|
828
1037
|
const q = (query ?? {});
|
|
829
1038
|
if (q.properties && q.properties.length === 0) {
|
|
830
1039
|
throw new Error("properties[] must not be empty — omit the field to return all properties, or specify at least one field name");
|
|
831
1040
|
}
|
|
832
|
-
const { results } = await this.executeModelQuery(perspective, q);
|
|
1041
|
+
const { results } = await this.executeModelQuery(perspective, q, undefined, options);
|
|
833
1042
|
return results;
|
|
834
1043
|
}
|
|
835
1044
|
/**
|
|
@@ -852,9 +1061,9 @@ export class Ad4mModel {
|
|
|
852
1061
|
* }
|
|
853
1062
|
* ```
|
|
854
1063
|
*/
|
|
855
|
-
static async findOne(perspective, query) {
|
|
1064
|
+
static async findOne(perspective, query, options) {
|
|
856
1065
|
const limitedQuery = { ...(query ?? {}), limit: 1 };
|
|
857
|
-
const results = await this.findAll(perspective, limitedQuery);
|
|
1066
|
+
const results = await this.findAll(perspective, limitedQuery, options);
|
|
858
1067
|
return results[0] ?? null;
|
|
859
1068
|
}
|
|
860
1069
|
/**
|
|
@@ -874,8 +1083,8 @@ export class Ad4mModel {
|
|
|
874
1083
|
* console.log(`Showing 10 of ${totalCount} dessert recipes`);
|
|
875
1084
|
* ```
|
|
876
1085
|
*/
|
|
877
|
-
static async findAllAndCount(perspective, query) {
|
|
878
|
-
const out = await this.executeModelQuery(perspective, (query ?? {}));
|
|
1086
|
+
static async findAllAndCount(perspective, query, options) {
|
|
1087
|
+
const out = await this.executeModelQuery(perspective, (query ?? {}), undefined, options);
|
|
879
1088
|
return out;
|
|
880
1089
|
}
|
|
881
1090
|
/**
|
|
@@ -895,9 +1104,9 @@ export class Ad4mModel {
|
|
|
895
1104
|
* console.log(`Page ${page.pageNumber} of recipes, ${page.results.length} items`);
|
|
896
1105
|
* ```
|
|
897
1106
|
*/
|
|
898
|
-
static async paginate(perspective, pageSize, pageNumber, query) {
|
|
1107
|
+
static async paginate(perspective, pageSize, pageNumber, query, options) {
|
|
899
1108
|
const paginationQuery = { ...(query ?? {}), limit: pageSize, offset: pageSize * (pageNumber - 1), count: true };
|
|
900
|
-
const { results, totalCount } = await this.executeModelQuery(perspective, paginationQuery);
|
|
1109
|
+
const { results, totalCount } = await this.executeModelQuery(perspective, paginationQuery, undefined, options);
|
|
901
1110
|
return { results: results, totalCount, pageSize, pageNumber };
|
|
902
1111
|
}
|
|
903
1112
|
/**
|
|
@@ -925,8 +1134,8 @@ export class Ad4mModel {
|
|
|
925
1134
|
* });
|
|
926
1135
|
* ```
|
|
927
1136
|
*/
|
|
928
|
-
static async count(perspective, query) {
|
|
929
|
-
const { totalCount } = await this.executeModelQuery(perspective, { ...(query ?? {}), limit: 0 });
|
|
1137
|
+
static async count(perspective, query, options) {
|
|
1138
|
+
const { totalCount } = await this.executeModelQuery(perspective, { ...(query ?? {}), limit: 0 }, undefined, options);
|
|
930
1139
|
return totalCount;
|
|
931
1140
|
}
|
|
932
1141
|
async setProperty(key, value, batchId) {
|
|
@@ -937,14 +1146,22 @@ export class Ad4mModel {
|
|
|
937
1146
|
}
|
|
938
1147
|
// Generate actions from metadata (replaces Prolog query)
|
|
939
1148
|
const actions = this.generatePropertySetterAction(key, metadata);
|
|
940
|
-
// Get resolve language from metadata (replaces Prolog query)
|
|
941
|
-
let resolveLanguage = metadata.resolveLanguage;
|
|
942
1149
|
// Skip storing empty/null/undefined values to avoid invalid empty literals (e.g. literal:string:)
|
|
943
1150
|
if (value === undefined || value === null || value === "") {
|
|
944
1151
|
return;
|
|
945
1152
|
}
|
|
946
|
-
|
|
947
|
-
|
|
1153
|
+
const mode = effectiveLiteralStorage(metadata);
|
|
1154
|
+
if (mode.kind === "custom") {
|
|
1155
|
+
// Custom language: route through expression_create on that language.
|
|
1156
|
+
value = await this._perspective.createExpression(value, mode.language);
|
|
1157
|
+
}
|
|
1158
|
+
else if (mode.kind === "envelope") {
|
|
1159
|
+
// Built-in literal language: signed-envelope expression (provenance).
|
|
1160
|
+
value = await this._perspective.createExpression(value, "literal");
|
|
1161
|
+
}
|
|
1162
|
+
else {
|
|
1163
|
+
// Deterministic literal: IRI (POS-index friendly).
|
|
1164
|
+
value = valueToLiteralIri(value);
|
|
948
1165
|
}
|
|
949
1166
|
await this._perspective.executeAction(actions, this._baseExpression, [{ name: "value", value }], batchId);
|
|
950
1167
|
}
|
|
@@ -1063,41 +1280,41 @@ export class Ad4mModel {
|
|
|
1063
1280
|
// entirely and let innerUpdate write the links directly.
|
|
1064
1281
|
const metadata = this.constructor.getModelMetadata();
|
|
1065
1282
|
const hasConstructor = Object.values(metadata.properties).some((p) => p.required || p.flag || p.initial !== undefined);
|
|
1066
|
-
// Track properties
|
|
1067
|
-
//
|
|
1068
|
-
|
|
1283
|
+
// Track properties resolved through expression_create — a signed literal
|
|
1284
|
+
// envelope or a custom (non-"literal") resolveLanguage. These may fail
|
|
1285
|
+
// inside a batch context, so defer them to setProperty after createSubject.
|
|
1286
|
+
const deferredExpressionProps = [];
|
|
1069
1287
|
if (hasConstructor) {
|
|
1070
|
-
// First filter out the properties that are not relations (arrays)
|
|
1071
1288
|
const initialValues = {};
|
|
1072
1289
|
for (const [key, value] of Object.entries(this)) {
|
|
1073
1290
|
if (value !== undefined && value !== null && !(Array.isArray(value) && value.length > 0) && !value?.action) {
|
|
1074
|
-
// Check if this property requires language resolution (e.g. file storage).
|
|
1075
|
-
// If so, resolve the expression *before* passing to createSubject so
|
|
1076
|
-
// the constructor receives a valid URI instead of raw data.
|
|
1077
1291
|
const propMeta = metadata.properties[key];
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1292
|
+
// Only offer keys with a declared, settable model property. This
|
|
1293
|
+
// excludes ORM bookkeeping fields (_baseExpression, _perspective —
|
|
1294
|
+
// enumerable instance fields, not model properties), HasMany
|
|
1295
|
+
// relations (tracked in a separate registry, never in
|
|
1296
|
+
// `metadata.properties`), and read-only properties/flags
|
|
1297
|
+
// (readOnly: true). None of these have an `ad4m://setter` on the
|
|
1298
|
+
// Rust side, which otherwise logs a "declares no setter" warning
|
|
1299
|
+
// per key on every save().
|
|
1300
|
+
if (!propMeta || propMeta.readOnly) {
|
|
1301
|
+
continue;
|
|
1302
|
+
}
|
|
1303
|
+
if (effectiveLiteralStorage(propMeta).kind !== "deterministic") {
|
|
1304
|
+
deferredExpressionProps.push(key);
|
|
1083
1305
|
continue;
|
|
1084
1306
|
}
|
|
1085
1307
|
initialValues[key] = value;
|
|
1086
1308
|
}
|
|
1087
1309
|
}
|
|
1088
|
-
// Get the class name instead of passing the instance to avoid Prolog query generation
|
|
1089
1310
|
const className = await this.perspective.stringOrTemplateObjectToSubjectClassName(this);
|
|
1090
|
-
// Create the subject with the initial values
|
|
1091
1311
|
await this.perspective.createSubject(className, this._baseExpression, initialValues, batchId);
|
|
1092
1312
|
}
|
|
1093
1313
|
// Set properties and relations via innerUpdate.
|
|
1094
1314
|
// When createSubject was skipped (no constructor actions), we must enable
|
|
1095
1315
|
// property writing so that scalar values are persisted as links.
|
|
1096
1316
|
await this.innerUpdate(!hasConstructor, batchId);
|
|
1097
|
-
|
|
1098
|
-
// from initialValues. setProperty will call createExpression to upload
|
|
1099
|
-
// the data to the appropriate language and store the resulting URI.
|
|
1100
|
-
for (const key of deferredResolveLanguageProps) {
|
|
1317
|
+
for (const key of deferredExpressionProps) {
|
|
1101
1318
|
const value = this[key];
|
|
1102
1319
|
if (value !== undefined && value !== null) {
|
|
1103
1320
|
await this.setProperty(key, value, batchId);
|
|
@@ -1192,13 +1409,36 @@ export class Ad4mModel {
|
|
|
1192
1409
|
await this.setRelationValues(key, value, batchId);
|
|
1193
1410
|
}
|
|
1194
1411
|
else if (value !== undefined && value !== null && value !== "") {
|
|
1412
|
+
// A scalar handed to a relation field is a single-member relation.
|
|
1413
|
+
//
|
|
1414
|
+
// This used to `continue` — "Skip, it's a relation, not a regular
|
|
1415
|
+
// property" — so `Model.create(p, { source: 'uri://x' })` typechecked,
|
|
1416
|
+
// ran, resolved, and wrote no link. The record was created with the
|
|
1417
|
+
// relation empty and nothing anywhere said so, which surfaces later as
|
|
1418
|
+
// a *reader* complaining about a malformed instance: a Relationship
|
|
1419
|
+
// with no endpoints, a Placement pointing at nothing. Both look like
|
|
1420
|
+
// rendering bugs from the outside, because the record exists and the
|
|
1421
|
+
// thing that should reference it does not.
|
|
1422
|
+
//
|
|
1423
|
+
// The array form is the documented path and stays correct; the problem
|
|
1424
|
+
// is that the scalar form is plausible, typechecks against a field
|
|
1425
|
+
// declared `string`, and fails silently. Coercing matches what a caller
|
|
1426
|
+
// reaching for it meant. A value `setRelationValues` cannot use now
|
|
1427
|
+
// fails there, loudly, instead of vanishing here.
|
|
1428
|
+
//
|
|
1429
|
+
// Deliberately outside the `setProperties` gate, mirroring the array
|
|
1430
|
+
// branch above. `create` passes `setProperties: false` when the class
|
|
1431
|
+
// has a constructor, because `create_subject` writes the values map —
|
|
1432
|
+
// but a relation carries `ad4m://adder`, not `ad4m://setter`, so
|
|
1433
|
+
// `create_subject` drops it (see the warning added in 3ee2b5bc6).
|
|
1434
|
+
// Relations have to be written here in both cases or they are lost by
|
|
1435
|
+
// both paths at once.
|
|
1436
|
+
const relationMeta = this.getRelationOptions(key);
|
|
1437
|
+
if (relationMeta) {
|
|
1438
|
+
await this.setRelationValues(key, [value], batchId);
|
|
1439
|
+
continue;
|
|
1440
|
+
}
|
|
1195
1441
|
if (setProperties) {
|
|
1196
|
-
// Check if this is a relation property (has relation metadata)
|
|
1197
|
-
const relationMeta = this.getRelationOptions(key);
|
|
1198
|
-
if (relationMeta) {
|
|
1199
|
-
// Skip - it's a relation, not a regular property
|
|
1200
|
-
continue;
|
|
1201
|
-
}
|
|
1202
1442
|
// Skip flag properties — they are immutable after creation
|
|
1203
1443
|
const propMeta = this.getPropertyMetadata(key);
|
|
1204
1444
|
if (propMeta?.flag) {
|
|
@@ -1392,7 +1632,7 @@ export class Ad4mModel {
|
|
|
1392
1632
|
* @param perspective - The perspective to create the instance in
|
|
1393
1633
|
* @param data - Property values to assign before saving
|
|
1394
1634
|
* @param options - Optional settings:
|
|
1395
|
-
* - `parent` — a `
|
|
1635
|
+
* - `parent` — a `Scope` (model form or raw form) whose `id` will
|
|
1396
1636
|
* be used to create an incoming link from the parent to the new instance.
|
|
1397
1637
|
* - `batchId` — an existing batch id; when provided the link write and
|
|
1398
1638
|
* `save()` are added to the batch instead of committed immediately.
|
|
@@ -1624,8 +1864,7 @@ export class Ad4mModel {
|
|
|
1624
1864
|
* // With explicit configuration
|
|
1625
1865
|
* const PersonClass = Ad4mModel.fromJSONSchema(schema, {
|
|
1626
1866
|
* name: "Person",
|
|
1627
|
-
* namespace: "person://"
|
|
1628
|
-
* resolveLanguage: "literal"
|
|
1867
|
+
* namespace: "person://"
|
|
1629
1868
|
* });
|
|
1630
1869
|
*
|
|
1631
1870
|
* // With property mapping
|