@coasys/ad4m 0.13.0-test-6 → 0.13.0-test-8
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 +1103 -747
- package/lib/index.js +1090 -747
- package/lib/src/Ad4mClient.d.ts +4 -1
- package/lib/src/Ad4mClient.js +2 -2
- package/lib/src/agent/AgentClient.js +17 -0
- package/lib/src/ai/AIClient.js +1 -1
- package/lib/src/apiClient.d.ts +5 -1
- package/lib/src/apiClient.js +14 -6
- package/lib/src/generated/rest/AIModelLoadingStatus.d.ts +7 -0
- package/lib/src/generated/rest/AIModelLoadingStatus.js +2 -0
- package/lib/src/generated/rest/AddAgentInfosRequest.d.ts +3 -0
- package/lib/src/generated/rest/AddAgentInfosRequest.js +2 -0
- package/lib/src/generated/rest/AddLinkExpressionRequest.d.ts +6 -0
- package/lib/src/generated/rest/AddLinkExpressionRequest.js +1 -0
- package/lib/src/generated/rest/AddLinkRequest.d.ts +6 -0
- package/lib/src/generated/rest/AddLinkRequest.js +1 -0
- package/lib/src/generated/rest/AddLinksBulkRequest.d.ts +6 -0
- package/lib/src/generated/rest/AddLinksBulkRequest.js +1 -0
- package/lib/src/generated/rest/AddSdnaRequest.d.ts +6 -0
- package/lib/src/generated/rest/AddSdnaRequest.js +2 -0
- package/lib/src/generated/rest/ApplyTemplateRequest.d.ts +4 -0
- package/lib/src/generated/rest/ApplyTemplateRequest.js +2 -0
- package/lib/src/generated/rest/AuthInfoInput.d.ts +10 -0
- package/lib/src/generated/rest/AuthInfoInput.js +1 -0
- package/lib/src/generated/rest/BroadcastRequest.d.ts +6 -0
- package/lib/src/generated/rest/BroadcastRequest.js +1 -0
- package/lib/src/generated/rest/CapabilityInput.d.ts +5 -0
- package/lib/src/generated/rest/CapabilityInput.js +1 -0
- package/lib/src/generated/rest/CommitBatchRequest.d.ts +3 -0
- package/lib/src/generated/rest/CommitBatchRequest.js +2 -0
- package/lib/src/generated/rest/ComputeLogEntry.d.ts +9 -0
- package/lib/src/generated/rest/ComputeLogEntry.js +2 -0
- package/lib/src/generated/rest/CreateExpressionRequest.d.ts +4 -0
- package/lib/src/generated/rest/CreateExpressionRequest.js +2 -0
- package/lib/src/generated/rest/CreatePerspectiveRequest.d.ts +3 -0
- package/lib/src/generated/rest/CreatePerspectiveRequest.js +2 -0
- package/lib/src/generated/rest/CreateSubjectRequest.d.ts +6 -0
- package/lib/src/generated/rest/CreateSubjectRequest.js +2 -0
- package/lib/src/generated/rest/CreateUserRequest.d.ts +4 -0
- package/lib/src/generated/rest/CreateUserRequest.js +2 -0
- package/lib/src/generated/rest/CreateUserResponse.d.ts +5 -0
- package/lib/src/generated/rest/CreateUserResponse.js +2 -0
- package/lib/src/generated/rest/DecoratedExpressionProof.d.ts +6 -0
- package/lib/src/generated/rest/DecoratedExpressionProof.js +2 -0
- package/lib/src/generated/rest/DecoratedLinkExpression.d.ts +10 -0
- package/lib/src/generated/rest/DecoratedLinkExpression.js +1 -0
- package/lib/src/generated/rest/DisposeQueryRequest.d.ts +3 -0
- package/lib/src/generated/rest/DisposeQueryRequest.js +2 -0
- package/lib/src/generated/rest/EmailTestRequest.d.ts +6 -0
- package/lib/src/generated/rest/EmailTestRequest.js +2 -0
- package/lib/src/generated/rest/EmbedRequest.d.ts +4 -0
- package/lib/src/generated/rest/EmbedRequest.js +2 -0
- package/lib/src/generated/rest/EntanglementProofInput.d.ts +6 -0
- package/lib/src/generated/rest/EntanglementProofInput.js +2 -0
- package/lib/src/generated/rest/EntanglementProofPreflightRequest.d.ts +4 -0
- package/lib/src/generated/rest/EntanglementProofPreflightRequest.js +2 -0
- package/lib/src/generated/rest/ExecuteCommandsRequest.d.ts +4 -0
- package/lib/src/generated/rest/ExecuteCommandsRequest.js +2 -0
- package/lib/src/generated/rest/ExportRequest.d.ts +5 -0
- package/lib/src/generated/rest/ExportRequest.js +2 -0
- package/lib/src/generated/rest/ExpressionManyRequest.d.ts +3 -0
- package/lib/src/generated/rest/ExpressionManyRequest.js +2 -0
- package/lib/src/generated/rest/ExpressionProof.d.ts +4 -0
- package/lib/src/generated/rest/ExpressionProof.js +2 -0
- package/lib/src/generated/rest/ExpressionProofInput.d.ts +6 -0
- package/lib/src/generated/rest/ExpressionProofInput.js +2 -0
- package/lib/src/generated/rest/FriendSendMessageRequest.d.ts +3 -0
- package/lib/src/generated/rest/FriendSendMessageRequest.js +2 -0
- package/lib/src/generated/rest/FriendsListRequest.d.ts +3 -0
- package/lib/src/generated/rest/FriendsListRequest.js +2 -0
- package/lib/src/generated/rest/GenerateAgentRequest.d.ts +3 -0
- package/lib/src/generated/rest/GenerateAgentRequest.js +2 -0
- package/lib/src/generated/rest/GenerateJwtRequest.d.ts +4 -0
- package/lib/src/generated/rest/GenerateJwtRequest.js +2 -0
- package/lib/src/generated/rest/GetSubjectDataRequest.d.ts +4 -0
- package/lib/src/generated/rest/GetSubjectDataRequest.js +2 -0
- package/lib/src/generated/rest/HostingInfoResponse.d.ts +5 -0
- package/lib/src/generated/rest/HostingInfoResponse.js +2 -0
- package/lib/src/generated/rest/HostingWalletResponse.d.ts +4 -0
- package/lib/src/generated/rest/HostingWalletResponse.js +2 -0
- package/lib/src/generated/rest/ImportAgentRequest.d.ts +6 -0
- package/lib/src/generated/rest/ImportAgentRequest.js +2 -0
- package/lib/src/generated/rest/ImportRequest.d.ts +4 -0
- package/lib/src/generated/rest/ImportRequest.js +2 -0
- package/lib/src/generated/rest/JoinNeighbourhoodRequest.d.ts +3 -0
- package/lib/src/generated/rest/JoinNeighbourhoodRequest.js +2 -0
- package/lib/src/generated/rest/KeepAliveQueryRequest.d.ts +3 -0
- package/lib/src/generated/rest/KeepAliveQueryRequest.js +2 -0
- package/lib/src/generated/rest/LanguageMetaInput.d.ts +6 -0
- package/lib/src/generated/rest/LanguageMetaInput.js +2 -0
- package/lib/src/generated/rest/Link.d.ts +5 -0
- package/lib/src/generated/rest/Link.js +2 -0
- package/lib/src/generated/rest/LinkExpression.d.ts +10 -0
- package/lib/src/generated/rest/LinkExpression.js +1 -0
- package/lib/src/generated/rest/LinkExpressionInput.d.ts +10 -0
- package/lib/src/generated/rest/LinkExpressionInput.js +1 -0
- package/lib/src/generated/rest/LinkInput.d.ts +5 -0
- package/lib/src/generated/rest/LinkInput.js +2 -0
- package/lib/src/generated/rest/LinkLanguageTemplatesRequest.d.ts +3 -0
- package/lib/src/generated/rest/LinkLanguageTemplatesRequest.js +2 -0
- package/lib/src/generated/rest/LinkMutationRequest.d.ts +12 -0
- package/lib/src/generated/rest/LinkMutationRequest.js +1 -0
- package/lib/src/generated/rest/LinkMutationResponse.d.ts +9 -0
- package/lib/src/generated/rest/LinkMutationResponse.js +1 -0
- package/lib/src/generated/rest/LinkMutations.d.ts +6 -0
- package/lib/src/generated/rest/LinkMutations.js +1 -0
- package/lib/src/generated/rest/LinkMutationsRequest.d.ts +5 -0
- package/lib/src/generated/rest/LinkMutationsRequest.js +1 -0
- package/lib/src/generated/rest/LinkStatus.d.ts +1 -0
- package/lib/src/generated/rest/LinkStatus.js +2 -0
- package/lib/src/generated/rest/LinkUpdateInput.d.ts +5 -0
- package/lib/src/generated/rest/LinkUpdateInput.js +1 -0
- package/lib/src/generated/rest/LockAgentRequest.d.ts +3 -0
- package/lib/src/generated/rest/LockAgentRequest.js +2 -0
- package/lib/src/generated/rest/LoginUserRequest.d.ts +5 -0
- package/lib/src/generated/rest/LoginUserRequest.js +2 -0
- package/lib/src/generated/rest/ModelType.d.ts +1 -0
- package/lib/src/generated/rest/ModelType.js +2 -0
- package/lib/src/generated/rest/NotificationGrantRequest.d.ts +3 -0
- package/lib/src/generated/rest/NotificationGrantRequest.js +2 -0
- package/lib/src/generated/rest/NotificationInput.d.ts +10 -0
- package/lib/src/generated/rest/NotificationInput.js +2 -0
- package/lib/src/generated/rest/OpenLinkRequest.d.ts +3 -0
- package/lib/src/generated/rest/OpenLinkRequest.js +2 -0
- package/lib/src/generated/rest/PermitCapabilityRequest.d.ts +3 -0
- package/lib/src/generated/rest/PermitCapabilityRequest.js +2 -0
- package/lib/src/generated/rest/PerspectiveInput.d.ts +4 -0
- package/lib/src/generated/rest/PerspectiveInput.js +1 -0
- package/lib/src/generated/rest/PromptRequest.d.ts +4 -0
- package/lib/src/generated/rest/PromptRequest.js +2 -0
- package/lib/src/generated/rest/PublicPerspectiveInput.d.ts +4 -0
- package/lib/src/generated/rest/PublicPerspectiveInput.js +1 -0
- package/lib/src/generated/rest/PublishLanguageRequest.d.ts +5 -0
- package/lib/src/generated/rest/PublishLanguageRequest.js +1 -0
- package/lib/src/generated/rest/PublishNeighbourhoodRequest.d.ts +6 -0
- package/lib/src/generated/rest/PublishNeighbourhoodRequest.js +1 -0
- package/lib/src/generated/rest/QueryRequest.d.ts +7 -0
- package/lib/src/generated/rest/QueryRequest.js +2 -0
- package/lib/src/generated/rest/RemoveLinkRequest.d.ts +5 -0
- package/lib/src/generated/rest/RemoveLinkRequest.js +1 -0
- package/lib/src/generated/rest/RemoveLinksBulkRequest.d.ts +5 -0
- package/lib/src/generated/rest/RemoveLinksBulkRequest.js +1 -0
- package/lib/src/generated/rest/RequestCapabilityRequest.d.ts +4 -0
- package/lib/src/generated/rest/RequestCapabilityRequest.js +1 -0
- package/lib/src/generated/rest/RequestPaymentRequest.d.ts +3 -0
- package/lib/src/generated/rest/RequestPaymentRequest.js +2 -0
- package/lib/src/generated/rest/RequestVerificationRequest.d.ts +4 -0
- package/lib/src/generated/rest/RequestVerificationRequest.js +2 -0
- package/lib/src/generated/rest/ResourceInput.d.ts +4 -0
- package/lib/src/generated/rest/ResourceInput.js +2 -0
- package/lib/src/generated/rest/SetDefaultModelRequest.d.ts +4 -0
- package/lib/src/generated/rest/SetDefaultModelRequest.js +1 -0
- package/lib/src/generated/rest/SetFreeHostingEnabledRequest.d.ts +3 -0
- package/lib/src/generated/rest/SetFreeHostingEnabledRequest.js +2 -0
- package/lib/src/generated/rest/SetHotWalletAddressRequest.d.ts +3 -0
- package/lib/src/generated/rest/SetHotWalletAddressRequest.js +2 -0
- package/lib/src/generated/rest/SetMultiUserRequest.d.ts +3 -0
- package/lib/src/generated/rest/SetMultiUserRequest.js +2 -0
- package/lib/src/generated/rest/SetOnlineStatusRequest.d.ts +5 -0
- package/lib/src/generated/rest/SetOnlineStatusRequest.js +1 -0
- package/lib/src/generated/rest/SetStatusRequest.d.ts +3 -0
- package/lib/src/generated/rest/SetStatusRequest.js +2 -0
- package/lib/src/generated/rest/SetUserFreeAccessRequest.d.ts +4 -0
- package/lib/src/generated/rest/SetUserFreeAccessRequest.js +2 -0
- package/lib/src/generated/rest/SignMessageRequest.d.ts +3 -0
- package/lib/src/generated/rest/SignMessageRequest.js +2 -0
- package/lib/src/generated/rest/SignalRequest.d.ts +6 -0
- package/lib/src/generated/rest/SignalRequest.js +1 -0
- package/lib/src/generated/rest/SubscribeQueryRequest.d.ts +3 -0
- package/lib/src/generated/rest/SubscribeQueryRequest.js +2 -0
- package/lib/src/generated/rest/SubscribeQueryResponse.d.ts +4 -0
- package/lib/src/generated/rest/SubscribeQueryResponse.js +2 -0
- package/lib/src/generated/rest/TrustedAgentsRequest.d.ts +3 -0
- package/lib/src/generated/rest/TrustedAgentsRequest.js +2 -0
- package/lib/src/generated/rest/UnlockAgentRequest.d.ts +4 -0
- package/lib/src/generated/rest/UnlockAgentRequest.js +2 -0
- package/lib/src/generated/rest/UpdateLinkRequest.d.ts +7 -0
- package/lib/src/generated/rest/UpdateLinkRequest.js +1 -0
- package/lib/src/generated/rest/UpdatePerspectiveRequest.d.ts +3 -0
- package/lib/src/generated/rest/UpdatePerspectiveRequest.js +2 -0
- package/lib/src/generated/rest/UpdateProfileRequest.d.ts +5 -0
- package/lib/src/generated/rest/UpdateProfileRequest.js +1 -0
- package/lib/src/generated/rest/UserStatistics.d.ts +9 -0
- package/lib/src/generated/rest/UserStatistics.js +2 -0
- package/lib/src/generated/rest/VerificationRequestResult.d.ts +6 -0
- package/lib/src/generated/rest/VerificationRequestResult.js +2 -0
- package/lib/src/generated/rest/VerifyEmailRequest.d.ts +6 -0
- package/lib/src/generated/rest/VerifyEmailRequest.js +2 -0
- package/lib/src/generated/rest/VerifySignatureRequest.d.ts +5 -0
- package/lib/src/generated/rest/VerifySignatureRequest.js +2 -0
- package/lib/src/generated/rest/WriteSettingsRequest.d.ts +3 -0
- package/lib/src/generated/rest/WriteSettingsRequest.js +2 -0
- package/lib/src/generated/rest/index.d.ts +73 -0
- package/lib/src/generated/rest/index.js +8 -0
- package/lib/src/generated/rest/routes.d.ts +587 -0
- package/lib/src/generated/rest/routes.js +3 -0
- package/lib/src/index.d.ts +4 -0
- package/lib/src/index.js +4 -0
- package/lib/src/model/Ad4mModel.d.ts +30 -21
- package/lib/src/model/Ad4mModel.js +93 -316
- package/lib/src/model/Ad4mModel.test.js +1536 -535
- package/lib/src/model/ModelQueryBuilder.d.ts +5 -10
- package/lib/src/model/ModelQueryBuilder.js +136 -47
- package/lib/src/model/Subject.js +6 -6
- package/lib/src/model/decorators.d.ts +7 -4
- package/lib/src/model/decorators.js +0 -7
- 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/query-cache.test.d.ts +1 -0
- package/lib/src/model/query-cache.test.js +43 -0
- 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 +0 -10
- package/lib/src/model/query-sparql.js +10 -15
- package/lib/src/model/query-sparql.test.d.ts +1 -0
- package/lib/src/model/query-sparql.test.js +230 -0
- package/lib/src/model/relation-filtering.test.js +15 -15
- package/lib/src/model/shacl-gen.js +120 -47
- 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 +140 -4
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +33 -33
- package/lib/src/perspectives/PerspectiveClient.d.ts +13 -4
- package/lib/src/perspectives/PerspectiveClient.js +22 -6
- package/lib/src/perspectives/PerspectiveClient.test.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveClient.test.js +224 -0
- package/lib/src/perspectives/PerspectiveProxy.d.ts +27 -7
- package/lib/src/perspectives/PerspectiveProxy.js +60 -12
- package/lib/src/perspectives/PerspectiveProxy.test.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveProxy.test.js +34 -0
- 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.js +5 -4
- package/lib/src/restClient.d.ts +52 -0
- package/lib/src/restClient.js +227 -0
- package/lib/src/runtime/RuntimeResolver.d.ts +28 -1
- package/lib/src/runtime/RuntimeResolver.js +197 -4
- package/lib/src/schema.gql +27 -1
- package/lib/src/shacl/NodeExpression.d.ts +44 -0
- package/lib/src/shacl/NodeExpression.js +39 -0
- package/lib/src/shacl/SHACLShape.d.ts +23 -0
- package/lib/src/shacl/SHACLShape.js +115 -1
- package/lib/src/shacl/builders.d.ts +58 -0
- package/lib/src/shacl/builders.js +76 -0
- package/lib/src/shacl/index.d.ts +36 -0
- package/lib/src/shacl/index.js +33 -0
- package/package.json +1 -1
- package/src/Ad4mClient.ts +7 -2
- package/src/agent/AgentClient.ts +23 -0
- package/src/ai/AIClient.ts +1 -1
- package/src/apiClient.ts +17 -6
- package/src/index.ts +4 -0
- package/src/model/Ad4mModel.test.ts +274 -72
- package/src/model/Ad4mModel.ts +141 -350
- package/src/model/ModelQueryBuilder.ts +131 -56
- package/src/model/decorators.ts +7 -13
- package/src/model/query-sparql.test.ts +35 -19
- package/src/model/query-sparql.ts +10 -16
- package/src/model/relation-filtering.test.ts +46 -39
- package/src/model/shacl-gen.ts +128 -54
- package/src/model/shacl-roundtrip.test.ts +197 -0
- package/src/model/types-typed-query.test.ts +360 -0
- package/src/model/types.ts +196 -4
- package/src/perspectives/PerspectiveClient.ts +24 -8
- package/src/perspectives/PerspectiveProxy.test.ts +85 -1
- package/src/perspectives/PerspectiveProxy.ts +65 -20
- package/src/perspectives/SparqlBindings.ts +79 -0
- package/src/perspectives/WakerSubscriptionManager.ts +5 -4
- package/src/shacl/NodeExpression.ts +54 -0
- package/src/shacl/SHACLShape.test.ts +172 -0
- package/src/shacl/SHACLShape.ts +173 -1
- package/src/shacl/builders.ts +86 -0
- package/src/shacl/index.ts +72 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batch SPARQL query builder for Ad4mModel eager-loading (includes).
|
|
3
|
+
*
|
|
4
|
+
* Uses direct triple + named graph storage model.
|
|
5
|
+
* Each AD4M link is stored as: <source> <predicate> <target> in a named graph
|
|
6
|
+
* with metadata in the default graph keyed by graph IRI.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { resolveParentPredicate } from "./query-common";
|
|
11
|
+
import { getRelationsMetadata } from "./decorators";
|
|
12
|
+
import { Literal } from "../Literal";
|
|
13
|
+
// ──────────────────────────────────────────────────────────
|
|
14
|
+
// Helpers
|
|
15
|
+
// ──────────────────────────────────────────────────────────
|
|
16
|
+
function iri(value) {
|
|
17
|
+
return `<${value}>`;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Check if a string value looks like a URI (has a scheme).
|
|
21
|
+
*/
|
|
22
|
+
function looksLikeUri(value) {
|
|
23
|
+
return /^[a-zA-Z][a-zA-Z0-9+\-._]*:/.test(value);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Convert a JS value to its literal: IRI form.
|
|
27
|
+
*/
|
|
28
|
+
function valueToLiteralIri(value) {
|
|
29
|
+
if (typeof value === 'string') {
|
|
30
|
+
if (looksLikeUri(value))
|
|
31
|
+
return value;
|
|
32
|
+
return Literal.from(value).toUrl();
|
|
33
|
+
}
|
|
34
|
+
if (typeof value === 'number')
|
|
35
|
+
return Literal.from(value).toUrl();
|
|
36
|
+
if (typeof value === 'boolean')
|
|
37
|
+
return Literal.from(value).toUrl();
|
|
38
|
+
return Literal.from(String(value)).toUrl();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Determine if a property stores values as literal: IRIs.
|
|
42
|
+
*/
|
|
43
|
+
function isLiteralStoredProperty(propMeta) {
|
|
44
|
+
if (propMeta.flag)
|
|
45
|
+
return false;
|
|
46
|
+
if (propMeta.resolveLanguage && propMeta.resolveLanguage !== "literal")
|
|
47
|
+
return false;
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
function buildConformanceJoins(metadata, sourceVar, prefix) {
|
|
51
|
+
const joins = [];
|
|
52
|
+
const filters = [];
|
|
53
|
+
let hasConformance = false;
|
|
54
|
+
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
55
|
+
if (propMeta.required) {
|
|
56
|
+
if (propMeta.getter)
|
|
57
|
+
continue;
|
|
58
|
+
hasConformance = true;
|
|
59
|
+
if (propMeta.flag && propMeta.initial) {
|
|
60
|
+
joins.push(`
|
|
61
|
+
${sourceVar} ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
joins.push(`
|
|
65
|
+
${sourceVar} ${iri(propMeta.predicate)} ?${prefix}_cfTarget_${propMeta.name} .`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (!hasConformance) {
|
|
70
|
+
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
71
|
+
if (propMeta.initial) {
|
|
72
|
+
if (propMeta.flag) {
|
|
73
|
+
joins.push(`
|
|
74
|
+
${sourceVar} ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
joins.push(`
|
|
78
|
+
${sourceVar} ${iri(propMeta.predicate)} ?${prefix}_cfInitAny_${propMeta.name} .`);
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return { joins, filters };
|
|
85
|
+
}
|
|
86
|
+
function flattenIncludeTree(parentDepth, parentClass, includeMap, nextDepth) {
|
|
87
|
+
const branches = [];
|
|
88
|
+
const relMeta = getRelationsMetadata(parentClass);
|
|
89
|
+
for (const [relName, includeValue] of Object.entries(includeMap)) {
|
|
90
|
+
const meta = relMeta[relName];
|
|
91
|
+
if (!meta || !meta.target)
|
|
92
|
+
continue;
|
|
93
|
+
const TargetClass = meta.target();
|
|
94
|
+
const targetMetadata = TargetClass.getModelMetadata
|
|
95
|
+
? TargetClass.getModelMetadata()
|
|
96
|
+
: { className: "Unknown", properties: {}, relations: {} };
|
|
97
|
+
const depth = nextDepth.value++;
|
|
98
|
+
const direction = meta.kind === 'belongsToOne' || meta.kind === 'belongsToMany'
|
|
99
|
+
? 'reverse' : 'forward';
|
|
100
|
+
const nestedInclude = typeof includeValue === 'object' && includeValue !== null
|
|
101
|
+
? includeValue.include
|
|
102
|
+
: undefined;
|
|
103
|
+
branches.push({
|
|
104
|
+
depth,
|
|
105
|
+
relationName: relName,
|
|
106
|
+
parentDepth,
|
|
107
|
+
parentPredicate: meta.predicate,
|
|
108
|
+
targetClass: TargetClass,
|
|
109
|
+
targetMetadata,
|
|
110
|
+
includeMap: nestedInclude,
|
|
111
|
+
direction,
|
|
112
|
+
});
|
|
113
|
+
if (nestedInclude) {
|
|
114
|
+
branches.push(...flattenIncludeTree(depth, TargetClass, nestedInclude, nextDepth));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return branches;
|
|
118
|
+
}
|
|
119
|
+
// ──────────────────────────────────────────────────────────
|
|
120
|
+
// Main entry point
|
|
121
|
+
// ──────────────────────────────────────────────────────────
|
|
122
|
+
/**
|
|
123
|
+
* Build a single SPARQL query that fetches root instances plus all
|
|
124
|
+
* included relations in one query using UNION branches.
|
|
125
|
+
*
|
|
126
|
+
* Uses direct triple patterns in named graphs with metadata in default graph.
|
|
127
|
+
*/
|
|
128
|
+
export function buildBatchSPARQLQuery(rootMetadata, query, rootModelClass) {
|
|
129
|
+
const includeMap = query.include;
|
|
130
|
+
if (!includeMap || Object.keys(includeMap).length === 0) {
|
|
131
|
+
throw new Error("buildBatchSPARQLQuery requires query.include to be non-empty");
|
|
132
|
+
}
|
|
133
|
+
const nextDepth = { value: 1 };
|
|
134
|
+
const branches = flattenIncludeTree(0, rootModelClass, includeMap, nextDepth);
|
|
135
|
+
// Root conformance
|
|
136
|
+
const rootConformance = buildConformanceJoins(rootMetadata, "?source", "root");
|
|
137
|
+
const rootJoins = [...rootConformance.joins];
|
|
138
|
+
const rootFilters = [...rootConformance.filters];
|
|
139
|
+
if (query.parent) {
|
|
140
|
+
const parentPredicate = resolveParentPredicate(query.parent, rootModelClass);
|
|
141
|
+
rootJoins.push(`
|
|
142
|
+
${iri(query.parent.id)} ${iri(parentPredicate)} ?source .`);
|
|
143
|
+
}
|
|
144
|
+
if (query.where) {
|
|
145
|
+
for (const [propertyName, condition] of Object.entries(query.where)) {
|
|
146
|
+
if (propertyName === "base" || propertyName === "id") {
|
|
147
|
+
if (Array.isArray(condition)) {
|
|
148
|
+
const formatted = condition.map(v => iri(v)).join(", ");
|
|
149
|
+
rootFilters.push(`?source IN (${formatted})`);
|
|
150
|
+
}
|
|
151
|
+
else if (typeof condition === "string") {
|
|
152
|
+
rootFilters.push(`?source = ${iri(condition)}`);
|
|
153
|
+
}
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (propertyName === "author" || propertyName === "timestamp")
|
|
157
|
+
continue;
|
|
158
|
+
const propMeta = rootMetadata.properties[propertyName];
|
|
159
|
+
if (!propMeta)
|
|
160
|
+
continue;
|
|
161
|
+
const useParseLiteral = isLiteralStoredProperty(propMeta);
|
|
162
|
+
if (typeof condition === "string" || typeof condition === "number" || typeof condition === "boolean") {
|
|
163
|
+
if (useParseLiteral) {
|
|
164
|
+
// For literal-stored properties, just add a join for conformance — filtering is done in JS
|
|
165
|
+
rootJoins.push(`
|
|
166
|
+
?source ${iri(propMeta.predicate)} ?root_wTarget_eq_${propertyName} .`);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
rootJoins.push(`
|
|
170
|
+
?source ${iri(propMeta.predicate)} ${iri(String(condition))} .`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else if (Array.isArray(condition)) {
|
|
174
|
+
if (useParseLiteral) {
|
|
175
|
+
// For literal-stored properties, just add a join for conformance — filtering is done in JS
|
|
176
|
+
rootJoins.push(`
|
|
177
|
+
?source ${iri(propMeta.predicate)} ?root_wTarget_${propertyName} .`);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
const formatted = condition.map(v => iri(v)).join(", ");
|
|
181
|
+
rootJoins.push(`
|
|
182
|
+
?source ${iri(propMeta.predicate)} ?root_wTarget_${propertyName} .`);
|
|
183
|
+
rootFilters.push(`?root_wTarget_${propertyName} IN (${formatted})`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const rootJoinClause = rootJoins.join("\n");
|
|
189
|
+
const rootFilterClause = rootFilters.length > 0
|
|
190
|
+
? `FILTER(\n ${rootFilters.join(" &&\n ")}\n )`
|
|
191
|
+
: "";
|
|
192
|
+
const unionBranches = [];
|
|
193
|
+
// Depth 0: root — direct triple pattern with named graph
|
|
194
|
+
unionBranches.push(`
|
|
195
|
+
{
|
|
196
|
+
${rootJoinClause}
|
|
197
|
+
GRAPH ?linkGraph { ?source ?predicate ?target . }
|
|
198
|
+
FILTER(isIRI(?source) && isIRI(?predicate))
|
|
199
|
+
?linkGraph <ad4m://ontology/author> ?author .
|
|
200
|
+
?linkGraph <ad4m://ontology/timestamp> ?timestamp .
|
|
201
|
+
${rootFilterClause}
|
|
202
|
+
BIND("0" AS ?depth)
|
|
203
|
+
BIND("" AS ?parentBase)
|
|
204
|
+
BIND("" AS ?relationName)
|
|
205
|
+
}
|
|
206
|
+
`);
|
|
207
|
+
// Depth N: include branches
|
|
208
|
+
for (const branch of branches) {
|
|
209
|
+
const childConformance = buildConformanceJoins(branch.targetMetadata, "?source", `d${branch.depth}`);
|
|
210
|
+
const childJoinClause = childConformance.joins.join("\n");
|
|
211
|
+
const childFilterClause = childConformance.filters.length > 0
|
|
212
|
+
? `FILTER(\n ${childConformance.filters.join(" &&\n ")}\n )`
|
|
213
|
+
: "";
|
|
214
|
+
const parentJoinClause = branch.parentDepth === 0 ? rootJoinClause.replace(/\?source/g, '?parentBase') : '';
|
|
215
|
+
const parentFilterClause = branch.parentDepth === 0 ? rootFilterClause.replace(/\?source/g, '?parentBase') : '';
|
|
216
|
+
if (branch.direction === 'forward') {
|
|
217
|
+
// Forward: parent --predicate--> child (source)
|
|
218
|
+
unionBranches.push(`
|
|
219
|
+
{
|
|
220
|
+
# Depth ${branch.depth}: ${branch.relationName} (forward)
|
|
221
|
+
?parentBase ${iri(branch.parentPredicate)} ?source .
|
|
222
|
+
${childJoinClause}
|
|
223
|
+
GRAPH ?linkGraph { ?source ?predicate ?target . }
|
|
224
|
+
FILTER(isIRI(?source) && isIRI(?predicate))
|
|
225
|
+
?linkGraph <ad4m://ontology/author> ?author .
|
|
226
|
+
?linkGraph <ad4m://ontology/timestamp> ?timestamp .
|
|
227
|
+
${childFilterClause}
|
|
228
|
+
${parentJoinClause}
|
|
229
|
+
${parentFilterClause}
|
|
230
|
+
BIND("${branch.depth}" AS ?depth)
|
|
231
|
+
BIND("${branch.relationName}" AS ?relationName)
|
|
232
|
+
}
|
|
233
|
+
`);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
// Reverse: child (source) --predicate--> parent
|
|
237
|
+
unionBranches.push(`
|
|
238
|
+
{
|
|
239
|
+
# Depth ${branch.depth}: ${branch.relationName} (reverse)
|
|
240
|
+
?source ${iri(branch.parentPredicate)} ?parentBase .
|
|
241
|
+
${childJoinClause}
|
|
242
|
+
GRAPH ?linkGraph { ?source ?predicate ?target . }
|
|
243
|
+
FILTER(isIRI(?source) && isIRI(?predicate))
|
|
244
|
+
?linkGraph <ad4m://ontology/author> ?author .
|
|
245
|
+
?linkGraph <ad4m://ontology/timestamp> ?timestamp .
|
|
246
|
+
${childFilterClause}
|
|
247
|
+
${parentJoinClause}
|
|
248
|
+
${parentFilterClause}
|
|
249
|
+
BIND("${branch.depth}" AS ?depth)
|
|
250
|
+
BIND("${branch.relationName}" AS ?relationName)
|
|
251
|
+
}
|
|
252
|
+
`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return `
|
|
256
|
+
SELECT ?depth ?parentBase ?relationName ?source ?predicate ?target ?author ?timestamp WHERE {
|
|
257
|
+
${unionBranches.join("\n UNION\n")}
|
|
258
|
+
}
|
|
259
|
+
`.trim();
|
|
260
|
+
}
|
|
@@ -38,16 +38,6 @@ export declare function formatSPARQLValue(value: any): string;
|
|
|
38
38
|
* with named graph storage and metadata keyed by graph IRI.
|
|
39
39
|
*/
|
|
40
40
|
export declare function buildSPARQLQuery(metadata: ModelMetadata, allRelationsMetadata: Record<string, RelationMetadataEntry>, query: Query, modelClass: any): string;
|
|
41
|
-
/**
|
|
42
|
-
* Build ORDER BY / LIMIT / OFFSET clauses for the SPARQL query.
|
|
43
|
-
*
|
|
44
|
-
* NOTE: The outer SELECT returns multiple rows per instance (one per link),
|
|
45
|
-
* so LIMIT/OFFSET at the outer level would cut off links mid-instance.
|
|
46
|
-
* This function is kept for backward compatibility and always returns "".
|
|
47
|
-
* SPARQL-level pagination is handled via a subquery in buildSPARQLQuery
|
|
48
|
-
* using buildPaginationSubquery().
|
|
49
|
-
*/
|
|
50
|
-
export declare function buildSPARQLOrderLimitOffset(_metadata: ModelMetadata, query: Query): string;
|
|
51
41
|
/**
|
|
52
42
|
* Build an inner subquery that selects DISTINCT ?source URIs with
|
|
53
43
|
* ORDER BY, LIMIT, and OFFSET applied at the instance level.
|
|
@@ -109,7 +109,9 @@ function escapeSPARQL(value) {
|
|
|
109
109
|
.replace(/"/g, '\\"')
|
|
110
110
|
.replace(/\n/g, "\\n")
|
|
111
111
|
.replace(/\r/g, "\\r")
|
|
112
|
-
.replace(/\t/g, "\\t")
|
|
112
|
+
.replace(/\t/g, "\\t")
|
|
113
|
+
.replace(/\0/g, "")
|
|
114
|
+
.replace(/\f/g, "\\u000C");
|
|
113
115
|
}
|
|
114
116
|
/**
|
|
115
117
|
* Format a value as a SPARQL string literal (double-quoted).
|
|
@@ -124,8 +126,14 @@ export function formatSPARQLValue(value) {
|
|
|
124
126
|
* Format an AD4M URI as an RDF IRI for use in SPARQL triple patterns.
|
|
125
127
|
* All AD4M link source/predicate/target values become IRIs in angle brackets.
|
|
126
128
|
* The Rust SPARQL service transparently transforms these to valid IRI format.
|
|
129
|
+
*
|
|
130
|
+
* Rejects characters that would break or inject into a SPARQL IRI token,
|
|
131
|
+
* matching the Rust-side `validate_iri()` function.
|
|
127
132
|
*/
|
|
128
133
|
function iri(value) {
|
|
134
|
+
if (/[<>{}" ]/.test(value)) {
|
|
135
|
+
throw new Error(`Invalid IRI component: '${value}'`);
|
|
136
|
+
}
|
|
129
137
|
return `<${value}>`;
|
|
130
138
|
}
|
|
131
139
|
/**
|
|
@@ -271,21 +279,8 @@ export function buildSPARQLQuery(metadata, allRelationsMetadata, query, modelCla
|
|
|
271
279
|
?_reifier <ad4m://ontology/timestamp> ?timestamp .
|
|
272
280
|
${filterClause}
|
|
273
281
|
}
|
|
274
|
-
${buildSPARQLOrderLimitOffset(metadata, query)}
|
|
275
282
|
`.trim();
|
|
276
283
|
}
|
|
277
|
-
/**
|
|
278
|
-
* Build ORDER BY / LIMIT / OFFSET clauses for the SPARQL query.
|
|
279
|
-
*
|
|
280
|
-
* NOTE: The outer SELECT returns multiple rows per instance (one per link),
|
|
281
|
-
* so LIMIT/OFFSET at the outer level would cut off links mid-instance.
|
|
282
|
-
* This function is kept for backward compatibility and always returns "".
|
|
283
|
-
* SPARQL-level pagination is handled via a subquery in buildSPARQLQuery
|
|
284
|
-
* using buildPaginationSubquery().
|
|
285
|
-
*/
|
|
286
|
-
export function buildSPARQLOrderLimitOffset(_metadata, query) {
|
|
287
|
-
return "";
|
|
288
|
-
}
|
|
289
284
|
/**
|
|
290
285
|
* Build an inner subquery that selects DISTINCT ?source URIs with
|
|
291
286
|
* ORDER BY, LIMIT, and OFFSET applied at the instance level.
|
|
@@ -332,7 +327,7 @@ export function buildPaginationSubquery(joinPatterns, filterExpressions, metadat
|
|
|
332
327
|
{ SELECT DISTINCT ?source${tsSelect} WHERE {${innerJoin}${orderJoinPatterns.join('')}${tsPattern}
|
|
333
328
|
FILTER(isIRI(?source))
|
|
334
329
|
${innerFilter}
|
|
335
|
-
}
|
|
330
|
+
} ${orderByClause} ${limitClause} ${offsetClause} }\n`;
|
|
336
331
|
}
|
|
337
332
|
/**
|
|
338
333
|
* Build SPARQL FILTER expressions from user `where` conditions.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { buildSPARQLOrderLimitOffset, buildSPARQLQuery, hasJsOnlyWhereFilters } from './query-sparql';
|
|
2
|
+
// Minimal stubs for ModelMetadata — buildSPARQLOrderLimitOffset only uses the query arg
|
|
3
|
+
const emptyMetadata = { properties: {}, relations: {} };
|
|
4
|
+
// Metadata with a required (non-literal) property and a literal-stored property
|
|
5
|
+
const richMetadata = {
|
|
6
|
+
properties: {
|
|
7
|
+
name: {
|
|
8
|
+
name: 'name',
|
|
9
|
+
predicate: 'flux://name',
|
|
10
|
+
required: true,
|
|
11
|
+
resolveLanguage: 'literal',
|
|
12
|
+
},
|
|
13
|
+
category: {
|
|
14
|
+
name: 'category',
|
|
15
|
+
predicate: 'flux://category',
|
|
16
|
+
required: true,
|
|
17
|
+
resolveLanguage: 'did:lang:abc', // non-literal → URI stored
|
|
18
|
+
},
|
|
19
|
+
description: {
|
|
20
|
+
name: 'description',
|
|
21
|
+
predicate: 'flux://description',
|
|
22
|
+
initial: 'literal://string:empty',
|
|
23
|
+
resolveLanguage: 'literal',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
relations: {},
|
|
27
|
+
};
|
|
28
|
+
const emptyRelations = {};
|
|
29
|
+
describe('hasJsOnlyWhereFilters', () => {
|
|
30
|
+
it('returns false when no where clause', () => {
|
|
31
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, undefined)).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
it('returns false when where has literal-stored property with equality filter (pushed to SPARQL)', () => {
|
|
34
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { name: 'Pasta' })).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
it('returns true when where has literal-stored property with comparison operator', () => {
|
|
37
|
+
// gt/lt/gte/lte/between/contains remain JS-only
|
|
38
|
+
const meta = { properties: { rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' } }, relations: {} };
|
|
39
|
+
expect(hasJsOnlyWhereFilters(meta, emptyRelations, { rating: { gt: 5 } })).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
it('returns false when where only has non-literal property filter', () => {
|
|
42
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { category: 'some://uri' })).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
it('returns true when where has author filter', () => {
|
|
45
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { author: 'did:key:abc' })).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
it('returns true when where has timestamp filter', () => {
|
|
48
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { timestamp: { gt: 100 } })).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
it('returns true for belongsToOne reverse relation', () => {
|
|
51
|
+
const relMeta = { parent: { kind: 'belongsToOne', predicate: 'flux://parent' } };
|
|
52
|
+
const meta = { properties: { parent: { name: 'parent', predicate: 'flux://parent' } }, relations: {} };
|
|
53
|
+
expect(hasJsOnlyWhereFilters(meta, relMeta, { parent: 'some://id' })).toBe(true);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
describe('buildSPARQLQuery — pagination handled in JS, not SPARQL', () => {
|
|
57
|
+
const modelClass = {};
|
|
58
|
+
it('does NOT include LIMIT/OFFSET in SPARQL even when query has them', () => {
|
|
59
|
+
const query = { limit: 10, offset: 0, where: { category: 'some://uri' } };
|
|
60
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
61
|
+
// SPARQL should NOT contain LIMIT/OFFSET — pagination is done in JS
|
|
62
|
+
expect(sparql).not.toContain('LIMIT');
|
|
63
|
+
expect(sparql).not.toContain('OFFSET');
|
|
64
|
+
// The outer query should still exist
|
|
65
|
+
expect(sparql).toContain('SELECT ?source ?predicate ?target');
|
|
66
|
+
});
|
|
67
|
+
it('does NOT include LIMIT/OFFSET even with JS-only where filters', () => {
|
|
68
|
+
const query = { limit: 10, offset: 0, where: { name: 'Pasta' } };
|
|
69
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
70
|
+
expect(sparql).not.toContain('LIMIT');
|
|
71
|
+
expect(sparql).not.toContain('OFFSET');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
describe('buildSPARQLOrderLimitOffset', () => {
|
|
75
|
+
it('always returns empty string (pagination is handled in JS)', () => {
|
|
76
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, {})).toBe('');
|
|
77
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, { limit: 50 })).toBe('');
|
|
78
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, { limit: 50, offset: 100 })).toBe('');
|
|
79
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, { order: { timestamp: 'DESC' } })).toBe('');
|
|
80
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, {
|
|
81
|
+
order: { timestamp: 'DESC' },
|
|
82
|
+
limit: 50,
|
|
83
|
+
offset: 100,
|
|
84
|
+
})).toBe('');
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
describe('buildSPARQLQuery — parse_literal push-down filters', () => {
|
|
88
|
+
const modelClass = {};
|
|
89
|
+
it('generates parse_literal FILTER for equality on literal property', () => {
|
|
90
|
+
const query = { where: { name: 'Alice' } };
|
|
91
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
92
|
+
expect(sparql).toContain('<ad4m://fn/parse_literal>');
|
|
93
|
+
expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) = "Alice"');
|
|
94
|
+
});
|
|
95
|
+
it('generates parse_literal IN FILTER for array on literal property', () => {
|
|
96
|
+
const query = { where: { name: ['Alice', 'Bob'] } };
|
|
97
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
98
|
+
expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) IN ("Alice", "Bob")');
|
|
99
|
+
});
|
|
100
|
+
it('generates parse_literal NOT FILTER for not condition', () => {
|
|
101
|
+
const query = { where: { name: { not: 'deleted' } } };
|
|
102
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
103
|
+
expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) != "deleted"');
|
|
104
|
+
});
|
|
105
|
+
it('does NOT use parse_literal for comparison operators (gt/lt)', () => {
|
|
106
|
+
const meta = {
|
|
107
|
+
properties: {
|
|
108
|
+
rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
|
|
109
|
+
},
|
|
110
|
+
relations: {},
|
|
111
|
+
};
|
|
112
|
+
const query = { where: { rating: { gt: 5 } } };
|
|
113
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
114
|
+
expect(sparql).not.toContain('parse_literal');
|
|
115
|
+
expect(sparql).toContain('?wTarget_cmp_rating');
|
|
116
|
+
});
|
|
117
|
+
it('does NOT use parse_literal for non-literal properties', () => {
|
|
118
|
+
const query = { where: { category: 'some://uri' } };
|
|
119
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
120
|
+
expect(sparql).not.toContain('parse_literal');
|
|
121
|
+
expect(sparql).toContain('<some://uri>');
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
describe('buildSPARQLQuery — structural correctness', () => {
|
|
125
|
+
const modelClass = {};
|
|
126
|
+
it('generates valid SPARQL with parent filter', () => {
|
|
127
|
+
const query = {
|
|
128
|
+
limit: 50,
|
|
129
|
+
offset: 0,
|
|
130
|
+
parent: { id: 'flux://channel-123', predicate: 'flux://has_message' },
|
|
131
|
+
where: { category: 'some://uri' },
|
|
132
|
+
};
|
|
133
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
134
|
+
expect(sparql).toContain('<flux://channel-123>');
|
|
135
|
+
// No SPARQL-level pagination
|
|
136
|
+
expect(sparql).not.toContain('LIMIT');
|
|
137
|
+
});
|
|
138
|
+
it('does not allow injection through where clause IRI values', () => {
|
|
139
|
+
const query = {
|
|
140
|
+
where: { category: 'some://uri"> . } UNION { SELECT * WHERE { ?s ?p ?o' },
|
|
141
|
+
};
|
|
142
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
143
|
+
// Value should be wrapped in angle brackets as an IRI
|
|
144
|
+
expect(sparql).toContain('<some://uri');
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
describe('buildSPARQLQuery — NOT with array push-down', () => {
|
|
148
|
+
const modelClass = {};
|
|
149
|
+
it('generates NOT IN filter for NOT with array of values', () => {
|
|
150
|
+
const query = { where: { name: { not: ['deleted', 'archived'] } } };
|
|
151
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
152
|
+
// Should use NOT IN with parse_literal for literal properties
|
|
153
|
+
expect(sparql).toContain('NOT IN');
|
|
154
|
+
expect(sparql).toContain('"deleted"');
|
|
155
|
+
expect(sparql).toContain('"archived"');
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
describe('buildSPARQLQuery — mixed push-down + JS-only', () => {
|
|
159
|
+
const modelClass = {};
|
|
160
|
+
const mixedMeta = {
|
|
161
|
+
properties: {
|
|
162
|
+
name: { name: 'name', predicate: 'flux://name', required: true, resolveLanguage: 'literal' },
|
|
163
|
+
rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
|
|
164
|
+
},
|
|
165
|
+
relations: {},
|
|
166
|
+
};
|
|
167
|
+
it('pushes equality to SPARQL but keeps gt as JS-only', () => {
|
|
168
|
+
const query = { where: { name: 'Alice', rating: { gt: 5 } } };
|
|
169
|
+
const sparql = buildSPARQLQuery(mixedMeta, emptyRelations, query, modelClass);
|
|
170
|
+
// name equality should be pushed to SPARQL
|
|
171
|
+
expect(sparql).toContain('parse_literal');
|
|
172
|
+
expect(sparql).toContain('Alice');
|
|
173
|
+
// hasJsOnlyWhereFilters should return true because of rating.gt
|
|
174
|
+
expect(hasJsOnlyWhereFilters(mixedMeta, emptyRelations, query.where)).toBe(true);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
describe('buildSPARQLQuery — non-literal and flag properties', () => {
|
|
178
|
+
const modelClass = {};
|
|
179
|
+
it('does NOT use parse_literal for non-literal (resolveLanguage) properties', () => {
|
|
180
|
+
const meta = {
|
|
181
|
+
properties: {
|
|
182
|
+
avatar: { name: 'avatar', predicate: 'flux://avatar', required: true, resolveLanguage: 'did:lang:some-language' },
|
|
183
|
+
},
|
|
184
|
+
relations: {},
|
|
185
|
+
};
|
|
186
|
+
const query = { where: { avatar: 'https://example.com/pic.jpg' } };
|
|
187
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
188
|
+
expect(sparql).not.toContain('parse_literal');
|
|
189
|
+
});
|
|
190
|
+
it('does NOT use parse_literal for flag properties', () => {
|
|
191
|
+
const meta = {
|
|
192
|
+
properties: {
|
|
193
|
+
isPublic: { name: 'isPublic', predicate: 'flux://isPublic', flag: true, initial: 'flux://true' },
|
|
194
|
+
},
|
|
195
|
+
relations: {},
|
|
196
|
+
};
|
|
197
|
+
const query = { where: { isPublic: 'flux://true' } };
|
|
198
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
199
|
+
expect(sparql).not.toContain('parse_literal');
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
describe('buildSPARQLQuery — IRI correctness', () => {
|
|
203
|
+
const modelClass = {};
|
|
204
|
+
it('uses <ad4m://fn/parse_literal> IRI, not fn::parse_literal SurrealDB syntax', () => {
|
|
205
|
+
const query = { where: { name: 'test' } };
|
|
206
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
207
|
+
expect(sparql).toContain('<ad4m://fn/parse_literal>');
|
|
208
|
+
expect(sparql).not.toContain('fn::parse_literal');
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
describe('buildSPARQLQuery — set-difference patterns', () => {
|
|
212
|
+
const modelClass = {};
|
|
213
|
+
// Subgroups are grandchildren of channels (channel → conversation → subgroup).
|
|
214
|
+
// Never assume direct child relationships in set-difference queries.
|
|
215
|
+
// A global EXISTS pattern (no parent scoping) should match items regardless of depth.
|
|
216
|
+
it('generates global EXISTS without parent scoping when no parent filter', () => {
|
|
217
|
+
const meta = {
|
|
218
|
+
properties: {
|
|
219
|
+
status: { name: 'status', predicate: 'flux://status', required: true, resolveLanguage: 'literal' },
|
|
220
|
+
},
|
|
221
|
+
relations: {},
|
|
222
|
+
};
|
|
223
|
+
const query = { where: { status: 'active' } };
|
|
224
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
225
|
+
// Without a parent filter, the query should not scope to any specific parent
|
|
226
|
+
// This ensures grandchildren (items at any depth) are matched
|
|
227
|
+
expect(sparql).not.toContain('flux://has_child');
|
|
228
|
+
expect(sparql).toContain('SELECT ?source ?predicate ?target');
|
|
229
|
+
});
|
|
230
|
+
});
|