@coasys/ad4m 0.12.1-dev → 0.13.0-postmessage-ws-proxy.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -25
- package/jest-setup.ts +1 -1
- package/lib/index.cjs +8677 -15318
- package/lib/index.js +14507 -21223
- package/lib/src/Ad4mClient.d.ts +9 -3
- package/lib/src/Ad4mClient.js +40 -13
- package/lib/src/Literal.js +6 -5
- package/lib/src/agent/Agent.js +26 -272
- package/lib/src/agent/AgentClient.d.ts +33 -21
- package/lib/src/agent/AgentClient.js +180 -374
- package/lib/src/agent/AgentStatus.js +2 -37
- package/lib/src/ai/AIClient.d.ts +12 -4
- package/lib/src/ai/AIClient.js +81 -285
- package/lib/src/ai/AITypes.d.ts +55 -0
- package/lib/src/ai/AITypes.js +18 -0
- package/lib/src/ai/Tasks.js +10 -133
- package/lib/src/apiClient.d.ts +53 -0
- package/lib/src/apiClient.js +234 -0
- package/lib/src/cache/PersistentCache.d.ts +40 -0
- package/lib/src/cache/PersistentCache.js +139 -0
- package/lib/src/expression/Expression.d.ts +1 -1
- package/lib/src/expression/Expression.js +13 -122
- package/lib/src/expression/ExpressionClient.d.ts +11 -3
- package/lib/src/expression/ExpressionClient.js +120 -75
- package/lib/src/expression/ExpressionRef.js +4 -27
- package/lib/src/generated/api/AIModelLoadingStatus.d.ts +7 -0
- package/lib/src/generated/api/AIModelLoadingStatus.js +2 -0
- package/lib/src/generated/api/AddLinkExpressionRequest.d.ts +6 -0
- package/lib/src/generated/api/AddLinkExpressionRequest.js +1 -0
- package/lib/src/generated/api/AddLinkRequest.d.ts +6 -0
- package/lib/src/generated/api/AddLinkRequest.js +1 -0
- package/lib/src/generated/api/AddLinksBulkRequest.d.ts +6 -0
- package/lib/src/generated/api/AddLinksBulkRequest.js +1 -0
- package/lib/src/generated/api/AddSdnaRequest.d.ts +6 -0
- package/lib/src/generated/api/AddSdnaRequest.js +2 -0
- package/lib/src/generated/api/ApplyTemplateRequest.d.ts +4 -0
- package/lib/src/generated/api/ApplyTemplateRequest.js +2 -0
- package/lib/src/generated/api/AuthInfoInput.d.ts +10 -0
- package/lib/src/generated/api/AuthInfoInput.js +1 -0
- package/lib/src/generated/api/CapabilityInput.d.ts +5 -0
- package/lib/src/generated/api/CapabilityInput.js +1 -0
- package/lib/src/generated/api/CommitBatchRequest.d.ts +3 -0
- package/lib/src/generated/api/CommitBatchRequest.js +2 -0
- package/lib/src/generated/api/ComputeLogEntry.d.ts +9 -0
- package/lib/src/generated/api/ComputeLogEntry.js +2 -0
- package/lib/src/generated/api/CreateExpressionRequest.d.ts +4 -0
- package/lib/src/generated/api/CreateExpressionRequest.js +2 -0
- package/lib/src/generated/api/CreatePerspectiveRequest.d.ts +3 -0
- package/lib/src/generated/api/CreatePerspectiveRequest.js +2 -0
- package/lib/src/generated/api/CreateSubjectRequest.d.ts +6 -0
- package/lib/src/generated/api/CreateSubjectRequest.js +2 -0
- package/lib/src/generated/api/EmbedRequest.d.ts +4 -0
- package/lib/src/generated/api/EmbedRequest.js +2 -0
- package/lib/src/generated/api/EntanglementProofInput.d.ts +6 -0
- package/lib/src/generated/api/EntanglementProofInput.js +2 -0
- package/lib/src/generated/api/ExecuteCommandsRequest.d.ts +4 -0
- package/lib/src/generated/api/ExecuteCommandsRequest.js +2 -0
- package/lib/src/generated/api/ExportRequest.d.ts +5 -0
- package/lib/src/generated/api/ExportRequest.js +2 -0
- package/lib/src/generated/api/ExpressionManyRequest.d.ts +3 -0
- package/lib/src/generated/api/ExpressionManyRequest.js +2 -0
- package/lib/src/generated/api/ExpressionProof.d.ts +4 -0
- package/lib/src/generated/api/ExpressionProof.js +2 -0
- package/lib/src/generated/api/ExpressionProofInput.d.ts +6 -0
- package/lib/src/generated/api/ExpressionProofInput.js +2 -0
- package/lib/src/generated/api/FriendSendMessageRequest.d.ts +3 -0
- package/lib/src/generated/api/FriendSendMessageRequest.js +2 -0
- package/lib/src/generated/api/FriendsListRequest.d.ts +3 -0
- package/lib/src/generated/api/FriendsListRequest.js +2 -0
- package/lib/src/generated/api/GenerateAgentRequest.d.ts +3 -0
- package/lib/src/generated/api/GenerateAgentRequest.js +2 -0
- package/lib/src/generated/api/GenerateJwtRequest.d.ts +4 -0
- package/lib/src/generated/api/GenerateJwtRequest.js +2 -0
- package/lib/src/generated/api/GetSubjectDataRequest.d.ts +4 -0
- package/lib/src/generated/api/GetSubjectDataRequest.js +2 -0
- package/lib/src/generated/api/ImportAgentRequest.d.ts +6 -0
- package/lib/src/generated/api/ImportAgentRequest.js +2 -0
- package/lib/src/generated/api/ImportRequest.d.ts +4 -0
- package/lib/src/generated/api/ImportRequest.js +2 -0
- package/lib/src/generated/api/JoinNeighbourhoodRequest.d.ts +3 -0
- package/lib/src/generated/api/JoinNeighbourhoodRequest.js +2 -0
- package/lib/src/generated/api/LanguageMetaInput.d.ts +6 -0
- package/lib/src/generated/api/LanguageMetaInput.js +2 -0
- package/lib/src/generated/api/Link.d.ts +5 -0
- package/lib/src/generated/api/Link.js +2 -0
- package/lib/src/generated/api/LinkExpression.d.ts +10 -0
- package/lib/src/generated/api/LinkExpression.js +1 -0
- package/lib/src/generated/api/LinkExpressionInput.d.ts +10 -0
- package/lib/src/generated/api/LinkExpressionInput.js +1 -0
- package/lib/src/generated/api/LinkInput.d.ts +5 -0
- package/lib/src/generated/api/LinkInput.js +2 -0
- package/lib/src/generated/api/LinkLanguageTemplatesRequest.d.ts +3 -0
- package/lib/src/generated/api/LinkLanguageTemplatesRequest.js +2 -0
- package/lib/src/generated/api/LinkMutations.d.ts +6 -0
- package/lib/src/generated/api/LinkMutations.js +1 -0
- package/lib/src/generated/api/LinkStatus.d.ts +1 -0
- package/lib/src/generated/api/LinkStatus.js +2 -0
- package/lib/src/generated/api/LockAgentRequest.d.ts +3 -0
- package/lib/src/generated/api/LockAgentRequest.js +2 -0
- package/lib/src/generated/api/ModelType.d.ts +1 -0
- package/lib/src/generated/api/ModelType.js +2 -0
- package/lib/src/generated/api/NotificationInput.d.ts +10 -0
- package/lib/src/generated/api/NotificationInput.js +2 -0
- package/lib/src/generated/api/OpenLinkRequest.d.ts +3 -0
- package/lib/src/generated/api/OpenLinkRequest.js +2 -0
- package/lib/src/generated/api/PermitCapabilityRequest.d.ts +3 -0
- package/lib/src/generated/api/PermitCapabilityRequest.js +2 -0
- package/lib/src/generated/api/PerspectiveInput.d.ts +4 -0
- package/lib/src/generated/api/PerspectiveInput.js +1 -0
- package/lib/src/generated/api/PromptRequest.d.ts +4 -0
- package/lib/src/generated/api/PromptRequest.js +2 -0
- package/lib/src/generated/api/PublishLanguageRequest.d.ts +5 -0
- package/lib/src/generated/api/PublishLanguageRequest.js +1 -0
- package/lib/src/generated/api/PublishNeighbourhoodRequest.d.ts +6 -0
- package/lib/src/generated/api/PublishNeighbourhoodRequest.js +1 -0
- package/lib/src/generated/api/QueryRequest.d.ts +7 -0
- package/lib/src/generated/api/QueryRequest.js +2 -0
- package/lib/src/generated/api/RemoveLinkRequest.d.ts +5 -0
- package/lib/src/generated/api/RemoveLinkRequest.js +1 -0
- package/lib/src/generated/api/RemoveLinksBulkRequest.d.ts +5 -0
- package/lib/src/generated/api/RemoveLinksBulkRequest.js +1 -0
- package/lib/src/generated/api/ResourceInput.d.ts +4 -0
- package/lib/src/generated/api/ResourceInput.js +2 -0
- package/lib/src/generated/api/SetDefaultModelRequest.d.ts +4 -0
- package/lib/src/generated/api/SetDefaultModelRequest.js +1 -0
- package/lib/src/generated/api/SetFreeHostingEnabledRequest.d.ts +3 -0
- package/lib/src/generated/api/SetFreeHostingEnabledRequest.js +2 -0
- package/lib/src/generated/api/SetMultiUserRequest.d.ts +3 -0
- package/lib/src/generated/api/SetMultiUserRequest.js +2 -0
- package/lib/src/generated/api/SetStatusRequest.d.ts +3 -0
- package/lib/src/generated/api/SetStatusRequest.js +2 -0
- package/lib/src/generated/api/SignMessageRequest.d.ts +3 -0
- package/lib/src/generated/api/SignMessageRequest.js +2 -0
- package/lib/src/generated/api/TrustedAgentsRequest.d.ts +3 -0
- package/lib/src/generated/api/TrustedAgentsRequest.js +2 -0
- package/lib/src/generated/api/UnlockAgentRequest.d.ts +4 -0
- package/lib/src/generated/api/UnlockAgentRequest.js +2 -0
- package/lib/src/generated/api/UpdateLinkRequest.d.ts +7 -0
- package/lib/src/generated/api/UpdateLinkRequest.js +1 -0
- package/lib/src/generated/api/UpdatePerspectiveRequest.d.ts +3 -0
- package/lib/src/generated/api/UpdatePerspectiveRequest.js +2 -0
- package/lib/src/generated/api/UserStatistics.d.ts +9 -0
- package/lib/src/generated/api/UserStatistics.js +2 -0
- package/lib/src/generated/api/VerificationRequestResult.d.ts +6 -0
- package/lib/src/generated/api/VerificationRequestResult.js +2 -0
- package/lib/src/generated/api/VerifySignatureRequest.d.ts +5 -0
- package/lib/src/generated/api/VerifySignatureRequest.js +2 -0
- package/lib/src/generated/api/WriteSettingsRequest.d.ts +3 -0
- package/lib/src/generated/api/WriteSettingsRequest.js +2 -0
- package/lib/src/generated/api/index.d.ts +42 -0
- package/lib/src/generated/api/index.js +7 -0
- package/lib/src/index.d.ts +8 -2
- package/lib/src/index.js +8 -2
- package/lib/src/jestSetup.d.ts +0 -1
- package/lib/src/jestSetup.js +1 -1
- package/lib/src/language/Icon.js +2 -21
- package/lib/src/language/Language.d.ts +26 -176
- package/lib/src/language/Language.js +32 -72
- package/lib/src/language/LanguageClient.d.ts +2 -2
- package/lib/src/language/LanguageClient.js +15 -105
- package/lib/src/language/LanguageHandle.js +2 -41
- package/lib/src/language/LanguageMeta.js +4 -75
- package/lib/src/language/LanguageRef.js +2 -25
- package/lib/src/links/Links.js +14 -111
- package/lib/src/model/Ad4mModel.d.ts +143 -63
- package/lib/src/model/Ad4mModel.js +583 -425
- package/lib/src/model/Ad4mModel.test.js +1434 -688
- package/lib/src/model/ModelQueryBuilder.d.ts +61 -45
- package/lib/src/model/ModelQueryBuilder.js +315 -157
- package/lib/src/model/Subject.js +6 -6
- package/lib/src/model/decorators.d.ts +36 -8
- package/lib/src/model/decorators.js +41 -14
- package/lib/src/model/hydration-batch.d.ts +34 -0
- package/lib/src/model/hydration-batch.js +147 -0
- package/lib/src/model/hydration-batching.test.d.ts +7 -0
- package/lib/src/model/hydration-batching.test.js +456 -0
- package/lib/src/model/hydration.d.ts +3 -92
- package/lib/src/model/hydration.js +3 -691
- package/lib/src/model/index.d.ts +3 -4
- package/lib/src/model/index.js +3 -4
- package/lib/src/model/json-schema.js +2 -2
- package/lib/src/model/model-manifest.d.ts +41 -0
- package/lib/src/model/model-manifest.js +11 -0
- package/lib/src/model/query-cache.d.ts +30 -0
- package/lib/src/model/query-cache.js +66 -0
- package/lib/src/model/query-cache.test.d.ts +1 -0
- package/lib/src/model/query-cache.test.js +43 -0
- package/lib/src/model/query-common.d.ts +1 -1
- package/lib/src/model/query-common.js +1 -1
- package/lib/src/model/query-sparql-batch.d.ts +17 -0
- package/lib/src/model/query-sparql-batch.js +260 -0
- package/lib/src/model/query-sparql.d.ts +51 -0
- package/lib/src/model/query-sparql.js +466 -0
- package/lib/src/model/query-sparql.test.d.ts +1 -0
- package/lib/src/model/query-sparql.test.js +312 -0
- package/lib/src/model/{surreal-utils.d.ts → query-utils.d.ts} +13 -9
- package/lib/src/model/query-utils.js +189 -0
- package/lib/src/model/relation-filtering.test.js +15 -15
- package/lib/src/model/sdna.js +1 -1
- package/lib/src/model/shacl-gen.js +154 -57
- package/lib/src/model/subscription-delta.test.d.ts +24 -0
- package/lib/src/model/subscription-delta.test.js +180 -0
- package/lib/src/model/subscription-pool.d.ts +39 -0
- package/lib/src/model/subscription-pool.js +165 -0
- package/lib/src/model/types.d.ts +195 -5
- package/lib/src/model/types.js +5 -2
- package/lib/src/neighbourhood/Neighbourhood.js +4 -32
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +3 -3
- package/lib/src/neighbourhood/NeighbourhoodClient.js +40 -203
- package/lib/src/perspectives/LinkQuery.js +2 -42
- package/lib/src/perspectives/Perspective.d.ts +1 -1
- package/lib/src/perspectives/Perspective.js +10 -52
- package/lib/src/perspectives/PerspectiveClient.d.ts +22 -32
- package/lib/src/perspectives/PerspectiveClient.js +188 -454
- package/lib/src/perspectives/PerspectiveClient.test.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveClient.test.js +224 -0
- package/lib/src/perspectives/PerspectiveDiff.js +4 -31
- package/lib/src/perspectives/PerspectiveHandle.js +3 -43
- package/lib/src/perspectives/PerspectiveProxy.d.ts +157 -78
- package/lib/src/perspectives/PerspectiveProxy.js +475 -389
- package/lib/src/perspectives/PerspectiveResolver.d.ts +0 -1
- package/lib/src/perspectives/PerspectiveResolver.js +0 -11
- package/lib/src/perspectives/SparqlBindings.d.ts +47 -0
- package/lib/src/perspectives/SparqlBindings.js +65 -0
- package/lib/src/perspectives/WakerSubscriptionManager.d.ts +3 -3
- package/lib/src/perspectives/WakerSubscriptionManager.js +12 -12
- package/lib/src/runtime/RuntimeClient.d.ts +8 -6
- package/lib/src/runtime/RuntimeClient.js +97 -454
- package/lib/src/runtime/RuntimeResolver.d.ts +14 -0
- package/lib/src/runtime/RuntimeResolver.js +84 -0
- package/lib/src/runtime/RuntimeTypes.d.ts +101 -0
- package/lib/src/runtime/RuntimeTypes.js +45 -0
- package/lib/src/schema.gql +14 -1
- package/lib/src/shacl/SHACLFlow.d.ts +1 -1
- package/lib/src/shacl/SHACLFlow.js +9 -9
- package/lib/src/shacl/SHACLShape.d.ts +26 -1
- package/lib/src/shacl/SHACLShape.js +140 -38
- package/lib/src/shacl/SHACLShape.test.js +82 -0
- package/lib/src/typeDefs.js +3 -615
- package/lib/src/utils.d.ts +6 -6
- package/lib/src/utils.js +6 -6
- package/package.json +9 -24
- package/rollup.config.js +0 -7
- package/src/Ad4mClient.test.ts +1192 -1288
- package/src/Ad4mClient.ts +46 -15
- package/src/Literal.test.ts +10 -5
- package/src/Literal.ts +6 -5
- package/src/agent/Agent.ts +0 -99
- package/src/agent/AgentClient.ts +238 -467
- package/src/agent/AgentStatus.ts +0 -12
- package/src/ai/AIClient.ts +96 -296
- package/src/ai/AITypes.ts +66 -0
- package/src/ai/Tasks.ts +0 -52
- package/src/apiClient.ts +287 -0
- package/src/cache/PersistentCache.ts +145 -0
- package/src/cache/cache.test.ts +713 -0
- package/src/expression/Expression.ts +3 -43
- package/src/expression/ExpressionClient.ts +148 -77
- package/src/expression/ExpressionRef.ts +2 -8
- package/src/generated/api/AIModelLoadingStatus.ts +3 -0
- package/src/generated/api/AddLinkExpressionRequest.ts +4 -0
- package/src/generated/api/AddLinkRequest.ts +4 -0
- package/src/generated/api/AddLinksBulkRequest.ts +4 -0
- package/src/generated/api/AddSdnaRequest.ts +3 -0
- package/src/generated/api/ApplyTemplateRequest.ts +3 -0
- package/src/generated/api/AuthInfoInput.ts +4 -0
- package/src/generated/api/CapabilityInput.ts +4 -0
- package/src/generated/api/CommitBatchRequest.ts +3 -0
- package/src/generated/api/ComputeLogEntry.ts +3 -0
- package/src/generated/api/CreateExpressionRequest.ts +3 -0
- package/src/generated/api/CreatePerspectiveRequest.ts +3 -0
- package/src/generated/api/CreateSubjectRequest.ts +3 -0
- package/src/generated/api/EmbedRequest.ts +3 -0
- package/src/generated/api/EntanglementProofInput.ts +3 -0
- package/src/generated/api/ExecuteCommandsRequest.ts +3 -0
- package/src/generated/api/ExportRequest.ts +3 -0
- package/src/generated/api/ExpressionManyRequest.ts +3 -0
- package/src/generated/api/ExpressionProof.ts +3 -0
- package/src/generated/api/ExpressionProofInput.ts +3 -0
- package/src/generated/api/FriendSendMessageRequest.ts +3 -0
- package/src/generated/api/FriendsListRequest.ts +3 -0
- package/src/generated/api/GenerateAgentRequest.ts +3 -0
- package/src/generated/api/GenerateJwtRequest.ts +3 -0
- package/src/generated/api/GetSubjectDataRequest.ts +3 -0
- package/src/generated/api/ImportAgentRequest.ts +3 -0
- package/src/generated/api/ImportRequest.ts +3 -0
- package/src/generated/api/JoinNeighbourhoodRequest.ts +3 -0
- package/src/generated/api/LanguageMetaInput.ts +3 -0
- package/src/generated/api/Link.ts +3 -0
- package/src/generated/api/LinkExpression.ts +6 -0
- package/src/generated/api/LinkExpressionInput.ts +6 -0
- package/src/generated/api/LinkInput.ts +3 -0
- package/src/generated/api/LinkLanguageTemplatesRequest.ts +3 -0
- package/src/generated/api/LinkMutations.ts +5 -0
- package/src/generated/api/LinkStatus.ts +3 -0
- package/src/generated/api/LockAgentRequest.ts +3 -0
- package/src/generated/api/ModelType.ts +3 -0
- package/src/generated/api/NotificationInput.ts +3 -0
- package/src/generated/api/OpenLinkRequest.ts +3 -0
- package/src/generated/api/PermitCapabilityRequest.ts +3 -0
- package/src/generated/api/PerspectiveInput.ts +4 -0
- package/src/generated/api/PromptRequest.ts +3 -0
- package/src/generated/api/PublishLanguageRequest.ts +4 -0
- package/src/generated/api/PublishNeighbourhoodRequest.ts +4 -0
- package/src/generated/api/QueryRequest.ts +6 -0
- package/src/generated/api/RemoveLinkRequest.ts +4 -0
- package/src/generated/api/RemoveLinksBulkRequest.ts +4 -0
- package/src/generated/api/ResourceInput.ts +3 -0
- package/src/generated/api/SetDefaultModelRequest.ts +4 -0
- package/src/generated/api/SetFreeHostingEnabledRequest.ts +3 -0
- package/src/generated/api/SetMultiUserRequest.ts +3 -0
- package/src/generated/api/SetStatusRequest.ts +3 -0
- package/src/generated/api/SignMessageRequest.ts +3 -0
- package/src/generated/api/TrustedAgentsRequest.ts +3 -0
- package/src/generated/api/UnlockAgentRequest.ts +3 -0
- package/src/generated/api/UpdateLinkRequest.ts +5 -0
- package/src/generated/api/UpdatePerspectiveRequest.ts +3 -0
- package/src/generated/api/UserStatistics.ts +3 -0
- package/src/generated/api/VerificationRequestResult.ts +3 -0
- package/src/generated/api/VerifySignatureRequest.ts +3 -0
- package/src/generated/api/WriteSettingsRequest.ts +3 -0
- package/src/generated/api/index.ts +49 -0
- package/src/index.ts +8 -2
- package/src/jestSetup.ts +1 -1
- package/src/language/Icon.ts +0 -4
- package/src/language/Language.ts +51 -248
- package/src/language/LanguageClient.ts +21 -132
- package/src/language/LanguageHandle.ts +0 -14
- package/src/language/LanguageMeta.ts +0 -29
- package/src/language/LanguageRef.ts +0 -7
- package/src/links/Links.ts +0 -37
- package/src/model/Ad4mModel.test.ts +1664 -792
- package/src/model/Ad4mModel.ts +664 -499
- package/src/model/ModelQueryBuilder.ts +362 -183
- package/src/model/decorators.ts +71 -22
- package/src/model/hydration.ts +3 -772
- package/src/model/index.ts +3 -4
- package/src/model/json-schema.ts +2 -2
- package/src/model/query-cache.test.ts +54 -0
- package/src/model/query-cache.ts +79 -0
- package/src/model/query-common.ts +1 -1
- package/src/model/query-sparql.test.ts +376 -0
- package/src/model/query-sparql.ts +504 -0
- package/src/model/query-utils.ts +209 -0
- package/src/model/relation-filtering.test.ts +60 -53
- package/src/model/sdna.ts +1 -1
- package/src/model/shacl-gen.ts +163 -64
- package/src/model/shacl-roundtrip.test.ts +197 -0
- package/src/model/types-typed-query.test.ts +360 -0
- package/src/model/types.ts +256 -5
- package/src/neighbourhood/Neighbourhood.ts +0 -9
- package/src/neighbourhood/NeighbourhoodClient.ts +42 -207
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +34 -36
- package/src/perspectives/LinkQuery.ts +0 -15
- package/src/perspectives/Perspective.ts +1 -12
- package/src/perspectives/PerspectiveClient.test.ts +262 -0
- package/src/perspectives/PerspectiveClient.ts +252 -468
- package/src/perspectives/PerspectiveDiff.ts +0 -8
- package/src/perspectives/PerspectiveHandle.ts +1 -12
- package/src/perspectives/PerspectiveProxy.test.ts +125 -0
- package/src/perspectives/PerspectiveProxy.ts +522 -434
- package/src/perspectives/SparqlBindings.ts +79 -0
- package/src/perspectives/WakerSubscriptionManager.ts +13 -13
- package/src/runtime/RuntimeClient.ts +118 -461
- package/src/runtime/RuntimeTypes.ts +144 -0
- package/src/shacl/SHACLFlow.ts +9 -9
- package/src/shacl/SHACLShape.test.ts +204 -0
- package/src/shacl/SHACLShape.ts +191 -39
- package/src/typeDefs.ts +3 -5
- package/src/utils.ts +6 -6
- package/tsconfig.json +3 -7
- 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.js +0 -128
- package/shims/reflect-metadata.ts +0 -0
- package/shims/type-graphql.ts +0 -75
- package/src/PubSub.ts +0 -17
- package/src/agent/AgentResolver.ts +0 -263
- package/src/ai/AIResolver.ts +0 -372
- package/src/buildSchema.ts +0 -43
- package/src/expression/ExpressionResolver.ts +0 -64
- package/src/language/LanguageContext.ts +0 -37
- package/src/language/LanguageResolver.ts +0 -98
- package/src/model/Subject.ts +0 -159
- package/src/model/prolog-facts.ts +0 -145
- package/src/model/query-prolog.ts +0 -148
- package/src/model/query-surreal.ts +0 -280
- package/src/model/surreal-utils.ts +0 -147
- package/src/neighbourhood/NeighbourhoodResolver.ts +0 -100
- package/src/perspectives/PerspectiveResolver.ts +0 -372
- package/src/runtime/RuntimeResolver.ts +0 -691
- package/src/unwrapApolloResult.ts +0 -16
- package/tsconfig-browser.json +0 -82
package/src/model/hydration.ts
CHANGED
|
@@ -1,24 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* hydration.ts — Instance hydration helpers extracted from Ad4mModel.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* the instance, metadata, or perspective as explicit parameters.
|
|
4
|
+
* After the Rust model_query pipeline migration, most hydration logic moved
|
|
5
|
+
* to Rust. This file retains:
|
|
6
|
+
* - normalizeValue() — snapshot dirty-tracking
|
|
8
7
|
*/
|
|
9
8
|
|
|
10
|
-
import { Literal } from "../Literal";
|
|
11
|
-
import { LinkQuery } from "../perspectives/LinkQuery";
|
|
12
|
-
import type { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
13
|
-
import { getPropertiesMetadata, getRelationsMetadata, buildConformanceFilter } from "./decorators";
|
|
14
|
-
import type { RelationMetadataEntry } from "./decorators";
|
|
15
|
-
import { escapeSurrealString } from "../utils";
|
|
16
|
-
import { formatSurrealValue, compileWhereClause } from "./surreal-utils";
|
|
17
|
-
import type {
|
|
18
|
-
PropertyMetadata, RelationMetadata, ModelMetadata,
|
|
19
|
-
ValueTuple, WhereCondition, IncludeMap, RelationSubQuery,
|
|
20
|
-
} from "./types";
|
|
21
|
-
|
|
22
9
|
// ──────────────────────────────────────────────────────────
|
|
23
10
|
// Pure helpers
|
|
24
11
|
// ──────────────────────────────────────────────────────────
|
|
@@ -37,760 +24,4 @@ export function normalizeValue(value: any): any {
|
|
|
37
24
|
return value;
|
|
38
25
|
}
|
|
39
26
|
|
|
40
|
-
/**
|
|
41
|
-
* Checks if a value matches a where-condition (for post-query filtering).
|
|
42
|
-
*/
|
|
43
|
-
export function matchesCondition(value: any, condition: WhereCondition): boolean {
|
|
44
|
-
// Handle array values (IN clause)
|
|
45
|
-
if (Array.isArray(condition)) {
|
|
46
|
-
return (condition as any[]).includes(value);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// Handle operator object
|
|
50
|
-
if (typeof condition === 'object' && condition !== null) {
|
|
51
|
-
const ops = condition as any;
|
|
52
|
-
|
|
53
|
-
// Special case: 'not' operator (exclusive with other operators)
|
|
54
|
-
if (ops.not !== undefined) {
|
|
55
|
-
if (Array.isArray(ops.not)) {
|
|
56
|
-
return !(ops.not as any[]).includes(value);
|
|
57
|
-
} else {
|
|
58
|
-
return value !== ops.not;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Special case: 'between' operator (inclusive range, exclusive with gt/gte/lt/lte)
|
|
63
|
-
if (ops.between !== undefined && Array.isArray(ops.between) && ops.between.length === 2) {
|
|
64
|
-
return value >= ops.between[0] && value <= ops.between[1];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// For all other operators (gt, gte, lt, lte, contains), we need to check ALL of them
|
|
68
|
-
// and return true only if ALL conditions are satisfied
|
|
69
|
-
let allConditionsMet = true;
|
|
70
|
-
|
|
71
|
-
if (ops.gt !== undefined) {
|
|
72
|
-
allConditionsMet = allConditionsMet && (value > ops.gt);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (ops.gte !== undefined) {
|
|
76
|
-
allConditionsMet = allConditionsMet && (value >= ops.gte);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (ops.lt !== undefined) {
|
|
80
|
-
allConditionsMet = allConditionsMet && (value < ops.lt);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (ops.lte !== undefined) {
|
|
84
|
-
allConditionsMet = allConditionsMet && (value <= ops.lte);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (ops.contains !== undefined) {
|
|
88
|
-
if (typeof value === 'string') {
|
|
89
|
-
allConditionsMet = allConditionsMet && value.includes(String(ops.contains));
|
|
90
|
-
} else if (Array.isArray(value)) {
|
|
91
|
-
allConditionsMet = allConditionsMet && value.includes(ops.contains);
|
|
92
|
-
} else {
|
|
93
|
-
allConditionsMet = false;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return allConditionsMet;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Simple equality
|
|
101
|
-
return value === condition;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// ──────────────────────────────────────────────────────────
|
|
105
|
-
// Property / link hydration
|
|
106
|
-
// ──────────────────────────────────────────────────────────
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Resolves a single property value from a raw link target string.
|
|
110
|
-
*
|
|
111
|
-
* Handles expression resolution, literal URI parsing, primitive
|
|
112
|
-
* type coercion, and optional transform functions.
|
|
113
|
-
*
|
|
114
|
-
* @param target - The raw target string from the link
|
|
115
|
-
* @param propMeta - Property metadata from the decorator registry
|
|
116
|
-
* @param perspective - The perspective for expression resolution
|
|
117
|
-
* @param expectedType - Optional JS typeof hint for coercion (e.g. 'number')
|
|
118
|
-
* @returns The resolved value
|
|
119
|
-
*/
|
|
120
|
-
export async function hydratePropertyValue(
|
|
121
|
-
target: string,
|
|
122
|
-
propMeta: PropertyMetadata,
|
|
123
|
-
perspective: PerspectiveProxy,
|
|
124
|
-
expectedType?: string,
|
|
125
|
-
): Promise<any> {
|
|
126
|
-
let value: any = target;
|
|
127
|
-
|
|
128
|
-
if (target !== undefined && target !== null && target !== '') {
|
|
129
|
-
// Non-literal expression resolution
|
|
130
|
-
if (
|
|
131
|
-
propMeta.resolveLanguage != null &&
|
|
132
|
-
propMeta.resolveLanguage !== 'literal' &&
|
|
133
|
-
typeof target === 'string' &&
|
|
134
|
-
!target.startsWith('literal://')
|
|
135
|
-
) {
|
|
136
|
-
try {
|
|
137
|
-
const expression = await perspective.getExpression(target);
|
|
138
|
-
if (expression) {
|
|
139
|
-
try { value = JSON.parse(expression.data); } catch { value = expression.data; }
|
|
140
|
-
}
|
|
141
|
-
} catch (e) {
|
|
142
|
-
console.warn(`hydratePropertyValue: failed to resolve expression for "${propMeta.name}":`, e);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
// Literal URI parsing
|
|
146
|
-
else if (
|
|
147
|
-
propMeta.resolveLanguage === 'literal' &&
|
|
148
|
-
typeof target === 'string' &&
|
|
149
|
-
target.startsWith('literal://')
|
|
150
|
-
) {
|
|
151
|
-
try {
|
|
152
|
-
const parsed = Literal.fromUrl(target).get();
|
|
153
|
-
value = parsed.data !== undefined ? parsed.data : parsed;
|
|
154
|
-
} catch {
|
|
155
|
-
// Keep raw value
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
// Primitive type coercion
|
|
159
|
-
else if (typeof target === 'string' && expectedType) {
|
|
160
|
-
if (expectedType === 'number') value = Number(target);
|
|
161
|
-
else if (expectedType === 'boolean') value = target === 'true' || target === '1';
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Transform function
|
|
166
|
-
if (propMeta.transform && typeof propMeta.transform === 'function') {
|
|
167
|
-
value = propMeta.transform(value);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return value;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Hydrates an instance from an array of raw links.
|
|
175
|
-
*
|
|
176
|
-
* Processes properties (latest-wins semantics), relations
|
|
177
|
-
* (chronological accumulation), and timestamps/author in a single
|
|
178
|
-
* pass over the links array.
|
|
179
|
-
*
|
|
180
|
-
* @param instance - The blank model instance to populate
|
|
181
|
-
* @param links - Array of link objects (predicate, target, author?, timestamp?)
|
|
182
|
-
* @param metadata - Model metadata from `getModelMetadata()`
|
|
183
|
-
* @param perspective - The perspective for expression resolution
|
|
184
|
-
* @param requestedProperties - Optional sparse fieldset; when provided, only these
|
|
185
|
-
* property names are hydrated (relations are unaffected).
|
|
186
|
-
* Omit or pass `undefined` to hydrate all properties.
|
|
187
|
-
*/
|
|
188
|
-
export async function hydrateFromLinks(
|
|
189
|
-
instance: any,
|
|
190
|
-
links: Array<{ predicate: string; target: string; author?: string; timestamp?: string | number }>,
|
|
191
|
-
metadata: ModelMetadata,
|
|
192
|
-
perspective: PerspectiveProxy,
|
|
193
|
-
requestedProperties?: string[],
|
|
194
|
-
): Promise<void> {
|
|
195
|
-
if (!links || links.length === 0) return;
|
|
196
|
-
|
|
197
|
-
let minTimestamp: string | number | null = null;
|
|
198
|
-
let maxTimestamp: string | number | null = null;
|
|
199
|
-
let originalAuthor: string | null = null;
|
|
200
|
-
let latestAuthor: string | null = null;
|
|
201
|
-
|
|
202
|
-
// Build predicate→propName and predicate→relName lookup maps for O(1) matching
|
|
203
|
-
// When requestedProperties is provided, only include those properties in the map
|
|
204
|
-
const propFilter = requestedProperties && requestedProperties.length > 0
|
|
205
|
-
? new Set(requestedProperties)
|
|
206
|
-
: null;
|
|
207
|
-
|
|
208
|
-
const predToProperty = new Map<string, [string, PropertyMetadata]>();
|
|
209
|
-
for (const [propName, propMeta] of Object.entries(metadata.properties)) {
|
|
210
|
-
if (propMeta.getter) continue; // Handled via custom getter evaluation
|
|
211
|
-
if (propFilter && !propFilter.has(propName)) continue; // Skip unrequested properties
|
|
212
|
-
predToProperty.set(propMeta.predicate, [propName, propMeta]);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const predToRelation = new Map<string, [string, RelationMetadata]>();
|
|
216
|
-
|
|
217
|
-
// Two-pass approach: first add all non-getter, non-target-filtered relations,
|
|
218
|
-
// then try to add target+filter relations only if their predicate isn't
|
|
219
|
-
// already claimed. This prevents predicate collisions (e.g. entries +
|
|
220
|
-
// messages sharing the same predicate) while still populating unique-
|
|
221
|
-
// predicate relations normally via link hydration.
|
|
222
|
-
const deferredTargetRelations: [string, RelationMetadata][] = [];
|
|
223
|
-
for (const [relName, relMeta] of Object.entries(metadata.relations)) {
|
|
224
|
-
if (relMeta.getter) continue; // Handled via explicit custom getter evaluation
|
|
225
|
-
if (relMeta.target && relMeta.filter !== false) {
|
|
226
|
-
deferredTargetRelations.push([relName, relMeta]);
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
predToRelation.set(relMeta.predicate, [relName, relMeta]);
|
|
230
|
-
}
|
|
231
|
-
// Second pass: add deferred target+filter relations only if their predicate
|
|
232
|
-
// is not already claimed by a base relation. When a collision exists, the
|
|
233
|
-
// conformance getter will handle the filtered view instead.
|
|
234
|
-
for (const [relName, relMeta] of deferredTargetRelations) {
|
|
235
|
-
if (!predToRelation.has(relMeta.predicate)) {
|
|
236
|
-
predToRelation.set(relMeta.predicate, [relName, relMeta]);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// Per-property accumulator: track all matching targets so we can pick the last (latest-wins)
|
|
241
|
-
const propertyLatest = new Map<string, { target: string; timestamp?: string | number }>();
|
|
242
|
-
|
|
243
|
-
// Per-relation accumulator: ordered targets with metadata for sorting
|
|
244
|
-
const relationAccum = new Map<string, Array<{ target: string; timestamp: string | number; index: number }>>();
|
|
245
|
-
|
|
246
|
-
// Single pass over all links
|
|
247
|
-
for (let i = 0; i < links.length; i++) {
|
|
248
|
-
const link = links[i];
|
|
249
|
-
const { predicate, target, author, timestamp } = link;
|
|
250
|
-
if (target === 'None' || target === undefined || target === null) continue;
|
|
251
|
-
|
|
252
|
-
// Track timestamps/authors
|
|
253
|
-
if (timestamp != null) {
|
|
254
|
-
if (minTimestamp == null || timestamp < minTimestamp) {
|
|
255
|
-
minTimestamp = timestamp;
|
|
256
|
-
originalAuthor = author ?? null;
|
|
257
|
-
}
|
|
258
|
-
if (maxTimestamp == null || timestamp > maxTimestamp) {
|
|
259
|
-
maxTimestamp = timestamp;
|
|
260
|
-
latestAuthor = author ?? null;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Property match — accumulate for latest-wins
|
|
265
|
-
const propEntry = predToProperty.get(predicate);
|
|
266
|
-
if (propEntry) {
|
|
267
|
-
const existing = propertyLatest.get(propEntry[0]);
|
|
268
|
-
// Latest-wins: always overwrite (links are ordered ASC so last = latest)
|
|
269
|
-
propertyLatest.set(propEntry[0], { target, timestamp });
|
|
270
|
-
continue;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// Relation match — accumulate all
|
|
274
|
-
const relEntry = predToRelation.get(predicate);
|
|
275
|
-
if (relEntry) {
|
|
276
|
-
const [relName] = relEntry;
|
|
277
|
-
let arr = relationAccum.get(relName);
|
|
278
|
-
if (!arr) {
|
|
279
|
-
arr = [];
|
|
280
|
-
relationAccum.set(relName, arr);
|
|
281
|
-
}
|
|
282
|
-
arr.push({ target, timestamp: timestamp ?? '', index: i });
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// Resolve properties
|
|
287
|
-
for (const [propName, { target }] of propertyLatest) {
|
|
288
|
-
const [, propMeta] = predToProperty.get(
|
|
289
|
-
metadata.properties[propName].predicate
|
|
290
|
-
)!;
|
|
291
|
-
const expectedType = typeof instance[propName];
|
|
292
|
-
instance[propName] = await hydratePropertyValue(
|
|
293
|
-
target,
|
|
294
|
-
propMeta,
|
|
295
|
-
perspective,
|
|
296
|
-
expectedType !== 'undefined' ? expectedType : undefined,
|
|
297
|
-
);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// Resolve relations: sort by timestamp (stable via index tiebreaker), filter empties
|
|
301
|
-
for (const [relName, items] of relationAccum) {
|
|
302
|
-
items.sort((a, b) => {
|
|
303
|
-
const cmp = String(a.timestamp).localeCompare(String(b.timestamp));
|
|
304
|
-
return cmp !== 0 ? cmp : a.index - b.index;
|
|
305
|
-
});
|
|
306
|
-
instance[relName] = items
|
|
307
|
-
.map(i => i.target)
|
|
308
|
-
.filter((v: any) => v !== undefined && v !== null && v !== '' && v !== 'None');
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// Assign author / timestamps
|
|
312
|
-
if (originalAuthor) instance.author = originalAuthor;
|
|
313
|
-
if (minTimestamp != null) {
|
|
314
|
-
instance.createdAt = typeof minTimestamp === 'string' && minTimestamp.includes('T')
|
|
315
|
-
? new Date(minTimestamp).getTime()
|
|
316
|
-
: typeof minTimestamp === 'string'
|
|
317
|
-
? (isNaN(parseInt(minTimestamp, 10)) ? minTimestamp : parseInt(minTimestamp, 10))
|
|
318
|
-
: minTimestamp;
|
|
319
|
-
}
|
|
320
|
-
if (maxTimestamp != null) {
|
|
321
|
-
instance.updatedAt = typeof maxTimestamp === 'string' && maxTimestamp.includes('T')
|
|
322
|
-
? new Date(maxTimestamp).getTime()
|
|
323
|
-
: typeof maxTimestamp === 'string'
|
|
324
|
-
? (isNaN(parseInt(maxTimestamp, 10)) ? maxTimestamp : parseInt(maxTimestamp, 10))
|
|
325
|
-
: maxTimestamp;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// ──────────────────────────────────────────────────────────
|
|
330
|
-
// Prolog result hydration (assignValuesToInstance)
|
|
331
|
-
// ──────────────────────────────────────────────────────────
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Assigns decoded Prolog property values to an instance.
|
|
335
|
-
*
|
|
336
|
-
* Handles UTF-8 byte sequence reconstruction, expression resolution,
|
|
337
|
-
* transform functions, and read-only property filtering.
|
|
338
|
-
*/
|
|
339
|
-
export async function assignValuesToInstance(
|
|
340
|
-
perspective: PerspectiveProxy,
|
|
341
|
-
instance: any,
|
|
342
|
-
values: ValueTuple[],
|
|
343
|
-
): Promise<void> {
|
|
344
|
-
// Map properties to object
|
|
345
|
-
const propsObject = Object.fromEntries(
|
|
346
|
-
await Promise.all(
|
|
347
|
-
values.map(async ([name, value, resolve]) => {
|
|
348
|
-
let finalValue = value;
|
|
349
|
-
|
|
350
|
-
// Handle UTF-8 byte sequences from Prolog URL decoding
|
|
351
|
-
if (!resolve && typeof value === 'string') {
|
|
352
|
-
// Only attempt reconstruction if the string looks like a byte string (all code points <= 0xFF)
|
|
353
|
-
// and contains at least one high byte (>= 0x80). This avoids mangling valid Unicode.
|
|
354
|
-
const codePoints = Array.from(value, ch => ch.codePointAt(0)!);
|
|
355
|
-
const looksByteString = codePoints.every(cp => cp <= 0xFF);
|
|
356
|
-
const hasHighByte = codePoints.some(cp => cp >= 0x80);
|
|
357
|
-
if (looksByteString && hasHighByte) {
|
|
358
|
-
try {
|
|
359
|
-
const bytes = Uint8Array.from(codePoints);
|
|
360
|
-
const decoded = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
361
|
-
if (decoded !== value) finalValue = decoded;
|
|
362
|
-
} catch (error) {
|
|
363
|
-
// If UTF-8 conversion fails, keep the original value
|
|
364
|
-
console.warn(`UTF-8 byte reconstruction failed for property "${name}"`, { value, error });
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
// Resolve the value if necessary
|
|
370
|
-
if (resolve) {
|
|
371
|
-
let resolvedExpression = await perspective.getExpression(value);
|
|
372
|
-
if (resolvedExpression) {
|
|
373
|
-
try {
|
|
374
|
-
// Attempt to parse the data as JSON
|
|
375
|
-
finalValue = JSON.parse(resolvedExpression.data);
|
|
376
|
-
} catch (error) {
|
|
377
|
-
// If parsing fails, keep the original data
|
|
378
|
-
finalValue = resolvedExpression.data;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
// Apply transform function if it exists
|
|
383
|
-
const propsMeta = getPropertiesMetadata(instance.constructor);
|
|
384
|
-
const transform = propsMeta[name]?.transform;
|
|
385
|
-
if (transform && typeof transform === "function") {
|
|
386
|
-
finalValue = transform(finalValue);
|
|
387
|
-
}
|
|
388
|
-
return [name, finalValue];
|
|
389
|
-
})
|
|
390
|
-
)
|
|
391
|
-
);
|
|
392
|
-
// Filter out properties that are read-only (getters without setters)
|
|
393
|
-
const writableProps = Object.fromEntries(
|
|
394
|
-
Object.entries(propsObject).filter(([key]) => {
|
|
395
|
-
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(instance), key);
|
|
396
|
-
if (!descriptor) {
|
|
397
|
-
// No descriptor means it's a regular property on the instance, allow it
|
|
398
|
-
return true;
|
|
399
|
-
}
|
|
400
|
-
// Check if it's an accessor descriptor (has get/set) vs data descriptor (has value/writable)
|
|
401
|
-
const isAccessor = descriptor.get !== undefined || descriptor.set !== undefined;
|
|
402
|
-
if (isAccessor) {
|
|
403
|
-
// Accessor descriptor: only allow if it has a setter
|
|
404
|
-
return descriptor.set !== undefined;
|
|
405
|
-
} else {
|
|
406
|
-
// Data descriptor: only allow if writable is not explicitly false
|
|
407
|
-
return descriptor.writable !== false;
|
|
408
|
-
}
|
|
409
|
-
})
|
|
410
|
-
);
|
|
411
|
-
// Assign properties to instance
|
|
412
|
-
Object.assign(instance, writableProps);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
// ──────────────────────────────────────────────────────────
|
|
416
|
-
// SurrealQL custom getter evaluation
|
|
417
|
-
// ──────────────────────────────────────────────────────────
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* Builds a SurrealQL conformance getter for a relation whose `target` model
|
|
421
|
-
* is known but no explicit `getter` string was supplied.
|
|
422
|
-
*
|
|
423
|
-
* The generated getter traverses outgoing links matching the relation's
|
|
424
|
-
* predicate and then filters the target nodes to only those that conform to
|
|
425
|
-
* the target model's shape (required properties / flags).
|
|
426
|
-
*
|
|
427
|
-
* Delegates to the shared `buildConformanceFilter()` utility in decorators.ts
|
|
428
|
-
* so the same logic is used at shape-definition time and at query time.
|
|
429
|
-
*
|
|
430
|
-
* @param relationPredicate - The relation's predicate URI (e.g. "flux://entry_type")
|
|
431
|
-
* @param targetClass - The target model class (result of calling the `target()` thunk)
|
|
432
|
-
* @returns A SurrealQL expression string, or `undefined` if no conformance
|
|
433
|
-
* conditions could be derived from the target model.
|
|
434
|
-
*/
|
|
435
|
-
export function buildConformanceGetter(
|
|
436
|
-
relationPredicate: string,
|
|
437
|
-
targetClass: any
|
|
438
|
-
): string | undefined {
|
|
439
|
-
const filter = buildConformanceFilter(relationPredicate, targetClass);
|
|
440
|
-
if (!filter) {
|
|
441
|
-
const targetName = targetClass?.prototype?.className || targetClass?.name || 'unknown';
|
|
442
|
-
console.warn(`[Ad4mModel] buildConformanceGetter: no conditions found for target "${targetName}".`);
|
|
443
|
-
return undefined;
|
|
444
|
-
}
|
|
445
|
-
return filter.getter;
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
/**
|
|
449
|
-
* Evaluates custom SurrealQL getters for properties and relations on a specific instance.
|
|
450
|
-
*
|
|
451
|
-
* For relations that declare a `target` but no explicit `getter`, a conformance
|
|
452
|
-
* getter is auto-generated from the target model's metadata (unless `filter: false`).
|
|
453
|
-
*/
|
|
454
|
-
export async function evaluateCustomGettersForInstance(
|
|
455
|
-
instance: any,
|
|
456
|
-
perspective: PerspectiveProxy,
|
|
457
|
-
metadata: ModelMetadata,
|
|
458
|
-
options?: { requestedProperties?: string[]; include?: Record<string, any> }
|
|
459
|
-
): Promise<void> {
|
|
460
|
-
const safeBaseExpression = formatSurrealValue(instance.id);
|
|
461
|
-
|
|
462
|
-
// Build projection filter — when requestedProperties is active, only
|
|
463
|
-
// evaluate getters for fields that are requested (or included).
|
|
464
|
-
const projectionActive = options?.requestedProperties && options.requestedProperties.length > 0;
|
|
465
|
-
const projectionSet = projectionActive ? new Set(options!.requestedProperties) : null;
|
|
466
27
|
|
|
467
|
-
// Evaluate property getters
|
|
468
|
-
for (const [propName, propMeta] of Object.entries(metadata.properties)) {
|
|
469
|
-
if (projectionSet && !projectionSet.has(propName)) continue;
|
|
470
|
-
if ((propMeta as any).getter) {
|
|
471
|
-
try {
|
|
472
|
-
// Replace 'Base' placeholder with actual base expression
|
|
473
|
-
const query = (propMeta as any).getter.replace(/Base/g, safeBaseExpression);
|
|
474
|
-
// Query from node table to have graph traversal context
|
|
475
|
-
const result = await perspective.querySurrealDB(
|
|
476
|
-
`SELECT (${query}) AS value FROM node WHERE uri = ${safeBaseExpression}`
|
|
477
|
-
);
|
|
478
|
-
if (result && result.length > 0 && result[0].value !== undefined && result[0].value !== null && result[0].value !== 'None' && result[0].value !== '') {
|
|
479
|
-
instance[propName] = result[0].value;
|
|
480
|
-
}
|
|
481
|
-
} catch (error) {
|
|
482
|
-
console.warn(`Failed to evaluate getter for ${propName}:`, error);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
// Evaluate relation getters (explicit or auto-generated from target)
|
|
488
|
-
for (const [relName, relMeta] of Object.entries(metadata.relations)) {
|
|
489
|
-
// Skip relations excluded by property projection (unless in include map)
|
|
490
|
-
if (projectionSet && !projectionSet.has(relName) && !(options?.include && relName in options.include)) continue;
|
|
491
|
-
const meta = relMeta as RelationMetadata;
|
|
492
|
-
|
|
493
|
-
// Determine the getter to execute:
|
|
494
|
-
// 1. Explicit `getter` always wins
|
|
495
|
-
// 2. `where` clause → compile DSL to SurrealQL getter
|
|
496
|
-
// 3. If `target` is set and `filter !== false`, auto-generate from target metadata
|
|
497
|
-
// BUT skip auto-generation for reverse relations (belongsToMany / belongsToOne)
|
|
498
|
-
// because buildConformanceGetter traverses outgoing links (->link) which is
|
|
499
|
-
// wrong for reverse relations. Their values are already populated by the
|
|
500
|
-
// reverse link lookup in instancesFromSurrealResult / getData.
|
|
501
|
-
let getter = meta.getter;
|
|
502
|
-
if (!getter && meta.where && meta.direction !== 'reverse') {
|
|
503
|
-
try {
|
|
504
|
-
const TargetClass = meta.target?.();
|
|
505
|
-
const targetMetadata = TargetClass
|
|
506
|
-
? (TargetClass as any).getModelMetadata?.() ?? null
|
|
507
|
-
: null;
|
|
508
|
-
const conditions = compileWhereClause(meta.where, targetMetadata);
|
|
509
|
-
if (conditions.length > 0) {
|
|
510
|
-
const escapedPred = escapeSurrealString(meta.predicate);
|
|
511
|
-
getter = `(->link[WHERE predicate = '${escapedPred}'].out[WHERE ${conditions.join(' AND ')}].uri)`;
|
|
512
|
-
}
|
|
513
|
-
} catch (e) {
|
|
514
|
-
// Target metadata may not be available yet
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
if (!getter && meta.target && meta.filter !== false && meta.direction !== 'reverse') {
|
|
518
|
-
try {
|
|
519
|
-
const TargetClass = meta.target();
|
|
520
|
-
getter = buildConformanceGetter(meta.predicate, TargetClass);
|
|
521
|
-
if (!getter) {
|
|
522
|
-
console.warn(`[Ad4mModel] buildConformanceGetter returned undefined for relation "${relName}" (predicate: "${meta.predicate}")`);
|
|
523
|
-
}
|
|
524
|
-
} catch (e) {
|
|
525
|
-
console.warn(`[Ad4mModel] auto-generation failed for relation "${relName}":`, e);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
if (getter) {
|
|
530
|
-
try {
|
|
531
|
-
// Replace 'Base' placeholder with actual base expression
|
|
532
|
-
const query = getter.replace(/Base/g, safeBaseExpression);
|
|
533
|
-
const fullQuery = `SELECT (${query}) AS value FROM node WHERE uri = ${safeBaseExpression}`;
|
|
534
|
-
// Query from node table to have graph traversal context
|
|
535
|
-
const result = await perspective.querySurrealDB(fullQuery);
|
|
536
|
-
|
|
537
|
-
if (result && result.length > 0 && result[0].value !== undefined && result[0].value !== null) {
|
|
538
|
-
// Filter out 'None' from relation results
|
|
539
|
-
const value = result[0].value;
|
|
540
|
-
instance[relName] = Array.isArray(value)
|
|
541
|
-
? value.filter((v: any) => v !== undefined && v !== null && v !== '' && v !== 'None')
|
|
542
|
-
: value;
|
|
543
|
-
}
|
|
544
|
-
} catch (error) {
|
|
545
|
-
console.warn(`Failed to evaluate getter for ${relName}:`, error);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
/**
|
|
552
|
-
* Hydrates relation fields on instances according to the provided IncludeMap.
|
|
553
|
-
*
|
|
554
|
-
* For each relation listed in `includeMap`, the raw expression-URI strings
|
|
555
|
-
* stored on the instance are replaced with fully-hydrated model instances
|
|
556
|
-
* (fetched via the relation's `target()` class). Nested IncludeMaps are
|
|
557
|
-
* supported for multi-level eager loading.
|
|
558
|
-
*
|
|
559
|
-
* @param modelClass - The model class whose relation metadata to read
|
|
560
|
-
* @param instances - The instances whose relations should be hydrated
|
|
561
|
-
* @param perspective - The perspective to fetch related instances from
|
|
562
|
-
* @param includeMap - Describes which relations to hydrate
|
|
563
|
-
*/
|
|
564
|
-
export async function hydrateRelations<T>(
|
|
565
|
-
modelClass: any,
|
|
566
|
-
instances: T[],
|
|
567
|
-
perspective: PerspectiveProxy,
|
|
568
|
-
includeMap: IncludeMap | undefined,
|
|
569
|
-
): Promise<void> {
|
|
570
|
-
if (!includeMap || Object.keys(includeMap).length === 0) return;
|
|
571
|
-
|
|
572
|
-
const relMeta = getRelationsMetadata(modelClass);
|
|
573
|
-
|
|
574
|
-
for (const [relName, includeValue] of Object.entries(includeMap)) {
|
|
575
|
-
const meta: RelationMetadataEntry | undefined = relMeta[relName];
|
|
576
|
-
if (!meta) {
|
|
577
|
-
console.warn(`include: relation "${relName}" not found in metadata, skipping`);
|
|
578
|
-
continue;
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
const TargetClass = meta.target() as any;
|
|
582
|
-
|
|
583
|
-
// Determine if a RelationSubQuery was supplied (object) vs a plain `true`
|
|
584
|
-
const subQuery: RelationSubQuery | undefined =
|
|
585
|
-
typeof includeValue === 'object' && includeValue !== null
|
|
586
|
-
? (includeValue as RelationSubQuery)
|
|
587
|
-
: undefined;
|
|
588
|
-
const nestedInclude: IncludeMap | undefined = subQuery?.include;
|
|
589
|
-
|
|
590
|
-
// ── Reverse relations (belongsToOne / belongsToMany) ──────────────────
|
|
591
|
-
// The link goes target→instance, so we query backwards:
|
|
592
|
-
// predicate = meta.predicate, target = inst.id → source is the related id
|
|
593
|
-
if (meta.kind === 'belongsToOne' || meta.kind === 'belongsToMany') {
|
|
594
|
-
// Per-instance reverse lookup (can't batch easily across instances)
|
|
595
|
-
for (const inst of instances) {
|
|
596
|
-
const reverseLinks = await perspective.get(
|
|
597
|
-
new LinkQuery({ predicate: meta.predicate, target: (inst as any).id })
|
|
598
|
-
);
|
|
599
|
-
// Defensive filter: perspective.get may return extra results; ensure
|
|
600
|
-
// we only use links that genuinely point to this instance.
|
|
601
|
-
const sourceIds = reverseLinks
|
|
602
|
-
.filter(l => l.data.target === (inst as any).id)
|
|
603
|
-
.map(l => l.data.source);
|
|
604
|
-
|
|
605
|
-
if (meta.kind === 'belongsToOne') {
|
|
606
|
-
if (sourceIds.length === 0) {
|
|
607
|
-
(inst as any)[relName] = null;
|
|
608
|
-
continue;
|
|
609
|
-
}
|
|
610
|
-
const sourceId = sourceIds[sourceIds.length - 1]; // latest-wins
|
|
611
|
-
try {
|
|
612
|
-
const related = new TargetClass(perspective, sourceId);
|
|
613
|
-
await related.get();
|
|
614
|
-
(inst as any)[relName] = related;
|
|
615
|
-
} catch {
|
|
616
|
-
(inst as any)[relName] = null;
|
|
617
|
-
}
|
|
618
|
-
} else {
|
|
619
|
-
// belongsToMany — return array of hydrated instances
|
|
620
|
-
let hydrated: any[] = [];
|
|
621
|
-
|
|
622
|
-
// If there's a where/order sub-query, delegate to findAll for filtering
|
|
623
|
-
if (subQuery && (subQuery.where || subQuery.order || subQuery.properties)) {
|
|
624
|
-
const whereWithIds: Record<string, any> = {
|
|
625
|
-
id: sourceIds,
|
|
626
|
-
...(subQuery.where ?? {}),
|
|
627
|
-
};
|
|
628
|
-
hydrated = await TargetClass.findAll(perspective, {
|
|
629
|
-
where: whereWithIds as any,
|
|
630
|
-
...(subQuery.order && { order: subQuery.order as any }),
|
|
631
|
-
...(subQuery.properties && { properties: subQuery.properties }),
|
|
632
|
-
});
|
|
633
|
-
} else {
|
|
634
|
-
await Promise.all(sourceIds.map(async (sid: string) => {
|
|
635
|
-
try {
|
|
636
|
-
const related = new TargetClass(perspective, sid);
|
|
637
|
-
await related.get(
|
|
638
|
-
subQuery?.properties ? { properties: subQuery.properties } : undefined
|
|
639
|
-
);
|
|
640
|
-
hydrated.push(related);
|
|
641
|
-
} catch { /* skip */ }
|
|
642
|
-
}));
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
// Apply order (client-side, if not already handled by findAll above)
|
|
646
|
-
if (subQuery?.order && !(subQuery.where || subQuery.properties)) {
|
|
647
|
-
const orderEntries = Object.entries(subQuery.order);
|
|
648
|
-
hydrated = hydrated.sort((a: any, b: any) => {
|
|
649
|
-
for (const [field, dir] of orderEntries) {
|
|
650
|
-
const av = String(a[field] ?? '');
|
|
651
|
-
const bv = String(b[field] ?? '');
|
|
652
|
-
const diff = av.localeCompare(bv);
|
|
653
|
-
if (diff !== 0) return dir === 'DESC' ? -diff : diff;
|
|
654
|
-
}
|
|
655
|
-
return 0;
|
|
656
|
-
});
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
// Apply offset and limit
|
|
660
|
-
if (subQuery?.offset != null || subQuery?.limit != null) {
|
|
661
|
-
const start = subQuery.offset ?? 0;
|
|
662
|
-
const end = subQuery.limit != null ? start + subQuery.limit : undefined;
|
|
663
|
-
hydrated = hydrated.slice(start, end);
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
(inst as any)[relName] = hydrated;
|
|
667
|
-
|
|
668
|
-
// Recurse for nested includes
|
|
669
|
-
if (nestedInclude && hydrated.length > 0) {
|
|
670
|
-
await hydrateRelations(TargetClass, hydrated, perspective, nestedInclude);
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
continue; // skip the forward-relation path below
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
// ── Forward relations (hasMany / hasOne) ──────────────────────────────
|
|
678
|
-
// Collect all unique URIs across all instances for batch-friendly lookup.
|
|
679
|
-
// IMPORTANT: We cache each instance's raw value NOW (before the async
|
|
680
|
-
// findAll) so that concurrent getData() calls on the same instance can't
|
|
681
|
-
// overwrite the relation field between the uriSet relation and the
|
|
682
|
-
// post-await assignment loop. Without this cache, a concurrent call
|
|
683
|
-
// can replace the raw URI strings with hydrated model objects, causing
|
|
684
|
-
// the `typeof v === 'string'` check to fail and the array to end up empty.
|
|
685
|
-
const uriSet = new Set<string>();
|
|
686
|
-
const rawCache = new Map<T, any>();
|
|
687
|
-
for (const inst of instances) {
|
|
688
|
-
const raw = (inst as any)[relName];
|
|
689
|
-
rawCache.set(inst, Array.isArray(raw) ? [...raw] : raw);
|
|
690
|
-
if (raw == null) continue;
|
|
691
|
-
if (Array.isArray(raw)) {
|
|
692
|
-
for (const v of raw) {
|
|
693
|
-
if (typeof v === 'string') uriSet.add(v);
|
|
694
|
-
// Handle already-hydrated instances (from a concurrent call)
|
|
695
|
-
else if (v && typeof v === 'object' && typeof v.id === 'string') uriSet.add(v.id);
|
|
696
|
-
}
|
|
697
|
-
} else if (typeof raw === 'string') {
|
|
698
|
-
uriSet.add(raw);
|
|
699
|
-
} else if (raw && typeof raw === 'object' && typeof raw.id === 'string') {
|
|
700
|
-
uriSet.add(raw.id);
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
if (uriSet.size === 0) continue;
|
|
705
|
-
|
|
706
|
-
// Hydrate related instances using findAll to ensure conformance checking.
|
|
707
|
-
// findAll validates model membership via graph traversal (required predicates / flags),
|
|
708
|
-
// so non-conforming linked URIs are silently dropped — matching the documented behaviour.
|
|
709
|
-
const hydrated = new Map<string, any>();
|
|
710
|
-
|
|
711
|
-
const whereWithIds: Record<string, any> = {
|
|
712
|
-
id: Array.from(uriSet),
|
|
713
|
-
...(subQuery?.where ?? {}),
|
|
714
|
-
};
|
|
715
|
-
const fetchQuery: any = {
|
|
716
|
-
where: whereWithIds,
|
|
717
|
-
...(subQuery?.order && { order: subQuery.order }),
|
|
718
|
-
...(subQuery?.properties && { properties: subQuery.properties }),
|
|
719
|
-
};
|
|
720
|
-
const results = await TargetClass.findAll(perspective, fetchQuery);
|
|
721
|
-
for (const result of results) {
|
|
722
|
-
hydrated.set(result.id, result);
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
// Replace raw URIs with hydrated instances on each parent instance.
|
|
726
|
-
// Use the cached raw values captured BEFORE the async findAll to avoid
|
|
727
|
-
// the race condition where a concurrent getData() already replaced the
|
|
728
|
-
// strings with hydrated objects.
|
|
729
|
-
for (const inst of instances) {
|
|
730
|
-
const raw = rawCache.get(inst);
|
|
731
|
-
if (raw == null) continue;
|
|
732
|
-
if (Array.isArray(raw)) {
|
|
733
|
-
// Map URIs → instances; handle both raw strings and already-hydrated objects
|
|
734
|
-
let resolved: any[] = raw
|
|
735
|
-
.map((v: any) => {
|
|
736
|
-
if (typeof v === 'string') {
|
|
737
|
-
return hydrated.has(v) ? hydrated.get(v)! : null;
|
|
738
|
-
}
|
|
739
|
-
// Already a hydrated model instance (from a concurrent call)
|
|
740
|
-
if (v && typeof v === 'object' && typeof v.id === 'string') {
|
|
741
|
-
return hydrated.has(v.id) ? hydrated.get(v.id)! : v;
|
|
742
|
-
}
|
|
743
|
-
return null;
|
|
744
|
-
})
|
|
745
|
-
.filter((v: any): v is any => v !== null);
|
|
746
|
-
|
|
747
|
-
// Per-instance sort (client-side, after filtering)
|
|
748
|
-
if (subQuery?.order) {
|
|
749
|
-
const orderEntries = Object.entries(subQuery.order);
|
|
750
|
-
resolved = resolved.sort((a: any, b: any) => {
|
|
751
|
-
for (const [field, dir] of orderEntries) {
|
|
752
|
-
const av = String(a[field] ?? '');
|
|
753
|
-
const bv = String(b[field] ?? '');
|
|
754
|
-
const diff = av.localeCompare(bv);
|
|
755
|
-
if (diff !== 0) return dir === 'DESC' ? -diff : diff;
|
|
756
|
-
}
|
|
757
|
-
return 0;
|
|
758
|
-
});
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
// Per-instance limit / offset
|
|
762
|
-
if (subQuery?.offset != null || subQuery?.limit != null) {
|
|
763
|
-
const start = subQuery.offset ?? 0;
|
|
764
|
-
const end =
|
|
765
|
-
subQuery.limit != null ? start + subQuery.limit : undefined;
|
|
766
|
-
resolved = resolved.slice(start, end);
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
// Enforce maxCount guard — single-valued relations keep only the last item
|
|
770
|
-
if (meta.maxCount === 1) {
|
|
771
|
-
if (resolved.length > 1) {
|
|
772
|
-
console.warn(
|
|
773
|
-
`include: relation "${relName}" has maxCount 1 but ${resolved.length} values found; keeping the last`,
|
|
774
|
-
);
|
|
775
|
-
}
|
|
776
|
-
(inst as any)[relName] = resolved.length > 0
|
|
777
|
-
? resolved[resolved.length - 1]
|
|
778
|
-
: null;
|
|
779
|
-
} else {
|
|
780
|
-
(inst as any)[relName] = resolved;
|
|
781
|
-
}
|
|
782
|
-
} else if (typeof raw === 'string' && hydrated.has(raw)) {
|
|
783
|
-
(inst as any)[relName] = hydrated.get(raw);
|
|
784
|
-
} else if (raw && typeof raw === 'object' && typeof raw.id === 'string') {
|
|
785
|
-
// Already-hydrated object — look up refreshed version or keep as-is
|
|
786
|
-
(inst as any)[relName] = hydrated.has(raw.id) ? hydrated.get(raw.id) : raw;
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
// Recurse for nested includes
|
|
791
|
-
if (nestedInclude) {
|
|
792
|
-
const hydratedInstances = Array.from(hydrated.values());
|
|
793
|
-
await hydrateRelations(TargetClass, hydratedInstances, perspective, nestedInclude);
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
}
|