@coasys/ad4m 0.12.0-rc1-dev.2 → 0.12.0-rc1-dev.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/jest.config.js +10 -3
- package/lib/index.cjs +6872 -5886
- package/lib/index.js +6868 -5874
- package/lib/src/Ad4mClient.d.ts +9 -16
- package/lib/src/Ad4mClient.js +46 -25
- package/lib/src/Literal.d.ts +1 -2
- package/lib/src/Literal.js +38 -35
- package/lib/src/SmartLiteral.d.ts +1 -2
- package/lib/src/SmartLiteral.js +30 -15
- package/lib/src/agent/AgentClient.d.ts +1 -4
- package/lib/src/agent/AgentClient.js +79 -99
- package/lib/src/ai/AIClient.d.ts +1 -2
- package/lib/src/ai/AIClient.js +224 -220
- package/lib/src/expression/ExpressionClient.d.ts +1 -1
- package/lib/src/expression/ExpressionClient.js +73 -86
- package/lib/src/index.d.ts +1 -1
- package/lib/src/index.js +1 -1
- package/lib/src/language/LanguageClient.d.ts +1 -1
- package/lib/src/language/LanguageClient.js +52 -52
- package/lib/src/model/Ad4mModel.d.ts +1351 -144
- package/lib/src/model/Ad4mModel.js +3737 -228
- package/lib/src/model/Ad4mModel.test.js +522 -979
- package/lib/src/model/Subject.d.ts +3 -3
- package/lib/src/model/Subject.js +25 -25
- package/lib/src/model/decorators.d.ts +444 -100
- package/lib/src/model/decorators.js +686 -321
- package/lib/src/model/util.d.ts +6 -6
- package/lib/src/model/util.js +24 -24
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +1 -5
- package/lib/src/neighbourhood/NeighbourhoodClient.js +179 -242
- package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +1 -2
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +28 -13
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +5 -1
- package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
- package/lib/src/perspectives/PerspectiveClient.js +240 -316
- package/lib/src/perspectives/PerspectiveProxy.d.ts +99 -61
- package/lib/src/perspectives/PerspectiveProxy.js +485 -832
- package/lib/src/runtime/RuntimeClient.d.ts +1 -5
- package/lib/src/runtime/RuntimeClient.js +238 -412
- package/lib/src/shacl/SHACLFlow.test.js +105 -164
- package/lib/src/shacl/SHACLShape.d.ts +12 -17
- package/lib/src/shacl/SHACLShape.js +115 -128
- package/lib/src/shacl/SHACLShape.test.js +176 -244
- package/lib/src/shims/reflect-metadata.d.ts +0 -0
- package/lib/src/shims/type-graphql.d.ts +32 -0
- package/lib/src/src/Ad4mClient.d.ts +28 -0
- package/lib/src/src/Address.d.ts +1 -0
- package/lib/src/src/DID.d.ts +1 -0
- package/lib/src/src/Exception.d.ts +7 -0
- package/lib/src/src/Literal.d.ts +7 -0
- package/lib/src/src/PubSub.d.ts +17 -0
- package/lib/src/src/SmartLiteral.d.ts +12 -0
- package/lib/src/src/agent/Agent.d.ts +110 -0
- package/lib/src/src/agent/AgentClient.d.ts +62 -0
- package/lib/src/src/agent/AgentResolver.d.ts +30 -0
- package/lib/src/src/agent/AgentStatus.d.ts +8 -0
- package/lib/src/src/ai/AIClient.d.ts +32 -0
- package/lib/src/src/ai/AIResolver.d.ts +75 -0
- package/lib/src/src/ai/Tasks.d.ts +37 -0
- package/lib/src/src/buildSchema.d.ts +1 -0
- package/lib/src/src/expression/Expression.d.ts +28 -0
- package/lib/src/src/expression/ExpressionClient.d.ts +13 -0
- package/lib/src/src/expression/ExpressionRef.d.ts +9 -0
- package/lib/src/src/expression/ExpressionResolver.d.ts +10 -0
- package/lib/src/src/index.d.ts +35 -0
- package/lib/src/src/jestSetup.d.ts +1 -0
- package/lib/src/src/language/Icon.d.ts +4 -0
- package/lib/src/src/language/Language.d.ts +206 -0
- package/lib/src/src/language/LanguageClient.d.ts +17 -0
- package/lib/src/src/language/LanguageContext.d.ts +37 -0
- package/lib/src/src/language/LanguageHandle.d.ts +9 -0
- package/lib/src/src/language/LanguageMeta.d.ts +35 -0
- package/lib/src/src/language/LanguageRef.d.ts +6 -0
- package/lib/src/src/language/LanguageResolver.d.ts +19 -0
- package/lib/src/src/links/Links.d.ts +39 -0
- package/lib/src/src/links/Links.test.d.ts +1 -0
- package/lib/src/src/model/Ad4mModel.d.ts +1438 -0
- package/lib/src/src/model/Ad4mModel.test.d.ts +1 -0
- package/lib/src/src/model/Subject.d.ts +25 -0
- package/lib/src/src/model/decorators.d.ts +545 -0
- package/lib/src/src/model/util.d.ts +12 -0
- package/lib/src/src/neighbourhood/Neighbourhood.d.ts +10 -0
- package/lib/src/src/neighbourhood/NeighbourhoodClient.d.ts +25 -0
- package/lib/src/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -0
- package/lib/src/src/neighbourhood/NeighbourhoodProxy.test.d.ts +1 -0
- package/lib/src/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -0
- package/lib/src/src/perspectives/LinkQuery.d.ts +11 -0
- package/lib/src/src/perspectives/Perspective.d.ts +38 -0
- package/lib/src/src/perspectives/Perspective.test.d.ts +1 -0
- package/lib/src/src/perspectives/PerspectiveClient.d.ts +97 -0
- package/lib/src/src/perspectives/PerspectiveDiff.d.ts +9 -0
- package/lib/src/src/perspectives/PerspectiveHandle.d.ts +18 -0
- package/lib/src/src/perspectives/PerspectiveProxy.d.ts +865 -0
- package/lib/src/src/perspectives/PerspectiveResolver.d.ts +57 -0
- package/lib/src/src/runtime/RuntimeClient.d.ts +56 -0
- package/lib/src/src/runtime/RuntimeResolver.d.ts +132 -0
- package/lib/src/src/shacl/SHACLFlow.d.ts +171 -0
- package/lib/src/src/shacl/SHACLFlow.test.d.ts +1 -0
- package/lib/src/src/shacl/SHACLShape.d.ts +117 -0
- package/lib/src/src/shacl/SHACLShape.test.d.ts +1 -0
- package/lib/src/src/typeDefs.d.ts +1 -0
- package/lib/src/src/unwrapApolloResult.d.ts +2 -0
- package/lib/src/src/utils.d.ts +25 -0
- package/lib/src/utils.d.ts +0 -8
- package/lib/src/utils.js +7 -21
- package/package.json +15 -14
- package/src/Ad4mClient.test.ts +1315 -1564
- package/src/Ad4mClient.ts +54 -55
- package/src/Literal.test.ts +26 -45
- package/src/Literal.ts +66 -82
- package/src/SmartLiteral.ts +58 -74
- package/src/agent/AgentClient.ts +99 -144
- package/src/ai/AIClient.ts +359 -409
- package/src/expression/ExpressionClient.ts +96 -142
- package/src/index.ts +1 -1
- package/src/language/LanguageClient.ts +113 -137
- package/src/model/Ad4mModel.test.ts +616 -1114
- package/src/model/Ad4mModel.ts +4361 -451
- package/src/model/Subject.ts +159 -0
- package/src/model/decorators.ts +1168 -667
- package/src/model/util.ts +54 -53
- package/src/neighbourhood/NeighbourhoodClient.ts +266 -392
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +7 -3
- package/src/neighbourhood/NeighbourhoodProxy.ts +40 -58
- package/src/perspectives/PerspectiveClient.ts +540 -779
- package/src/perspectives/PerspectiveProxy.ts +2067 -2860
- package/src/runtime/RuntimeClient.ts +476 -775
- package/src/shacl/SHACLFlow.test.ts +129 -196
- package/src/shacl/SHACLShape.test.ts +190 -284
- package/src/shacl/SHACLShape.ts +200 -224
- package/src/utils.ts +17 -31
- package/tsconfig.json +10 -9
- package/src/model/IMPROVEMENTS.md +0 -482
- package/src/model/PREDICATE_COLLISION_FIX.md +0 -651
- package/src/model/README.md +0 -317
- package/src/model/SHACL_MUTATION_DECOUPLING.md +0 -356
- package/src/model/SUBSCRIPTION_STRATEGY.md +0 -317
- package/src/model/TC39_STAGE_3_DECORATORS.md +0 -609
- package/src/model/mutation.ts +0 -461
- package/src/model/parentUtils.ts +0 -91
- package/src/model/prolog/generatePrologFacts.ts +0 -176
- package/src/model/prolog/index.ts +0 -1
- package/src/model/query/ModelQueryBuilder.ts +0 -173
- package/src/model/query/fetchInstance.ts +0 -181
- package/src/model/query/hydration.ts +0 -273
- package/src/model/query/operations.ts +0 -510
- package/src/model/query/snapshot.ts +0 -126
- package/src/model/query/surrealCompiler.ts +0 -376
- package/src/model/schema/fromJSONSchema.ts +0 -411
- package/src/model/schema/metadata.ts +0 -148
- package/src/model/subscription.ts +0 -307
- package/src/model/transaction.ts +0 -60
- package/src/model/types.ts +0 -207
|
@@ -1,376 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure SurrealQL compiler — translates high-level `Query<T>` objects into
|
|
3
|
-
* raw SurrealQL strings.
|
|
4
|
-
*
|
|
5
|
-
* All functions are stateless — they take `ModelMetadata` (and optionally a
|
|
6
|
-
* `Query`) as plain data and return strings or booleans. No dependency on the
|
|
7
|
-
* `Ad4mModel` class, which means they can be imported by tests and other
|
|
8
|
-
* modules without pulling in the full class graph.
|
|
9
|
-
*
|
|
10
|
-
* `Ad4mModel` keeps thin static wrapper methods with the original signatures so
|
|
11
|
-
* the public API is unchanged.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { escapeSurrealString } from "../../utils";
|
|
15
|
-
import {
|
|
16
|
-
ModelMetadata,
|
|
17
|
-
Query,
|
|
18
|
-
Where,
|
|
19
|
-
WhereCondition,
|
|
20
|
-
ParentQueryByPredicate,
|
|
21
|
-
} from "../types";
|
|
22
|
-
|
|
23
|
-
// ── Value formatting ────────────────────────────────────────────────────────
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Formats a value for use in SurrealQL queries.
|
|
27
|
-
*
|
|
28
|
-
* - Strings: wrapped in single quotes, special characters escaped
|
|
29
|
-
* - Numbers / booleans: converted to string
|
|
30
|
-
* - Arrays: recursively formatted, wrapped in `[...]`
|
|
31
|
-
*/
|
|
32
|
-
export function formatSurrealValue(value: any): string {
|
|
33
|
-
if (typeof value === "string") {
|
|
34
|
-
const escaped = value
|
|
35
|
-
.replace(/\\/g, "\\\\")
|
|
36
|
-
.replace(/'/g, "\\'")
|
|
37
|
-
.replace(/"/g, '\\"')
|
|
38
|
-
.replace(/\n/g, "\\n")
|
|
39
|
-
.replace(/\r/g, "\\r")
|
|
40
|
-
.replace(/\t/g, "\\t");
|
|
41
|
-
return `'${escaped}'`;
|
|
42
|
-
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
43
|
-
return String(value);
|
|
44
|
-
} else if (Array.isArray(value)) {
|
|
45
|
-
return `[${value.map(formatSurrealValue).join(", ")}]`;
|
|
46
|
-
} else {
|
|
47
|
-
return String(value);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// ── WHERE clause builders ───────────────────────────────────────────────────
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Builds graph-traversal WHERE clause filters (`->link[WHERE ...]`).
|
|
55
|
-
* Used by `buildSurrealQuery` — more efficient than subqueries because
|
|
56
|
-
* SurrealDB can use graph indexes.
|
|
57
|
-
*/
|
|
58
|
-
export function buildGraphTraversalWhereClause(
|
|
59
|
-
metadata: ModelMetadata,
|
|
60
|
-
where?: Where,
|
|
61
|
-
): string {
|
|
62
|
-
if (!where) return "";
|
|
63
|
-
|
|
64
|
-
const conditions: string[] = [];
|
|
65
|
-
|
|
66
|
-
for (const [propertyName, condition] of Object.entries(where)) {
|
|
67
|
-
const isSpecial = ["base", "id", "author", "timestamp"].includes(
|
|
68
|
-
propertyName,
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
if (isSpecial) {
|
|
72
|
-
if (propertyName === "author" || propertyName === "timestamp") {
|
|
73
|
-
continue; // filtered post-query in JavaScript
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const columnName = "uri"; // base/id → uri in node table
|
|
77
|
-
|
|
78
|
-
if (Array.isArray(condition)) {
|
|
79
|
-
const formattedValues = condition.map(formatSurrealValue).join(", ");
|
|
80
|
-
conditions.push(`${columnName} IN [${formattedValues}]`);
|
|
81
|
-
} else if (typeof condition === "object" && condition !== null) {
|
|
82
|
-
const ops = condition as any;
|
|
83
|
-
if (ops.not !== undefined) {
|
|
84
|
-
if (Array.isArray(ops.not)) {
|
|
85
|
-
const formattedValues = ops.not.map(formatSurrealValue).join(", ");
|
|
86
|
-
conditions.push(`${columnName} NOT IN [${formattedValues}]`);
|
|
87
|
-
} else {
|
|
88
|
-
conditions.push(`${columnName} != ${formatSurrealValue(ops.not)}`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
if (
|
|
92
|
-
ops.between !== undefined &&
|
|
93
|
-
Array.isArray(ops.between) &&
|
|
94
|
-
ops.between.length === 2
|
|
95
|
-
) {
|
|
96
|
-
conditions.push(
|
|
97
|
-
`${columnName} >= ${formatSurrealValue(ops.between[0])} AND ${columnName} <= ${formatSurrealValue(ops.between[1])}`,
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
if (ops.gt !== undefined)
|
|
101
|
-
conditions.push(`${columnName} > ${formatSurrealValue(ops.gt)}`);
|
|
102
|
-
if (ops.gte !== undefined)
|
|
103
|
-
conditions.push(`${columnName} >= ${formatSurrealValue(ops.gte)}`);
|
|
104
|
-
if (ops.lt !== undefined)
|
|
105
|
-
conditions.push(`${columnName} < ${formatSurrealValue(ops.lt)}`);
|
|
106
|
-
if (ops.lte !== undefined)
|
|
107
|
-
conditions.push(`${columnName} <= ${formatSurrealValue(ops.lte)}`);
|
|
108
|
-
if (ops.contains !== undefined)
|
|
109
|
-
conditions.push(
|
|
110
|
-
`${columnName} CONTAINS ${formatSurrealValue(ops.contains)}`,
|
|
111
|
-
);
|
|
112
|
-
} else {
|
|
113
|
-
conditions.push(`${columnName} = ${formatSurrealValue(condition)}`);
|
|
114
|
-
}
|
|
115
|
-
} else {
|
|
116
|
-
const propMeta = metadata.properties[propertyName];
|
|
117
|
-
if (!propMeta) {
|
|
118
|
-
// ── Relation-based where clause ────────────────────────────────────
|
|
119
|
-
// If the where key is a relation name (e.g. `where: { post: postId }`),
|
|
120
|
-
// generate a graph-traversal filter on the link predicate.
|
|
121
|
-
//
|
|
122
|
-
// • Reverse relations (@BelongsToOne / @BelongsToMany):
|
|
123
|
-
// The link is stored as parentId --predicate--> thisId.
|
|
124
|
-
// From this node we traverse <-link (incoming) and check in.uri.
|
|
125
|
-
//
|
|
126
|
-
// • Forward relations (@HasMany / @HasOne):
|
|
127
|
-
// The link is stored as thisId --predicate--> childId.
|
|
128
|
-
// From this node we traverse ->link (outgoing) and check out.uri.
|
|
129
|
-
const relMeta = metadata.relations[propertyName];
|
|
130
|
-
if (!relMeta) continue;
|
|
131
|
-
|
|
132
|
-
const predicate = escapeSurrealString(relMeta.predicate);
|
|
133
|
-
const isReverse = relMeta.direction === "reverse";
|
|
134
|
-
const arrow = isReverse ? "<-" : "->";
|
|
135
|
-
const uriField = isReverse ? "in.uri" : "out.uri";
|
|
136
|
-
|
|
137
|
-
if (Array.isArray(condition)) {
|
|
138
|
-
const formattedValues = condition.map(formatSurrealValue).join(", ");
|
|
139
|
-
conditions.push(
|
|
140
|
-
`count(${arrow}link[WHERE predicate = '${predicate}' AND ${uriField} IN [${formattedValues}]]) > 0`,
|
|
141
|
-
);
|
|
142
|
-
} else if (typeof condition === "object" && condition !== null) {
|
|
143
|
-
const ops = condition as any;
|
|
144
|
-
if (ops.not !== undefined) {
|
|
145
|
-
if (Array.isArray(ops.not)) {
|
|
146
|
-
const formattedValues = ops.not
|
|
147
|
-
.map(formatSurrealValue)
|
|
148
|
-
.join(", ");
|
|
149
|
-
conditions.push(
|
|
150
|
-
`count(${arrow}link[WHERE predicate = '${predicate}' AND ${uriField} IN [${formattedValues}]]) = 0`,
|
|
151
|
-
);
|
|
152
|
-
} else {
|
|
153
|
-
conditions.push(
|
|
154
|
-
`count(${arrow}link[WHERE predicate = '${predicate}' AND ${uriField} = ${formatSurrealValue(ops.not)}]) = 0`,
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
// Comparison operators on relation IDs are unusual but handled:
|
|
159
|
-
// they'll be caught in JS post-filtering (step 6 in operations.ts).
|
|
160
|
-
} else {
|
|
161
|
-
conditions.push(
|
|
162
|
-
`count(${arrow}link[WHERE predicate = '${predicate}' AND ${uriField} = ${formatSurrealValue(condition)}]) > 0`,
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
continue;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const predicate = escapeSurrealString(propMeta.predicate);
|
|
170
|
-
// Scalar properties (resolveLanguage === "literal" or unset) store values as
|
|
171
|
-
// literal:// URIs — use fn::parse_literal to unwrap before comparing.
|
|
172
|
-
// Only non-literal custom languages (e.g. IPFS) store raw expression URLs.
|
|
173
|
-
const targetField =
|
|
174
|
-
!propMeta.resolveLanguage || propMeta.resolveLanguage === "literal"
|
|
175
|
-
? "fn::parse_literal(out.uri)"
|
|
176
|
-
: "out.uri";
|
|
177
|
-
|
|
178
|
-
if (Array.isArray(condition)) {
|
|
179
|
-
const formattedValues = condition.map(formatSurrealValue).join(", ");
|
|
180
|
-
conditions.push(
|
|
181
|
-
`count(->link[WHERE predicate = '${predicate}' AND ${targetField} IN [${formattedValues}]]) > 0`,
|
|
182
|
-
);
|
|
183
|
-
} else if (typeof condition === "object" && condition !== null) {
|
|
184
|
-
const ops = condition as any;
|
|
185
|
-
if (ops.not !== undefined) {
|
|
186
|
-
if (Array.isArray(ops.not)) {
|
|
187
|
-
const formattedValues = ops.not.map(formatSurrealValue).join(", ");
|
|
188
|
-
conditions.push(
|
|
189
|
-
`count(->link[WHERE predicate = '${predicate}' AND ${targetField} IN [${formattedValues}]]) = 0`,
|
|
190
|
-
);
|
|
191
|
-
} else {
|
|
192
|
-
conditions.push(
|
|
193
|
-
`count(->link[WHERE predicate = '${predicate}' AND ${targetField} = ${formatSurrealValue(ops.not)}]) = 0`,
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
const hasComparisonOps =
|
|
198
|
-
ops.gt !== undefined ||
|
|
199
|
-
ops.gte !== undefined ||
|
|
200
|
-
ops.lt !== undefined ||
|
|
201
|
-
ops.lte !== undefined ||
|
|
202
|
-
ops.between !== undefined ||
|
|
203
|
-
ops.contains !== undefined;
|
|
204
|
-
if (hasComparisonOps) {
|
|
205
|
-
conditions.push(
|
|
206
|
-
`count(->link[WHERE predicate = '${predicate}']) > 0`,
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
} else {
|
|
210
|
-
conditions.push(
|
|
211
|
-
`count(->link[WHERE predicate = '${predicate}' AND ${targetField} = ${formatSurrealValue(condition)}]) > 0`,
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return conditions.join(" AND ");
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// ── Main query builders ─────────────────────────────────────────────────────
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Builds the SurrealQL SELECT query for a given model metadata + query params.
|
|
224
|
-
* This is the extracted, pure-function form of `Ad4mModel.queryToSurrealQL`.
|
|
225
|
-
*/
|
|
226
|
-
export function buildSurrealQuery(
|
|
227
|
-
metadata: ModelMetadata,
|
|
228
|
-
query: Query,
|
|
229
|
-
): string {
|
|
230
|
-
const { where, parent: linkedFrom } = query as Query & {
|
|
231
|
-
parent?: ParentQueryByPredicate;
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
const graphTraversalFilters: string[] = [];
|
|
235
|
-
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
236
|
-
if (propMeta.required) {
|
|
237
|
-
if (propMeta.flag && propMeta.initial) {
|
|
238
|
-
graphTraversalFilters.push(
|
|
239
|
-
`count(->link[WHERE predicate = '${escapeSurrealString(propMeta.predicate)}' AND out.uri = '${escapeSurrealString(propMeta.initial)}']) > 0`,
|
|
240
|
-
);
|
|
241
|
-
} else {
|
|
242
|
-
graphTraversalFilters.push(
|
|
243
|
-
`count(->link[WHERE predicate = '${escapeSurrealString(propMeta.predicate)}']) > 0`,
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (graphTraversalFilters.length === 0) {
|
|
250
|
-
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
251
|
-
if (propMeta.initial) {
|
|
252
|
-
if (propMeta.flag) {
|
|
253
|
-
graphTraversalFilters.push(
|
|
254
|
-
`count(->link[WHERE predicate = '${escapeSurrealString(propMeta.predicate)}' AND out.uri = '${escapeSurrealString(propMeta.initial)}']) > 0`,
|
|
255
|
-
);
|
|
256
|
-
} else {
|
|
257
|
-
graphTraversalFilters.push(
|
|
258
|
-
`count(->link[WHERE predicate = '${escapeSurrealString(propMeta.predicate)}']) > 0`,
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// Last-resort fallback: require at least one link with any of the model's
|
|
267
|
-
// own declared predicates (properties OR relations). This prevents matching
|
|
268
|
-
// SDNA registration nodes (which use sh://, ad4m://, rdf:// predicates) for
|
|
269
|
-
// models that have no @Flag, no required, and no initial-valued properties.
|
|
270
|
-
if (graphTraversalFilters.length === 0) {
|
|
271
|
-
const allPredicates = [
|
|
272
|
-
...Object.values(metadata.properties).map((p) => p.predicate),
|
|
273
|
-
...Object.values(metadata.relations).map((r) => r.predicate),
|
|
274
|
-
].filter(Boolean);
|
|
275
|
-
|
|
276
|
-
if (allPredicates.length > 0) {
|
|
277
|
-
const predicateConditions = allPredicates
|
|
278
|
-
.map(
|
|
279
|
-
(p) =>
|
|
280
|
-
`count(->link[WHERE predicate = '${escapeSurrealString(p)}']) > 0`,
|
|
281
|
-
)
|
|
282
|
-
.join(" OR ");
|
|
283
|
-
graphTraversalFilters.push(`(${predicateConditions})`);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
const userWhereClause = buildGraphTraversalWhereClause(metadata, where);
|
|
288
|
-
|
|
289
|
-
const whereConditions: string[] = [
|
|
290
|
-
...graphTraversalFilters,
|
|
291
|
-
...(userWhereClause ? [userWhereClause] : []),
|
|
292
|
-
...(linkedFrom
|
|
293
|
-
? [
|
|
294
|
-
`count(<-link[WHERE predicate = '${escapeSurrealString(linkedFrom.predicate)}' AND in.uri = '${escapeSurrealString(linkedFrom.id)}']) > 0`,
|
|
295
|
-
]
|
|
296
|
-
: []),
|
|
297
|
-
];
|
|
298
|
-
|
|
299
|
-
// Ensure we always have at least one condition to produce valid SurrealQL
|
|
300
|
-
const whereClause =
|
|
301
|
-
whereConditions.length > 0
|
|
302
|
-
? whereConditions.join(" AND ")
|
|
303
|
-
: "count(->link) > 0";
|
|
304
|
-
|
|
305
|
-
return `
|
|
306
|
-
SELECT
|
|
307
|
-
id AS source,
|
|
308
|
-
uri AS source_uri,
|
|
309
|
-
(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links
|
|
310
|
-
FROM node
|
|
311
|
-
WHERE ${whereClause}
|
|
312
|
-
`.trim();
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* Builds a count query (same as `buildSurrealQuery` but without LIMIT/OFFSET).
|
|
317
|
-
*/
|
|
318
|
-
export function buildSurrealCountQuery(
|
|
319
|
-
metadata: ModelMetadata,
|
|
320
|
-
query: Query,
|
|
321
|
-
): string {
|
|
322
|
-
const countQuery = { ...query };
|
|
323
|
-
delete countQuery.limit;
|
|
324
|
-
delete countQuery.offset;
|
|
325
|
-
return buildSurrealQuery(metadata, countQuery);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
// ── Post-query filtering ────────────────────────────────────────────────────
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Checks whether a single value satisfies a WhereCondition.
|
|
332
|
-
* Used for post-query JavaScript filtering of operators that SurrealDB
|
|
333
|
-
* cannot evaluate reliably (gt/gte/lt/lte/between/contains on literals).
|
|
334
|
-
*/
|
|
335
|
-
export function matchesCondition(
|
|
336
|
-
value: any,
|
|
337
|
-
condition: WhereCondition,
|
|
338
|
-
): boolean {
|
|
339
|
-
if (Array.isArray(condition)) {
|
|
340
|
-
return (condition as any[]).includes(value);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
if (typeof condition === "object" && condition !== null) {
|
|
344
|
-
const ops = condition as any;
|
|
345
|
-
|
|
346
|
-
let allMet = true;
|
|
347
|
-
if (ops.not !== undefined)
|
|
348
|
-
allMet =
|
|
349
|
-
allMet &&
|
|
350
|
-
(Array.isArray(ops.not)
|
|
351
|
-
? !(ops.not as any[]).includes(value)
|
|
352
|
-
: value !== ops.not);
|
|
353
|
-
if (
|
|
354
|
-
ops.between !== undefined &&
|
|
355
|
-
Array.isArray(ops.between) &&
|
|
356
|
-
ops.between.length === 2
|
|
357
|
-
)
|
|
358
|
-
allMet = allMet && value >= ops.between[0] && value <= ops.between[1];
|
|
359
|
-
if (ops.gt !== undefined) allMet = allMet && value > ops.gt;
|
|
360
|
-
if (ops.gte !== undefined) allMet = allMet && value >= ops.gte;
|
|
361
|
-
if (ops.lt !== undefined) allMet = allMet && value < ops.lt;
|
|
362
|
-
if (ops.lte !== undefined) allMet = allMet && value <= ops.lte;
|
|
363
|
-
if (ops.contains !== undefined) {
|
|
364
|
-
if (typeof value === "string") {
|
|
365
|
-
allMet = allMet && value.includes(String(ops.contains));
|
|
366
|
-
} else if (Array.isArray(value)) {
|
|
367
|
-
allMet = allMet && value.includes(ops.contains);
|
|
368
|
-
} else {
|
|
369
|
-
allMet = false;
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
return allMet;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
return value === condition;
|
|
376
|
-
}
|