@coasys/ad4m 0.13.0-test-6 → 0.13.0-test-7
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 +111 -36
- package/lib/index.js +111 -36
- package/lib/src/model/Ad4mModel.d.ts +15 -0
- package/lib/src/model/Ad4mModel.js +94 -28
- package/lib/src/model/Ad4mModel.test.js +1434 -688
- package/lib/src/model/ModelQueryBuilder.js +17 -8
- package/lib/src/model/Subject.js +6 -6
- 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/model-manifest.d.ts +41 -0
- package/lib/src/model/model-manifest.js +11 -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.test.d.ts +1 -0
- package/lib/src/model/query-sparql.test.js +312 -0
- package/lib/src/model/relation-filtering.test.js +15 -15
- 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/perspectives/PerspectiveClient.test.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveClient.test.js +224 -0
- package/lib/src/perspectives/PerspectiveResolver.d.ts +0 -1
- package/lib/src/perspectives/PerspectiveResolver.js +0 -11
- 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/SHACLShape.test.js +82 -0
- package/package.json +8 -11
- package/src/model/Ad4mModel.ts +97 -26
- package/src/model/ModelQueryBuilder.ts +17 -7
- package/lib/src/model/query-surreal.d.ts +0 -44
- package/lib/src/model/query-surreal.js +0 -249
- package/lib/src/model/surreal-utils.d.ts +0 -42
- package/lib/src/model/surreal-utils.js +0 -128
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared SurrealQL compilation utilities.
|
|
3
|
-
*
|
|
4
|
-
* These functions are used by both the runtime query builder (`Ad4mModel`) and
|
|
5
|
-
* the compile-time SHACL generator (`decorators.ts`).
|
|
6
|
-
*/
|
|
7
|
-
import { escapeSurrealString } from "../utils";
|
|
8
|
-
/**
|
|
9
|
-
* Format a JavaScript value for embedding in a SurrealQL expression.
|
|
10
|
-
*
|
|
11
|
-
* - Strings are single-quoted with backslash / quote / newline escaping
|
|
12
|
-
* - Numbers and booleans are passed through as-is
|
|
13
|
-
* - Arrays are recursed
|
|
14
|
-
*/
|
|
15
|
-
export function formatSurrealValue(value) {
|
|
16
|
-
if (typeof value === 'string') {
|
|
17
|
-
// Escape backslashes first, then single quotes and other special characters
|
|
18
|
-
const escaped = value
|
|
19
|
-
.replace(/\\/g, '\\\\') // Backslash -> \\
|
|
20
|
-
.replace(/'/g, "\\'") // Single quote -> \'
|
|
21
|
-
.replace(/"/g, '\\"') // Double quote -> \"
|
|
22
|
-
.replace(/\n/g, '\\n') // Newline -> \n
|
|
23
|
-
.replace(/\r/g, '\\r') // Carriage return -> \r
|
|
24
|
-
.replace(/\t/g, '\\t'); // Tab -> \t
|
|
25
|
-
return `'${escaped}'`;
|
|
26
|
-
}
|
|
27
|
-
else if (typeof value === 'number' || typeof value === 'boolean') {
|
|
28
|
-
return String(value);
|
|
29
|
-
}
|
|
30
|
-
else if (Array.isArray(value)) {
|
|
31
|
-
return `[${value.map(v => formatSurrealValue(v)).join(', ')}]`;
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
return String(value);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Compile a single Where condition for a property into a SurrealQL
|
|
39
|
-
* graph-traversal sub-expression.
|
|
40
|
-
*
|
|
41
|
-
* Operates on the **target node** — generates `count(->link[WHERE ...]) > 0`
|
|
42
|
-
* style checks that can be used inside a `[WHERE ...]` filter on nodes.
|
|
43
|
-
*
|
|
44
|
-
* @param predicate - The predicate URI for the property
|
|
45
|
-
* @param condition - The Where condition value
|
|
46
|
-
* @param opts - Optional settings (e.g. `resolveLanguage`)
|
|
47
|
-
* @returns A single SurrealQL condition string
|
|
48
|
-
*/
|
|
49
|
-
export function buildWhereCondition(predicate, condition, opts) {
|
|
50
|
-
const escapedPredicate = escapeSurrealString(predicate);
|
|
51
|
-
const targetField = opts?.resolveLanguage === 'literal'
|
|
52
|
-
? 'fn::parse_literal(out.uri)'
|
|
53
|
-
: 'out.uri';
|
|
54
|
-
if (Array.isArray(condition)) {
|
|
55
|
-
// Array values → IN clause
|
|
56
|
-
const formattedValues = condition.map(v => formatSurrealValue(v)).join(', ');
|
|
57
|
-
return `count(->link[WHERE predicate = '${escapedPredicate}' AND ${targetField} IN [${formattedValues}]]) > 0`;
|
|
58
|
-
}
|
|
59
|
-
else if (typeof condition === 'object' && condition !== null) {
|
|
60
|
-
// Operator object
|
|
61
|
-
const ops = condition;
|
|
62
|
-
const parts = [];
|
|
63
|
-
if (ops.not !== undefined) {
|
|
64
|
-
if (Array.isArray(ops.not)) {
|
|
65
|
-
const formattedValues = ops.not.map((v) => formatSurrealValue(v)).join(', ');
|
|
66
|
-
parts.push(`count(->link[WHERE predicate = '${escapedPredicate}' AND ${targetField} IN [${formattedValues}]]) = 0`);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
parts.push(`count(->link[WHERE predicate = '${escapedPredicate}' AND ${targetField} = ${formatSurrealValue(ops.not)}]) = 0`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
// Comparison operators — ensure property exists (actual comparison
|
|
73
|
-
// may be post-filtered in JS at runtime, but the SurrealQL condition
|
|
74
|
-
// is valid for compile-time getter generation too).
|
|
75
|
-
const hasComparisonOps = ops.gt !== undefined || ops.gte !== undefined ||
|
|
76
|
-
ops.lt !== undefined || ops.lte !== undefined ||
|
|
77
|
-
ops.between !== undefined || ops.contains !== undefined;
|
|
78
|
-
if (hasComparisonOps) {
|
|
79
|
-
parts.push(`count(->link[WHERE predicate = '${escapedPredicate}']) > 0`);
|
|
80
|
-
}
|
|
81
|
-
return parts.join(' AND ');
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
// Simple equality
|
|
85
|
-
return `count(->link[WHERE predicate = '${escapedPredicate}' AND ${targetField} = ${formatSurrealValue(condition)}]) > 0`;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Compile a full `Where` clause to an array of SurrealQL condition strings.
|
|
90
|
-
*
|
|
91
|
-
* When `metadata` is provided, property names are resolved to predicates
|
|
92
|
-
* using the model's property metadata. When `metadata` is `null`, property
|
|
93
|
-
* names are treated as raw predicate URIs.
|
|
94
|
-
*
|
|
95
|
-
* Only handles **regular properties** — special fields (`id`, `author`,
|
|
96
|
-
* `timestamp`) and relation fields are skipped (they are only relevant at
|
|
97
|
-
* runtime query time, not for compile-time getter generation).
|
|
98
|
-
*/
|
|
99
|
-
export function compileWhereClause(where, metadata) {
|
|
100
|
-
const conditions = [];
|
|
101
|
-
for (const [propertyName, condition] of Object.entries(where)) {
|
|
102
|
-
// Skip special fields that don't apply at compile time
|
|
103
|
-
if (['id', 'author', 'timestamp'].includes(propertyName))
|
|
104
|
-
continue;
|
|
105
|
-
let predicate;
|
|
106
|
-
let resolveLanguage;
|
|
107
|
-
if (metadata) {
|
|
108
|
-
const propMeta = metadata.properties[propertyName];
|
|
109
|
-
if (propMeta) {
|
|
110
|
-
predicate = propMeta.predicate;
|
|
111
|
-
resolveLanguage = propMeta.resolveLanguage;
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
// Property not found in metadata — treat name as raw predicate URI
|
|
115
|
-
predicate = propertyName;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
// No metadata — treat name as raw predicate URI
|
|
120
|
-
predicate = propertyName;
|
|
121
|
-
}
|
|
122
|
-
const cond = buildWhereCondition(predicate, condition, { resolveLanguage });
|
|
123
|
-
if (cond) {
|
|
124
|
-
conditions.push(cond);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return conditions;
|
|
128
|
-
}
|