@coasys/ad4m 0.13.0-test-1 → 0.13.0-test-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/lib/index.cjs +2377 -849
- package/lib/index.js +2354 -846
- package/lib/src/Literal.js +6 -5
- package/lib/src/agent/AgentClient.d.ts +5 -1
- package/lib/src/agent/AgentClient.js +48 -2
- package/lib/src/expression/ExpressionRef.js +2 -2
- package/lib/src/index.d.ts +0 -1
- package/lib/src/index.js +0 -1
- package/lib/src/language/Language.d.ts +26 -176
- package/lib/src/language/Language.js +28 -2
- package/lib/src/model/Ad4mModel.d.ts +52 -28
- package/lib/src/model/Ad4mModel.js +255 -118
- package/lib/src/model/Ad4mModel.test.js +1727 -537
- package/lib/src/model/ModelQueryBuilder.d.ts +67 -34
- package/lib/src/model/ModelQueryBuilder.js +208 -79
- package/lib/src/model/Subject.js +6 -6
- package/lib/src/model/decorators.d.ts +14 -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 +4 -3
- package/lib/src/model/hydration.js +365 -86
- package/lib/src/model/index.d.ts +4 -1
- package/lib/src/model/index.js +4 -1
- package/lib/src/model/json-schema.js +2 -2
- 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 +124 -0
- package/lib/src/model/query-sparql.js +767 -0
- package/lib/src/model/query-sparql.test.d.ts +1 -0
- package/lib/src/model/query-sparql.test.js +495 -0
- package/lib/src/model/{surreal-utils.d.ts → query-utils.d.ts} +12 -9
- package/lib/src/model/{surreal-utils.js → query-utils.js} +43 -27
- 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 +42 -18
- 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 +52 -4
- package/lib/src/model/types.js +1 -1
- package/lib/src/perspectives/PerspectiveClient.d.ts +3 -17
- package/lib/src/perspectives/PerspectiveClient.js +42 -66
- package/lib/src/perspectives/PerspectiveClient.test.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveClient.test.js +169 -0
- package/lib/src/perspectives/PerspectiveProxy.d.ts +54 -30
- package/lib/src/perspectives/PerspectiveProxy.js +239 -177
- package/lib/src/perspectives/PerspectiveResolver.d.ts +0 -1
- package/lib/src/perspectives/PerspectiveResolver.js +0 -11
- package/lib/src/perspectives/WakerSubscriptionManager.d.ts +3 -3
- package/lib/src/perspectives/WakerSubscriptionManager.js +7 -8
- package/lib/src/runtime/RuntimeResolver.d.ts +11 -0
- package/lib/src/runtime/RuntimeResolver.js +56 -0
- package/lib/src/schema.gql +11 -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 +1 -1
- package/lib/src/shacl/SHACLShape.js +38 -38
- package/lib/src/typeDefs.js +11 -1
- package/lib/src/utils.d.ts +6 -6
- package/lib/src/utils.js +6 -6
- package/package.json +1 -1
- package/src/Ad4mClient.test.ts +56 -1
- package/src/Literal.test.ts +10 -5
- package/src/Literal.ts +6 -5
- package/src/agent/AgentClient.ts +53 -1
- package/src/expression/ExpressionRef.ts +2 -2
- package/src/index.ts +0 -1
- package/src/language/Language.ts +55 -231
- package/src/model/Ad4mModel.test.ts +1709 -572
- package/src/model/Ad4mModel.ts +274 -120
- package/src/model/ModelQueryBuilder.ts +234 -87
- package/src/model/Subject.ts +6 -6
- package/src/model/decorators.ts +51 -22
- package/src/model/hydration-batch.ts +205 -0
- package/src/model/hydration-batching.test.ts +563 -0
- package/src/model/hydration.ts +349 -84
- package/src/model/index.ts +4 -1
- 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-batch.ts +301 -0
- package/src/model/query-sparql.test.ts +568 -0
- package/src/model/query-sparql.ts +820 -0
- package/src/model/{surreal-utils.ts → query-utils.ts} +44 -27
- package/src/model/relation-filtering.test.ts +15 -15
- package/src/model/sdna.ts +1 -1
- package/src/model/shacl-gen.ts +42 -17
- package/src/model/subscription-delta.test.ts +207 -0
- package/src/model/subscription-pool.ts +211 -0
- package/src/model/types.ts +53 -4
- package/src/perspectives/PerspectiveClient.test.ts +204 -0
- package/src/perspectives/PerspectiveClient.ts +41 -68
- package/src/perspectives/PerspectiveProxy.ts +244 -181
- package/src/perspectives/PerspectiveResolver.ts +0 -8
- package/src/perspectives/WakerSubscriptionManager.ts +8 -9
- package/src/runtime/RuntimeResolver.ts +43 -0
- package/src/shacl/SHACLFlow.ts +9 -9
- package/src/shacl/SHACLShape.ts +39 -39
- package/src/utils.ts +6 -6
- package/lib/src/model/query-surreal.d.ts +0 -44
- package/lib/src/model/query-surreal.js +0 -249
- package/src/language/LanguageContext.ts +0 -37
- package/src/model/query-surreal.ts +0 -280
- package/tsconfig-browser.json +0 -82
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import { buildSPARQLOrderLimitOffset, buildSPARQLQuery, buildSPARQLCountQuery, hasJsOnlyWhereFilters, buildSPARQLGroupedCountQuery, parseSparqlGroupedCount } 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 (count() fast path is unsafe — buildSPARQLCountQuery has no named graph)', () => {
|
|
45
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { author: 'did:key:abc' })).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
it('returns true when where has timestamp filter (same reason — guards the COUNT fast path)', () => {
|
|
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 — SPARQL-level pagination via subquery', () => {
|
|
57
|
+
const modelClass = {};
|
|
58
|
+
it('includes LIMIT/OFFSET in a pagination subquery when query has them, no JS-only filters, and ORDER BY', () => {
|
|
59
|
+
const query = { limit: 10, offset: 0, where: { category: 'some://uri' }, order: { name: 'ASC' } };
|
|
60
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
61
|
+
// SPARQL should contain LIMIT inside the pagination subquery
|
|
62
|
+
expect(sparql).toContain('LIMIT 10');
|
|
63
|
+
// The outer query should still exist
|
|
64
|
+
expect(sparql).toContain('SELECT ?source ?predicate ?target');
|
|
65
|
+
// Should have a subquery pattern
|
|
66
|
+
expect(sparql).toContain('SELECT DISTINCT ?source');
|
|
67
|
+
});
|
|
68
|
+
it('does NOT use SPARQL-level pagination when limit/offset present but no ORDER BY', () => {
|
|
69
|
+
const query = { limit: 10, offset: 0, where: { category: 'some://uri' } };
|
|
70
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
71
|
+
// Without ORDER BY, SPARQL pagination is not used — JS-level slicing handles it
|
|
72
|
+
expect(sparql).not.toContain('LIMIT');
|
|
73
|
+
expect(sparql).not.toContain('OFFSET');
|
|
74
|
+
// No subquery needed
|
|
75
|
+
expect(sparql).not.toContain('SELECT DISTINCT');
|
|
76
|
+
});
|
|
77
|
+
it('does NOT include LIMIT/OFFSET when JS-only where filters exist (literal gt operator)', () => {
|
|
78
|
+
// gt on a literal-stored property is JS-only, so pagination must not be pushed to SPARQL
|
|
79
|
+
const meta = { properties: { rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' } }, relations: {} };
|
|
80
|
+
const query = { limit: 10, offset: 0, where: { rating: { gt: 3 } } };
|
|
81
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
82
|
+
expect(sparql).not.toContain('LIMIT');
|
|
83
|
+
expect(sparql).not.toContain('OFFSET');
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
describe('buildSPARQLOrderLimitOffset', () => {
|
|
87
|
+
it('always returns empty string (pagination uses subquery pattern instead)', () => {
|
|
88
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, {})).toBe('');
|
|
89
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, { limit: 50 })).toBe('');
|
|
90
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, { limit: 50, offset: 100 })).toBe('');
|
|
91
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, { order: { timestamp: 'DESC' } })).toBe('');
|
|
92
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, {
|
|
93
|
+
order: { timestamp: 'DESC' },
|
|
94
|
+
limit: 50,
|
|
95
|
+
offset: 100,
|
|
96
|
+
})).toBe('');
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe('buildSPARQLQuery — parse_literal push-down filters', () => {
|
|
100
|
+
const modelClass = {};
|
|
101
|
+
it('generates parse_literal FILTER for equality on literal property', () => {
|
|
102
|
+
const query = { where: { name: 'Alice' } };
|
|
103
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
104
|
+
expect(sparql).toContain('<ad4m://fn/parse_literal>');
|
|
105
|
+
expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) = "Alice"');
|
|
106
|
+
});
|
|
107
|
+
it('generates parse_literal IN FILTER for array on literal property', () => {
|
|
108
|
+
const query = { where: { name: ['Alice', 'Bob'] } };
|
|
109
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
110
|
+
expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) IN ("Alice", "Bob")');
|
|
111
|
+
});
|
|
112
|
+
it('generates parse_literal NOT FILTER for not condition', () => {
|
|
113
|
+
const query = { where: { name: { not: 'deleted' } } };
|
|
114
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
115
|
+
expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) != "deleted"');
|
|
116
|
+
});
|
|
117
|
+
it('does NOT use parse_literal for comparison operators (gt/lt)', () => {
|
|
118
|
+
const meta = {
|
|
119
|
+
properties: {
|
|
120
|
+
rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
|
|
121
|
+
},
|
|
122
|
+
relations: {},
|
|
123
|
+
};
|
|
124
|
+
const query = { where: { rating: { gt: 5 } } };
|
|
125
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
126
|
+
expect(sparql).not.toContain('parse_literal');
|
|
127
|
+
expect(sparql).toContain('?wTarget_cmp_rating');
|
|
128
|
+
});
|
|
129
|
+
it('does NOT use parse_literal for non-literal properties', () => {
|
|
130
|
+
const query = { where: { category: 'some://uri' } };
|
|
131
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
132
|
+
expect(sparql).not.toContain('parse_literal');
|
|
133
|
+
expect(sparql).toContain('<some://uri>');
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
describe('buildSPARQLQuery — structural correctness', () => {
|
|
137
|
+
const modelClass = {};
|
|
138
|
+
it('generates valid SPARQL with parent filter', () => {
|
|
139
|
+
const query = {
|
|
140
|
+
limit: 50,
|
|
141
|
+
offset: 0,
|
|
142
|
+
parent: { id: 'flux://channel-123', predicate: 'flux://has_message' },
|
|
143
|
+
where: { category: 'some://uri' },
|
|
144
|
+
order: { name: 'ASC' },
|
|
145
|
+
};
|
|
146
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
147
|
+
expect(sparql).toContain('<flux://channel-123>');
|
|
148
|
+
// SPARQL-level pagination via subquery
|
|
149
|
+
expect(sparql).toContain('LIMIT 50');
|
|
150
|
+
expect(sparql).toContain('SELECT DISTINCT ?source');
|
|
151
|
+
});
|
|
152
|
+
it('does not allow injection through where clause IRI values', () => {
|
|
153
|
+
const query = {
|
|
154
|
+
where: { category: 'some://uri"> . } UNION { SELECT * WHERE { ?s ?p ?o' },
|
|
155
|
+
};
|
|
156
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
157
|
+
// Value should be wrapped in angle brackets as an IRI
|
|
158
|
+
expect(sparql).toContain('<some://uri');
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
describe('buildSPARQLQuery — NOT with array push-down', () => {
|
|
162
|
+
const modelClass = {};
|
|
163
|
+
it('generates NOT IN filter for NOT with array of values', () => {
|
|
164
|
+
const query = { where: { name: { not: ['deleted', 'archived'] } } };
|
|
165
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
166
|
+
// Should use NOT IN with parse_literal for literal properties
|
|
167
|
+
expect(sparql).toContain('NOT IN');
|
|
168
|
+
expect(sparql).toContain('"deleted"');
|
|
169
|
+
expect(sparql).toContain('"archived"');
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
describe('buildSPARQLQuery — mixed push-down + JS-only', () => {
|
|
173
|
+
const modelClass = {};
|
|
174
|
+
const mixedMeta = {
|
|
175
|
+
properties: {
|
|
176
|
+
name: { name: 'name', predicate: 'flux://name', required: true, resolveLanguage: 'literal' },
|
|
177
|
+
rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
|
|
178
|
+
},
|
|
179
|
+
relations: {},
|
|
180
|
+
};
|
|
181
|
+
it('pushes equality to SPARQL but keeps gt as JS-only', () => {
|
|
182
|
+
const query = { where: { name: 'Alice', rating: { gt: 5 } } };
|
|
183
|
+
const sparql = buildSPARQLQuery(mixedMeta, emptyRelations, query, modelClass);
|
|
184
|
+
// name equality should be pushed to SPARQL
|
|
185
|
+
expect(sparql).toContain('parse_literal');
|
|
186
|
+
expect(sparql).toContain('Alice');
|
|
187
|
+
// hasJsOnlyWhereFilters should return true because of rating.gt
|
|
188
|
+
expect(hasJsOnlyWhereFilters(mixedMeta, emptyRelations, query.where)).toBe(true);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
describe('buildSPARQLQuery — non-literal and flag properties', () => {
|
|
192
|
+
const modelClass = {};
|
|
193
|
+
it('does NOT use parse_literal for non-literal (resolveLanguage) properties', () => {
|
|
194
|
+
const meta = {
|
|
195
|
+
properties: {
|
|
196
|
+
avatar: { name: 'avatar', predicate: 'flux://avatar', required: true, resolveLanguage: 'did:lang:some-language' },
|
|
197
|
+
},
|
|
198
|
+
relations: {},
|
|
199
|
+
};
|
|
200
|
+
const query = { where: { avatar: 'https://example.com/pic.jpg' } };
|
|
201
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
202
|
+
expect(sparql).not.toContain('parse_literal');
|
|
203
|
+
});
|
|
204
|
+
it('does NOT use parse_literal for flag properties', () => {
|
|
205
|
+
const meta = {
|
|
206
|
+
properties: {
|
|
207
|
+
isPublic: { name: 'isPublic', predicate: 'flux://isPublic', flag: true, initial: 'flux://true' },
|
|
208
|
+
},
|
|
209
|
+
relations: {},
|
|
210
|
+
};
|
|
211
|
+
const query = { where: { isPublic: 'flux://true' } };
|
|
212
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
213
|
+
expect(sparql).not.toContain('parse_literal');
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
describe('buildSPARQLQuery — IRI correctness', () => {
|
|
217
|
+
const modelClass = {};
|
|
218
|
+
it('uses <ad4m://fn/parse_literal> IRI, not fn::parse_literal SurrealDB syntax', () => {
|
|
219
|
+
const query = { where: { name: 'test' } };
|
|
220
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
221
|
+
expect(sparql).toContain('<ad4m://fn/parse_literal>');
|
|
222
|
+
expect(sparql).not.toContain('fn::parse_literal');
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
// ──────────────────────────────────────────────────────────
|
|
226
|
+
// SPARQL-level pagination (LIMIT/OFFSET in generated queries)
|
|
227
|
+
// ──────────────────────────────────────────────────────────
|
|
228
|
+
describe('SPARQL-level pagination', () => {
|
|
229
|
+
const modelClass = {};
|
|
230
|
+
it('includes LIMIT in SPARQL when query specifies limit + order', () => {
|
|
231
|
+
const query = { limit: 30, order: { name: 'ASC' } };
|
|
232
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
233
|
+
expect(sparql).toContain('LIMIT 30');
|
|
234
|
+
expect(sparql).toContain('SELECT DISTINCT ?source');
|
|
235
|
+
});
|
|
236
|
+
it('does NOT push LIMIT to SPARQL when no ORDER BY is specified', () => {
|
|
237
|
+
const query = { limit: 30 };
|
|
238
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
239
|
+
// Without ORDER BY, SPARQL-level pagination is not used
|
|
240
|
+
expect(sparql).not.toContain('LIMIT');
|
|
241
|
+
expect(sparql).not.toContain('SELECT DISTINCT');
|
|
242
|
+
});
|
|
243
|
+
it('includes OFFSET in SPARQL when query specifies offset > 0 with order', () => {
|
|
244
|
+
const query = { limit: 20, offset: 40, order: { name: 'ASC' } };
|
|
245
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
246
|
+
expect(sparql).toContain('LIMIT 20');
|
|
247
|
+
expect(sparql).toContain('OFFSET 40');
|
|
248
|
+
});
|
|
249
|
+
it('does NOT include OFFSET when offset is 0', () => {
|
|
250
|
+
const query = { limit: 10, offset: 0, order: { name: 'ASC' } };
|
|
251
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
252
|
+
expect(sparql).toContain('LIMIT 10');
|
|
253
|
+
expect(sparql).not.toContain('OFFSET');
|
|
254
|
+
});
|
|
255
|
+
it('includes ORDER BY in subquery when query.order is specified', () => {
|
|
256
|
+
const query = { limit: 10, order: { name: 'DESC' } };
|
|
257
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
258
|
+
expect(sparql).toContain('ORDER BY DESC(');
|
|
259
|
+
expect(sparql).toContain('LIMIT 10');
|
|
260
|
+
});
|
|
261
|
+
it('uses SAMPLE() aggregate for ORDER BY variables in pagination subquery (SPARQL 1.1 compliance)', () => {
|
|
262
|
+
const query = { limit: 5, order: { name: 'ASC' } };
|
|
263
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
264
|
+
// Must use SAMPLE() to aggregate the order variable under GROUP BY
|
|
265
|
+
expect(sparql).toMatch(/SAMPLE\(/);
|
|
266
|
+
expect(sparql).toMatch(/AS \?pg_sort_0/);
|
|
267
|
+
expect(sparql).toContain('GROUP BY ?source');
|
|
268
|
+
expect(sparql).toContain('ORDER BY ASC(?pg_sort_0)');
|
|
269
|
+
});
|
|
270
|
+
it('omits GROUP BY when ORDER BY is present (uses SAMPLE() aggregate instead)', () => {
|
|
271
|
+
// When ORDER BY is present, GROUP BY ?source is used with SAMPLE() aggregates.
|
|
272
|
+
// The old redundant GROUP BY (without ORDER BY) no longer applies because
|
|
273
|
+
// SPARQL-level pagination is only used when ORDER BY is present.
|
|
274
|
+
const query = { limit: 10, order: { name: 'ASC' } };
|
|
275
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
276
|
+
expect(sparql).toContain('GROUP BY ?source');
|
|
277
|
+
expect(sparql).toContain('SAMPLE(');
|
|
278
|
+
expect(sparql).toContain('SELECT DISTINCT ?source');
|
|
279
|
+
expect(sparql).toContain('LIMIT 10');
|
|
280
|
+
});
|
|
281
|
+
it('does NOT default to ORDER BY timestamp when paginating without explicit order', () => {
|
|
282
|
+
const query = { limit: 30 };
|
|
283
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
284
|
+
expect(sparql).not.toContain('ORDER BY');
|
|
285
|
+
expect(sparql).not.toContain('pg_minTs');
|
|
286
|
+
expect(sparql).not.toContain('GRAPH ?pg_g');
|
|
287
|
+
});
|
|
288
|
+
it('does NOT push pagination to SPARQL when JS-only where filters exist (literal gt operator)', () => {
|
|
289
|
+
// author equality is now SPARQL-pushable; use a literal gt which remains JS-only
|
|
290
|
+
const meta = { properties: { rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' } }, relations: {} };
|
|
291
|
+
const query = { limit: 10, where: { rating: { gt: 5 } } };
|
|
292
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
293
|
+
expect(sparql).not.toContain('LIMIT');
|
|
294
|
+
expect(sparql).not.toContain('OFFSET');
|
|
295
|
+
});
|
|
296
|
+
it('does NOT push pagination to SPARQL when JS-only where filters exist (gt operator)', () => {
|
|
297
|
+
const meta = {
|
|
298
|
+
properties: {
|
|
299
|
+
rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
|
|
300
|
+
},
|
|
301
|
+
relations: {},
|
|
302
|
+
};
|
|
303
|
+
const query = { limit: 10, where: { rating: { gt: 5 } } };
|
|
304
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
305
|
+
expect(sparql).not.toContain('LIMIT');
|
|
306
|
+
});
|
|
307
|
+
it('wraps pagination in a subquery (outer SELECT fetches all links for the page)', () => {
|
|
308
|
+
const query = { limit: 5, offset: 10, order: { name: 'DESC' } };
|
|
309
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
310
|
+
expect(sparql).toContain('SELECT ?source ?predicate ?target ?author ?timestamp');
|
|
311
|
+
expect(sparql).toContain('SELECT DISTINCT ?source');
|
|
312
|
+
expect(sparql).toContain('LIMIT 5');
|
|
313
|
+
expect(sparql).toContain('OFFSET 10');
|
|
314
|
+
});
|
|
315
|
+
it('does NOT wrap pagination in a subquery when no ORDER BY (JS slicing preserves natural order)', () => {
|
|
316
|
+
const query = { limit: 5, offset: 10 };
|
|
317
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
318
|
+
// Should be a plain SELECT without subquery
|
|
319
|
+
expect(sparql).toContain('SELECT ?source ?predicate ?target ?author ?timestamp');
|
|
320
|
+
expect(sparql).not.toContain('SELECT DISTINCT');
|
|
321
|
+
expect(sparql).not.toContain('LIMIT');
|
|
322
|
+
expect(sparql).not.toContain('OFFSET');
|
|
323
|
+
});
|
|
324
|
+
describe('buildSPARQLCountQuery', () => {
|
|
325
|
+
it('returns a COUNT(DISTINCT ?source) query', () => {
|
|
326
|
+
const query = { parent: { id: 'flux://ch-1', predicate: 'flux://has_child' } };
|
|
327
|
+
const countSparql = buildSPARQLCountQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
328
|
+
expect(countSparql).toContain('COUNT(DISTINCT ?source)');
|
|
329
|
+
expect(countSparql).toContain('?count');
|
|
330
|
+
expect(countSparql).toContain('<flux://ch-1>');
|
|
331
|
+
});
|
|
332
|
+
it('does NOT include LIMIT/OFFSET (counts full result set)', () => {
|
|
333
|
+
const query = { limit: 10, offset: 20, parent: { id: 'flux://ch-1', predicate: 'flux://has_child' } };
|
|
334
|
+
const countSparql = buildSPARQLCountQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
335
|
+
expect(countSparql).not.toContain('LIMIT');
|
|
336
|
+
expect(countSparql).not.toContain('OFFSET');
|
|
337
|
+
expect(countSparql).toContain('COUNT(DISTINCT ?source)');
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
describe('buildSPARQLQuery — set-difference patterns', () => {
|
|
342
|
+
const modelClass = {};
|
|
343
|
+
// Subgroups are grandchildren of channels (channel → conversation → subgroup).
|
|
344
|
+
// Never assume direct child relationships in set-difference queries.
|
|
345
|
+
// A global EXISTS pattern (no parent scoping) should match items regardless of depth.
|
|
346
|
+
it('generates global EXISTS without parent scoping when no parent filter', () => {
|
|
347
|
+
const meta = {
|
|
348
|
+
properties: {
|
|
349
|
+
status: { name: 'status', predicate: 'flux://status', required: true, resolveLanguage: 'literal' },
|
|
350
|
+
},
|
|
351
|
+
relations: {},
|
|
352
|
+
};
|
|
353
|
+
const query = { where: { status: 'active' } };
|
|
354
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
355
|
+
// Without a parent filter, the query should not scope to any specific parent
|
|
356
|
+
// This ensures grandchildren (items at any depth) are matched
|
|
357
|
+
expect(sparql).not.toContain('flux://has_child');
|
|
358
|
+
expect(sparql).toContain('SELECT ?source ?predicate ?target');
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
// ─────────────────────────────────────────────────────────────
|
|
362
|
+
// buildSPARQLWhereFilters — author / timestamp SPARQL push
|
|
363
|
+
// ─────────────────────────────────────────────────────────────
|
|
364
|
+
describe('buildSPARQLQuery — author/timestamp SPARQL filter push', () => {
|
|
365
|
+
const modelClass = {};
|
|
366
|
+
it('pushes author equality into SPARQL FILTER using STR(?author)', () => {
|
|
367
|
+
const query = { where: { author: 'did:key:zABC' } };
|
|
368
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
369
|
+
expect(sparql).toContain('STR(?author) = "did:key:zABC"');
|
|
370
|
+
expect(sparql).toContain('?author');
|
|
371
|
+
});
|
|
372
|
+
it('pushes author IN array into SPARQL FILTER', () => {
|
|
373
|
+
const query = { where: { author: ['did:key:zABC', 'did:key:zDEF'] } };
|
|
374
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
375
|
+
expect(sparql).toContain('STR(?author) IN');
|
|
376
|
+
expect(sparql).toContain('"did:key:zABC"');
|
|
377
|
+
expect(sparql).toContain('"did:key:zDEF"');
|
|
378
|
+
});
|
|
379
|
+
it('pushes author NOT into SPARQL FILTER', () => {
|
|
380
|
+
const query = { where: { author: { not: 'did:key:zABC' } } };
|
|
381
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
382
|
+
expect(sparql).toContain('STR(?author) != "did:key:zABC"');
|
|
383
|
+
});
|
|
384
|
+
it('pushes timestamp gt/lt into SPARQL FILTER', () => {
|
|
385
|
+
const query = { where: { timestamp: { gt: 1000, lt: 9000 } } };
|
|
386
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
387
|
+
expect(sparql).toContain('?timestamp > 1000');
|
|
388
|
+
expect(sparql).toContain('?timestamp < 9000');
|
|
389
|
+
});
|
|
390
|
+
it('pushes timestamp between into SPARQL FILTER', () => {
|
|
391
|
+
const query = { where: { timestamp: { between: [100, 200] } } };
|
|
392
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
393
|
+
expect(sparql).toContain('?timestamp >= 100');
|
|
394
|
+
expect(sparql).toContain('?timestamp <= 200');
|
|
395
|
+
});
|
|
396
|
+
it('pushes timestamp equality into SPARQL FILTER', () => {
|
|
397
|
+
const query = { where: { timestamp: 1714000000000 } };
|
|
398
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
399
|
+
expect(sparql).toContain('?timestamp = 1714000000000');
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
// ─────────────────────────────────────────────────────────────
|
|
403
|
+
// buildSPARQLGroupedCountQuery
|
|
404
|
+
// ─────────────────────────────────────────────────────────────
|
|
405
|
+
describe('buildSPARQLGroupedCountQuery', () => {
|
|
406
|
+
const signalMetadata = {
|
|
407
|
+
properties: {
|
|
408
|
+
signalTypeId: {
|
|
409
|
+
name: 'signalTypeId',
|
|
410
|
+
predicate: 'flux://signalTypeId',
|
|
411
|
+
required: true,
|
|
412
|
+
resolveLanguage: 'literal',
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
relations: {},
|
|
416
|
+
};
|
|
417
|
+
it('selects ?parent and COUNT(DISTINCT ?source)', () => {
|
|
418
|
+
const sparql = buildSPARQLGroupedCountQuery(signalMetadata, emptyRelations, 'flux://has-signal', ['flux://post/1'], undefined);
|
|
419
|
+
expect(sparql).toContain('SELECT ?parent (COUNT(DISTINCT ?source) AS ?count)');
|
|
420
|
+
});
|
|
421
|
+
it('groups by ?parent', () => {
|
|
422
|
+
const sparql = buildSPARQLGroupedCountQuery(signalMetadata, emptyRelations, 'flux://has-signal', ['flux://post/1'], undefined);
|
|
423
|
+
expect(sparql).toContain('GROUP BY ?parent');
|
|
424
|
+
});
|
|
425
|
+
it('includes parent → child join with the supplied predicate', () => {
|
|
426
|
+
const sparql = buildSPARQLGroupedCountQuery(signalMetadata, emptyRelations, 'flux://has-signal', ['flux://post/1', 'flux://post/2'], undefined);
|
|
427
|
+
expect(sparql).toContain('?parent <flux://has-signal> ?source');
|
|
428
|
+
});
|
|
429
|
+
it('includes FILTER(?parent IN (...)) for the supplied parent IDs', () => {
|
|
430
|
+
const sparql = buildSPARQLGroupedCountQuery(signalMetadata, emptyRelations, 'flux://has-signal', ['flux://post/1', 'flux://post/2'], undefined);
|
|
431
|
+
expect(sparql).toContain('?parent IN (<flux://post/1>, <flux://post/2>)');
|
|
432
|
+
});
|
|
433
|
+
it('includes the full named graph block (GRAPH ?linkGraph + author + timestamp)', () => {
|
|
434
|
+
const sparql = buildSPARQLGroupedCountQuery(signalMetadata, emptyRelations, 'flux://has-signal', ['flux://post/1'], undefined);
|
|
435
|
+
expect(sparql).toContain('GRAPH ?linkGraph');
|
|
436
|
+
expect(sparql).toContain('?linkGraph <ad4m://ontology/author> ?author');
|
|
437
|
+
expect(sparql).toContain('?linkGraph <ad4m://ontology/timestamp> ?timestamp');
|
|
438
|
+
});
|
|
439
|
+
it('includes conformance join for required properties', () => {
|
|
440
|
+
const sparql = buildSPARQLGroupedCountQuery(signalMetadata, emptyRelations, 'flux://has-signal', ['flux://post/1'], undefined);
|
|
441
|
+
expect(sparql).toContain('<flux://signalTypeId>');
|
|
442
|
+
});
|
|
443
|
+
it('applies a where clause filter (author equality) via SPARQL FILTER', () => {
|
|
444
|
+
const sparql = buildSPARQLGroupedCountQuery(signalMetadata, emptyRelations, 'flux://has-signal', ['flux://post/1'], { author: 'did:key:zABC' });
|
|
445
|
+
expect(sparql).toContain('STR(?author) = "did:key:zABC"');
|
|
446
|
+
});
|
|
447
|
+
it('applies a where clause filter (required property equality) via parse_literal', () => {
|
|
448
|
+
const sparql = buildSPARQLGroupedCountQuery(signalMetadata, emptyRelations, 'flux://has-signal', ['flux://post/1'], { signalTypeId: 'like' });
|
|
449
|
+
expect(sparql).toContain('parse_literal');
|
|
450
|
+
expect(sparql).toContain('"like"');
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
// ─────────────────────────────────────────────────────────────
|
|
454
|
+
// parseSparqlGroupedCount
|
|
455
|
+
// ─────────────────────────────────────────────────────────────
|
|
456
|
+
describe('parseSparqlGroupedCount', () => {
|
|
457
|
+
it('returns an empty map for empty input', () => {
|
|
458
|
+
expect(parseSparqlGroupedCount([]).size).toBe(0);
|
|
459
|
+
});
|
|
460
|
+
it('returns an empty map for non-array input', () => {
|
|
461
|
+
expect(parseSparqlGroupedCount(null).size).toBe(0);
|
|
462
|
+
});
|
|
463
|
+
it('parses RDF binding objects ({ parent: { value: ... }, count: { value: ... } })', () => {
|
|
464
|
+
const rows = [
|
|
465
|
+
{ parent: { value: 'flux://post/1' }, count: { value: '3' } },
|
|
466
|
+
{ parent: { value: 'flux://post/2' }, count: { value: '0' } },
|
|
467
|
+
];
|
|
468
|
+
const map = parseSparqlGroupedCount(rows);
|
|
469
|
+
expect(map.get('flux://post/1')).toBe(3);
|
|
470
|
+
expect(map.get('flux://post/2')).toBe(0);
|
|
471
|
+
});
|
|
472
|
+
it('parses plain string/number values (non-RDF binding format)', () => {
|
|
473
|
+
const rows = [
|
|
474
|
+
{ parent: 'flux://post/1', count: 5 },
|
|
475
|
+
{ parent: 'flux://post/2', count: '2' },
|
|
476
|
+
];
|
|
477
|
+
const map = parseSparqlGroupedCount(rows);
|
|
478
|
+
expect(map.get('flux://post/1')).toBe(5);
|
|
479
|
+
expect(map.get('flux://post/2')).toBe(2);
|
|
480
|
+
});
|
|
481
|
+
it('skips rows with missing parent or count', () => {
|
|
482
|
+
const rows = [
|
|
483
|
+
{ parent: { value: 'flux://post/1' } },
|
|
484
|
+
{ count: { value: '3' } },
|
|
485
|
+
{ parent: { value: 'flux://post/3' }, count: { value: '1' } },
|
|
486
|
+
];
|
|
487
|
+
const map = parseSparqlGroupedCount(rows);
|
|
488
|
+
expect(map.size).toBe(1);
|
|
489
|
+
expect(map.get('flux://post/3')).toBe(1);
|
|
490
|
+
});
|
|
491
|
+
it('skips rows with NaN count', () => {
|
|
492
|
+
const rows = [{ parent: { value: 'flux://post/1' }, count: { value: 'not-a-number' } }];
|
|
493
|
+
expect(parseSparqlGroupedCount(rows).size).toBe(0);
|
|
494
|
+
});
|
|
495
|
+
});
|
|
@@ -1,35 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Shared
|
|
2
|
+
* Shared query compilation utilities.
|
|
3
3
|
*
|
|
4
4
|
* These functions are used by both the runtime query builder (`Ad4mModel`) and
|
|
5
5
|
* the compile-time SHACL generator (`decorators.ts`).
|
|
6
|
+
*
|
|
7
|
+
* All output uses native SPARQL syntax.
|
|
6
8
|
*/
|
|
7
9
|
import type { WhereCondition, Where, ModelMetadata } from "./types";
|
|
8
10
|
/**
|
|
9
|
-
* Format a JavaScript value for embedding in a
|
|
11
|
+
* Format a JavaScript value for embedding in a query expression.
|
|
10
12
|
*
|
|
11
13
|
* - Strings are single-quoted with backslash / quote / newline escaping
|
|
12
14
|
* - Numbers and booleans are passed through as-is
|
|
13
15
|
* - Arrays are recursed
|
|
14
16
|
*/
|
|
15
|
-
export declare function
|
|
17
|
+
export declare function formatQueryValue(value: any): string;
|
|
16
18
|
/**
|
|
17
|
-
* Compile a single Where condition for a property into a
|
|
18
|
-
*
|
|
19
|
+
* Compile a single Where condition for a property into a SPARQL triple pattern
|
|
20
|
+
* that can be embedded in a getter's WHERE clause.
|
|
19
21
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
+
* The generated pattern operates on `?target` — e.g.:
|
|
23
|
+
* `?target <pred> <value> .`
|
|
24
|
+
* `FILTER EXISTS { ?target <pred> ?_v0 }`
|
|
22
25
|
*
|
|
23
26
|
* @param predicate - The predicate URI for the property
|
|
24
27
|
* @param condition - The Where condition value
|
|
25
28
|
* @param opts - Optional settings (e.g. `resolveLanguage`)
|
|
26
|
-
* @returns A single
|
|
29
|
+
* @returns A single SPARQL condition string
|
|
27
30
|
*/
|
|
28
31
|
export declare function buildWhereCondition(predicate: string, condition: WhereCondition, opts?: {
|
|
29
32
|
resolveLanguage?: string;
|
|
30
33
|
}): string;
|
|
31
34
|
/**
|
|
32
|
-
* Compile a full `Where` clause to an array of
|
|
35
|
+
* Compile a full `Where` clause to an array of SPARQL condition strings.
|
|
33
36
|
*
|
|
34
37
|
* When `metadata` is provided, property names are resolved to predicates
|
|
35
38
|
* using the model's property metadata. When `metadata` is `null`, property
|