grafeo-ogm 2.2.0 → 2.3.0
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/CHANGELOG.md +83 -0
- package/README.md +49 -1
- package/dist/cjs/compilers/mutation.compiler.d.ts +94 -31
- package/dist/cjs/compilers/mutation.compiler.d.ts.map +1 -1
- package/dist/cjs/compilers/mutation.compiler.js +381 -343
- package/dist/cjs/compilers/mutation.compiler.js.map +1 -1
- package/dist/cjs/compilers/selection.compiler.d.ts +1 -1
- package/dist/cjs/compilers/selection.compiler.d.ts.map +1 -1
- package/dist/cjs/compilers/selection.compiler.js +33 -39
- package/dist/cjs/compilers/selection.compiler.js.map +1 -1
- package/dist/cjs/compilers/where.compiler.d.ts +124 -1
- package/dist/cjs/compilers/where.compiler.d.ts.map +1 -1
- package/dist/cjs/compilers/where.compiler.js +371 -102
- package/dist/cjs/compilers/where.compiler.js.map +1 -1
- package/dist/cjs/interface-model.d.ts +20 -10
- package/dist/cjs/interface-model.d.ts.map +1 -1
- package/dist/cjs/interface-model.js +47 -104
- package/dist/cjs/interface-model.js.map +1 -1
- package/dist/cjs/model.d.ts +4 -29
- package/dist/cjs/model.d.ts.map +1 -1
- package/dist/cjs/model.js +46 -129
- package/dist/cjs/model.js.map +1 -1
- package/dist/cjs/policy/nested-writes.d.ts +35 -0
- package/dist/cjs/policy/nested-writes.d.ts.map +1 -0
- package/dist/cjs/policy/nested-writes.js +199 -0
- package/dist/cjs/policy/nested-writes.js.map +1 -0
- package/dist/cjs/policy/types.d.ts +18 -0
- package/dist/cjs/policy/types.d.ts.map +1 -1
- package/dist/cjs/policy/types.js +24 -0
- package/dist/cjs/policy/types.js.map +1 -1
- package/dist/esm/compilers/mutation.compiler.d.ts +94 -31
- package/dist/esm/compilers/mutation.compiler.d.ts.map +1 -1
- package/dist/esm/compilers/mutation.compiler.js +381 -344
- package/dist/esm/compilers/mutation.compiler.js.map +1 -1
- package/dist/esm/compilers/selection.compiler.d.ts +1 -1
- package/dist/esm/compilers/selection.compiler.d.ts.map +1 -1
- package/dist/esm/compilers/selection.compiler.js +33 -39
- package/dist/esm/compilers/selection.compiler.js.map +1 -1
- package/dist/esm/compilers/where.compiler.d.ts +124 -1
- package/dist/esm/compilers/where.compiler.d.ts.map +1 -1
- package/dist/esm/compilers/where.compiler.js +371 -104
- package/dist/esm/compilers/where.compiler.js.map +1 -1
- package/dist/esm/interface-model.d.ts +20 -10
- package/dist/esm/interface-model.d.ts.map +1 -1
- package/dist/esm/interface-model.js +47 -104
- package/dist/esm/interface-model.js.map +1 -1
- package/dist/esm/model.d.ts +4 -29
- package/dist/esm/model.d.ts.map +1 -1
- package/dist/esm/model.js +47 -130
- package/dist/esm/model.js.map +1 -1
- package/dist/esm/policy/nested-writes.d.ts +35 -0
- package/dist/esm/policy/nested-writes.d.ts.map +1 -0
- package/dist/esm/policy/nested-writes.js +193 -0
- package/dist/esm/policy/nested-writes.js.map +1 -0
- package/dist/esm/policy/types.d.ts +18 -0
- package/dist/esm/policy/types.d.ts.map +1 -1
- package/dist/esm/policy/types.js +23 -0
- package/dist/esm/policy/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WhereCompiler = void 0;
|
|
4
|
+
exports.stitchUserAndPolicy = stitchUserAndPolicy;
|
|
5
|
+
exports.traversalBundle = traversalBundle;
|
|
4
6
|
exports.policyValueExpression = policyValueExpression;
|
|
5
7
|
const errors_1 = require("../errors");
|
|
6
8
|
const types_1 = require("../policy/types");
|
|
@@ -122,6 +124,104 @@ class WhereCompiler {
|
|
|
122
124
|
policy: split?.policy ?? null,
|
|
123
125
|
};
|
|
124
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* The policy clause guarding a node of a possibly ABSTRACT type for one
|
|
129
|
+
* operation (v2.3.0) — the single construction behind every target-
|
|
130
|
+
* policy consumer (nested selection, traversal filters, nested writes)
|
|
131
|
+
* and `InterfaceModel`'s root clause:
|
|
132
|
+
*
|
|
133
|
+
* - concrete type → its own composed clause for `op` (byte-identical
|
|
134
|
+
* to compiling it directly); `null` when it has no policy for `op`
|
|
135
|
+
* or an override fires.
|
|
136
|
+
* - interface / union →
|
|
137
|
+
* `(CASE WHEN v:M1 THEN <M1 clause> WHEN v:M2 THEN … ELSE false END)`
|
|
138
|
+
* over the concrete members, each branch being exactly that member's
|
|
139
|
+
* own clause (`resolveForType(M, op)` already folds in M's interface
|
|
140
|
+
* policies). A member without a policy for `op`, or whose override
|
|
141
|
+
* fires, is `true`; when EVERY member is `true` the result is `null`
|
|
142
|
+
* (unconstrained, like a concrete type without policies). `ELSE false`
|
|
143
|
+
* excludes a node carrying no known member label (defense in depth).
|
|
144
|
+
*
|
|
145
|
+
* Pre-2.3.0 abstract targets resolved the ABSTRACT type's name, so the
|
|
146
|
+
* implementers' own policies were never applied through relationships.
|
|
147
|
+
*
|
|
148
|
+
* `fallbackOp`: resolve it for a type with NO policy for `op` — the
|
|
149
|
+
* `aggregate` → `read` fallback `Model.aggregate` applies at the root.
|
|
150
|
+
*
|
|
151
|
+
* `preludes` (for `@cypher` fields referenced by a policy) are returned
|
|
152
|
+
* to the caller; nested contexts reject them.
|
|
153
|
+
*
|
|
154
|
+
* @internal
|
|
155
|
+
*/
|
|
156
|
+
compileTargetPolicyClause(typeName, varName, op, policyContext, paramCounter, fallbackOp) {
|
|
157
|
+
const members = this.abstractMembers(typeName);
|
|
158
|
+
if (members === null)
|
|
159
|
+
return this.compileConcreteTargetClause(typeName, varName, op, policyContext, paramCounter, fallbackOp);
|
|
160
|
+
const params = {};
|
|
161
|
+
const preludes = [];
|
|
162
|
+
const branches = [];
|
|
163
|
+
let constrained = false;
|
|
164
|
+
for (const member of members) {
|
|
165
|
+
if (!this.schema.nodes.has(member))
|
|
166
|
+
continue;
|
|
167
|
+
const clause = this.compileConcreteTargetClause(member, varName, op, policyContext, paramCounter, fallbackOp);
|
|
168
|
+
(0, validation_1.mergeParams)(params, clause.params);
|
|
169
|
+
// Each member compiles in its own `@cypher` scope; chaining two
|
|
170
|
+
// scopes would drop the first one's aliases at the second's `WITH`.
|
|
171
|
+
if (preludes.length > 0 && clause.preludes.length > 0)
|
|
172
|
+
throw new errors_1.OGMError(`Policies on more than one member of "${typeName}" project @cypher ` +
|
|
173
|
+
`fields, which cannot be combined in one query. Refactor all but ` +
|
|
174
|
+
`one to stored properties.`);
|
|
175
|
+
preludes.push(...clause.preludes);
|
|
176
|
+
if (clause.cypher !== null)
|
|
177
|
+
constrained = true;
|
|
178
|
+
branches.push(`WHEN ${varName}:${(0, validation_1.escapeIdentifier)(member)} THEN ${clause.cypher ?? 'true'}`);
|
|
179
|
+
}
|
|
180
|
+
if (!constrained)
|
|
181
|
+
return { cypher: null, params: {}, preludes: [] };
|
|
182
|
+
return {
|
|
183
|
+
cypher: `(CASE ${branches.join(' ')} ELSE false END)`,
|
|
184
|
+
params,
|
|
185
|
+
preludes,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
compileConcreteTargetClause(typeName, varName, op, policyContext, paramCounter, fallbackOp) {
|
|
189
|
+
const nodeDef = this.schema.nodes.get(typeName);
|
|
190
|
+
let resolvedOp = op;
|
|
191
|
+
let resolved = policyContext.resolveForType(typeName, op);
|
|
192
|
+
if (!resolved && fallbackOp) {
|
|
193
|
+
resolvedOp = fallbackOp;
|
|
194
|
+
resolved = policyContext.resolveForType(typeName, fallbackOp);
|
|
195
|
+
}
|
|
196
|
+
if (!nodeDef || !resolved || resolved.overridden)
|
|
197
|
+
return { cypher: null, params: {}, preludes: [] };
|
|
198
|
+
const compiled = this.compile(undefined, varName, nodeDef, paramCounter, {
|
|
199
|
+
policyContext: {
|
|
200
|
+
ctx: policyContext.ctx,
|
|
201
|
+
operation: resolvedOp,
|
|
202
|
+
resolved,
|
|
203
|
+
resolveForType: policyContext.resolveForType,
|
|
204
|
+
defaults: policyContext.defaults,
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
return {
|
|
208
|
+
cypher: compiled.cypher || null,
|
|
209
|
+
params: compiled.params,
|
|
210
|
+
preludes: compiled.preludes ?? [],
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/** Concrete members of an interface / union; `null` for any other type. */
|
|
214
|
+
abstractMembers(typeName) {
|
|
215
|
+
if (this.schema.nodes.has(typeName))
|
|
216
|
+
return null;
|
|
217
|
+
const iface = this.schema.interfaces?.get(typeName);
|
|
218
|
+
if (iface)
|
|
219
|
+
return [...iface.implementedBy];
|
|
220
|
+
const union = this.schema.unions?.get(typeName);
|
|
221
|
+
if (union)
|
|
222
|
+
return [...union];
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
125
225
|
/**
|
|
126
226
|
* Shared body of `compile()` / `compileForExplain()`: compiles the user
|
|
127
227
|
* where, then the policy fragments, sharing one `paramCounter` and one
|
|
@@ -137,7 +237,9 @@ class WhereCompiler {
|
|
|
137
237
|
// Top-level scope — preludes here are returned to the caller for stitching
|
|
138
238
|
// BEFORE the WHERE clause. Nested scopes (relationship quantifiers) build
|
|
139
239
|
// their own scopes and stitch their preludes into the EXISTS body inline.
|
|
140
|
-
const
|
|
240
|
+
const ownsScope = options?.scope === undefined;
|
|
241
|
+
const scope = options?.scope ??
|
|
242
|
+
new cypher_field_projection_1.CypherFieldScope(nodeVar, options?.preserveVars ?? [], '__where');
|
|
141
243
|
const userBody = hasUserWhere
|
|
142
244
|
? this.compileConditions(where, nodeVar, nodeDef, paramCounter, 0, scope, undefined, policyContext)
|
|
143
245
|
: { cypher: '', params: {} };
|
|
@@ -150,7 +252,7 @@ class WhereCompiler {
|
|
|
150
252
|
return {
|
|
151
253
|
userCypher: userBody.cypher,
|
|
152
254
|
params,
|
|
153
|
-
preludes: scope.hasAny() ? scope.emit() : undefined,
|
|
255
|
+
preludes: ownsScope && scope.hasAny() ? scope.emit() : undefined,
|
|
154
256
|
policy,
|
|
155
257
|
};
|
|
156
258
|
}
|
|
@@ -268,9 +370,12 @@ class WhereCompiler {
|
|
|
268
370
|
buildTargetBundle(typeName, policyContext) {
|
|
269
371
|
if (!policyContext)
|
|
270
372
|
return undefined;
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
373
|
+
// v2.3.0 — a target WITHOUT a read policy still gets a bundle
|
|
374
|
+
// (`NO_ROOT_POLICY`: no clause of its own) so enforcement keeps
|
|
375
|
+
// cascading into deeper traversals. Pre-2.3.0 this returned
|
|
376
|
+
// `undefined`, making every policy-free type on a traversal path a
|
|
377
|
+
// gateway around the types beyond it (A → B(no policy) → C skipped C).
|
|
378
|
+
const targetPolicy = policyContext.resolveForType(typeName, 'read') ?? types_1.NO_ROOT_POLICY;
|
|
274
379
|
return {
|
|
275
380
|
ctx: policyContext.ctx,
|
|
276
381
|
operation: 'read',
|
|
@@ -279,6 +384,73 @@ class WhereCompiler {
|
|
|
279
384
|
defaults: policyContext.defaults,
|
|
280
385
|
};
|
|
281
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* Compile a traversal's filter against its target node with the
|
|
389
|
+
* caller's filter (`user`) and the target's `read` policy (`policy`)
|
|
390
|
+
* kept SEPARATE, so each quantifier composes them correctly — the
|
|
391
|
+
* policy must never sit inside a negation (v2.3.0). Pre-2.3.0 the policy
|
|
392
|
+
* was compiled INTO the filter, so `_ALL` (`NOT (user AND policy)`) was
|
|
393
|
+
* falsified by hidden related nodes (an existence oracle), and a
|
|
394
|
+
* connection `node_NOT` was SATISFIED by them.
|
|
395
|
+
*
|
|
396
|
+
* - no policy context → plain compile.
|
|
397
|
+
* - concrete target → one `compileUserAndPolicy` pass (shared `@cypher`
|
|
398
|
+
* prelude scope); the bundle cascades as `NO_ROOT_POLICY` at any depth.
|
|
399
|
+
* - interface target → filter via a `NO_ROOT_POLICY` traversal bundle,
|
|
400
|
+
* policy via `compileTargetPolicyClause` (CASE over the members'
|
|
401
|
+
* own clauses — pre-2.3.0 only the interface's own policies ran).
|
|
402
|
+
*
|
|
403
|
+
* `''` for an absent part. With `scope`, concrete-target preludes
|
|
404
|
+
* register into it (caller emits); otherwise they are returned.
|
|
405
|
+
* Shared with `SelectionCompiler`'s nested-projection filters.
|
|
406
|
+
*
|
|
407
|
+
* @internal
|
|
408
|
+
*/
|
|
409
|
+
compileTargetBody(where, targetVar, targetDef, counter, policyContext, scope) {
|
|
410
|
+
if (!policyContext) {
|
|
411
|
+
const plain = this.compile(where, targetVar, targetDef, counter, {
|
|
412
|
+
scope,
|
|
413
|
+
});
|
|
414
|
+
return {
|
|
415
|
+
user: plain.cypher,
|
|
416
|
+
policy: '',
|
|
417
|
+
params: plain.params,
|
|
418
|
+
preludes: plain.preludes ?? [],
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
if (this.abstractMembers(targetDef.typeName) === null) {
|
|
422
|
+
const split = this.compileUserAndPolicy(where, targetVar, targetDef, counter, {
|
|
423
|
+
policyContext: this.buildTargetBundle(targetDef.typeName, policyContext),
|
|
424
|
+
scope,
|
|
425
|
+
});
|
|
426
|
+
if (!split)
|
|
427
|
+
return { user: '', policy: '', params: {}, preludes: [] };
|
|
428
|
+
return {
|
|
429
|
+
user: split.userCypher,
|
|
430
|
+
policy: split.policy?.clause ?? '',
|
|
431
|
+
params: split.params,
|
|
432
|
+
preludes: split.preludes ?? [],
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
const userPart = this.compile(where, targetVar, targetDef, counter, {
|
|
436
|
+
policyContext: traversalBundle(policyContext),
|
|
437
|
+
scope,
|
|
438
|
+
});
|
|
439
|
+
const policyPart = this.compileTargetPolicyClause(targetDef.typeName, targetVar, 'read', policyContext, counter);
|
|
440
|
+
const userHasPreludes = (userPart.preludes?.length ?? 0) > 0 || (scope?.hasAny() ?? false);
|
|
441
|
+
if (userHasPreludes && policyPart.preludes.length > 0)
|
|
442
|
+
throw new errors_1.OGMError(`Filtering "${targetDef.typeName}" by @cypher fields is not supported ` +
|
|
443
|
+
`when a member type's policy also projects @cypher fields. Filter on ` +
|
|
444
|
+
`stored properties, or refactor the member policy.`);
|
|
445
|
+
const params = { ...userPart.params };
|
|
446
|
+
(0, validation_1.mergeParams)(params, policyPart.params);
|
|
447
|
+
return {
|
|
448
|
+
user: userPart.cypher,
|
|
449
|
+
policy: policyPart.cypher ?? '',
|
|
450
|
+
params,
|
|
451
|
+
preludes: [...(userPart.preludes ?? []), ...policyPart.preludes],
|
|
452
|
+
};
|
|
453
|
+
}
|
|
282
454
|
compileConditions(where, nodeVar, nodeDef, counter, depth, scope,
|
|
283
455
|
/**
|
|
284
456
|
* Optional shared params accumulator. When provided, every leaf
|
|
@@ -362,37 +534,10 @@ class WhereCompiler {
|
|
|
362
534
|
clauses.push(`NOT (${sub.cypher})`);
|
|
363
535
|
continue;
|
|
364
536
|
}
|
|
365
|
-
//
|
|
537
|
+
// Null values — the operator suffix is split off FIRST (v2.3.0); see
|
|
538
|
+
// `compileNullCondition`.
|
|
366
539
|
if (value === null) {
|
|
367
|
-
|
|
368
|
-
if (relDef) {
|
|
369
|
-
// Relationship null means "no such relationship exists"
|
|
370
|
-
const targetNodeDef = (0, utils_1.resolveTargetDef)(relDef.target, this.schema);
|
|
371
|
-
if (targetNodeDef) {
|
|
372
|
-
const relVar = `r${counter.count}`;
|
|
373
|
-
counter.count++;
|
|
374
|
-
const pattern = (0, utils_1.buildRelPattern)({
|
|
375
|
-
sourceVar: nodeVar,
|
|
376
|
-
relDef,
|
|
377
|
-
targetVar: relVar,
|
|
378
|
-
targetLabel: 'auto',
|
|
379
|
-
// Pass schema so that union/interface targets resolve to a
|
|
380
|
-
// labelless target (relationship-type-only filter). Without
|
|
381
|
-
// this, the literal abstract type name is escaped as a label
|
|
382
|
-
// that no concrete node carries → NOT EXISTS is true for
|
|
383
|
-
// every row → ALL rows match.
|
|
384
|
-
schema: this.schema,
|
|
385
|
-
});
|
|
386
|
-
clauses.push(`NOT EXISTS { MATCH ${pattern} }`);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
else {
|
|
390
|
-
// Scalar null means "property IS NULL". `@cypher` scalars need
|
|
391
|
-
// to project through the scope first (NULL check on the alias).
|
|
392
|
-
(0, validation_1.assertSafeIdentifier)(key, 'where clause');
|
|
393
|
-
const fieldRef = this.resolveFieldRef(key, nodeVar, nodeDef, scope);
|
|
394
|
-
clauses.push(`${fieldRef} IS NULL`);
|
|
395
|
-
}
|
|
540
|
+
clauses.push(this.compileNullCondition(key, nodeVar, nodeDef, counter, scope));
|
|
396
541
|
continue;
|
|
397
542
|
}
|
|
398
543
|
// Connection operators (e.g. hasStatusConnection_SOME)
|
|
@@ -494,16 +639,24 @@ class WhereCompiler {
|
|
|
494
639
|
? (this.schema.relationshipProperties.get(relDef.properties) ?? null)
|
|
495
640
|
: null;
|
|
496
641
|
const inner = this.compileConnectionWhereInput(value, relVar, edgeVar, targetNodeDef, propsDef, nodeScope, edgeScope, counter, depth, policyContext);
|
|
497
|
-
const
|
|
498
|
-
|
|
642
|
+
const innerParams = { ...inner.params };
|
|
643
|
+
// v2.3.0 — the target's `'read'` policy, composed ONCE over the whole
|
|
644
|
+
// connection body (node, node_NOT, edge and logical groups alike) and
|
|
645
|
+
// kept outside every negation. Compiled after the caller's filter so
|
|
646
|
+
// parameter numbering matches the pre-2.3.0 single-`node` shape.
|
|
647
|
+
const target = policyContext
|
|
648
|
+
? this.compileTargetBody(undefined, relVar, targetNodeDef, counter, policyContext, nodeScope)
|
|
649
|
+
: null;
|
|
650
|
+
const policy = target?.policy ?? '';
|
|
651
|
+
if (target)
|
|
652
|
+
(0, validation_1.mergeParams)(innerParams, target.params);
|
|
499
653
|
// Stitch the inner preludes (CALL { ... } + WITH ...) INSIDE the
|
|
500
654
|
// EXISTS body, between the MATCH pattern and the inner WHERE.
|
|
501
|
-
// - `nodeScope.emit()` —
|
|
502
|
-
//
|
|
655
|
+
// - `nodeScope.emit()` — node-side `@cypher` projections: the
|
|
656
|
+
// caller's `node` filters and (concrete target) the target policy.
|
|
503
657
|
// - `edgeScope.emit()` — same, but edge-side.
|
|
504
|
-
// - `inner.preludes`
|
|
505
|
-
//
|
|
506
|
-
// scopes.
|
|
658
|
+
// - `inner.preludes` / `target.preludes` — scopes owned elsewhere
|
|
659
|
+
// (an abstract target's member policies).
|
|
507
660
|
const innerPreludes = [];
|
|
508
661
|
if (nodeScope.hasAny())
|
|
509
662
|
innerPreludes.push(...nodeScope.emit());
|
|
@@ -511,10 +664,13 @@ class WhereCompiler {
|
|
|
511
664
|
innerPreludes.push(...edgeScope.emit());
|
|
512
665
|
if (inner.preludes && inner.preludes.length > 0)
|
|
513
666
|
innerPreludes.push(...inner.preludes);
|
|
667
|
+
if (target && target.preludes.length > 0)
|
|
668
|
+
innerPreludes.push(...target.preludes);
|
|
514
669
|
const preludeFragment = innerPreludes.length
|
|
515
670
|
? ` ${innerPreludes.join(' ')}`
|
|
516
671
|
: '';
|
|
517
|
-
const
|
|
672
|
+
const matched = stitchUserAndPolicy(inner.cypher, policy);
|
|
673
|
+
const whereClause = matched ? ` WHERE ${matched}` : '';
|
|
518
674
|
switch (connSuffix) {
|
|
519
675
|
case 'Connection':
|
|
520
676
|
case 'Connection_SOME':
|
|
@@ -529,9 +685,9 @@ class WhereCompiler {
|
|
|
529
685
|
params: innerParams,
|
|
530
686
|
};
|
|
531
687
|
case 'Connection_ALL':
|
|
532
|
-
if (
|
|
688
|
+
if (inner.cypher)
|
|
533
689
|
return {
|
|
534
|
-
cypher: `NOT EXISTS { MATCH ${pattern}${preludeFragment} WHERE
|
|
690
|
+
cypher: `NOT EXISTS { MATCH ${pattern}${preludeFragment} WHERE ${stitchAllCounterexample(inner.cypher, policy)} }`,
|
|
535
691
|
params: innerParams,
|
|
536
692
|
};
|
|
537
693
|
return { cypher: '', params: {} };
|
|
@@ -577,23 +733,23 @@ class WhereCompiler {
|
|
|
577
733
|
* `MATCH ${pattern}` and `WHERE ...` inside the EXISTS body.
|
|
578
734
|
*/
|
|
579
735
|
const extraPreludes = [];
|
|
580
|
-
// Resolve the target type's `'read'` policy ONCE per connection-where
|
|
581
|
-
// input. The same bundle is reused across every `node` / `node_NOT`
|
|
582
|
-
// key we encounter at this level (and across nested AND/OR/NOT
|
|
583
|
-
// groups via direct policyContext threading).
|
|
584
|
-
const targetBundle = this.buildTargetBundle(targetNodeDef.typeName, policyContext);
|
|
585
736
|
for (const [key, val] of Object.entries(value)) {
|
|
586
737
|
if (val === undefined)
|
|
587
738
|
continue;
|
|
588
739
|
if (key === 'node' || key === 'node_NOT') {
|
|
589
|
-
//
|
|
590
|
-
//
|
|
591
|
-
//
|
|
592
|
-
//
|
|
593
|
-
//
|
|
594
|
-
//
|
|
595
|
-
//
|
|
596
|
-
const nodeResult = this.compile(val, relVar, targetNodeDef, counter,
|
|
740
|
+
// v2.3.0 — the caller's node filter only. The target's `'read'`
|
|
741
|
+
// policy is composed ONCE by `tryCompileConnection` over the whole
|
|
742
|
+
// connection body, never here: stitched in here it sat inside the
|
|
743
|
+
// `node_NOT` negation (satisfied by hidden nodes) and was absent
|
|
744
|
+
// from edge-only filters. The traversal bundle keeps deeper
|
|
745
|
+
// traversals enforcing; `nodeScope` shares the `@cypher` chain
|
|
746
|
+
// with that policy.
|
|
747
|
+
const nodeResult = this.compile(val, relVar, targetNodeDef, counter, {
|
|
748
|
+
policyContext: policyContext
|
|
749
|
+
? traversalBundle(policyContext)
|
|
750
|
+
: undefined,
|
|
751
|
+
scope: nodeScope,
|
|
752
|
+
});
|
|
597
753
|
if (nodeResult.preludes && nodeResult.preludes.length > 0)
|
|
598
754
|
extraPreludes.push(...nodeResult.preludes);
|
|
599
755
|
if (nodeResult.cypher) {
|
|
@@ -713,28 +869,22 @@ class WhereCompiler {
|
|
|
713
869
|
// EXISTS never matches concrete-typed nodes.
|
|
714
870
|
schema: this.schema,
|
|
715
871
|
});
|
|
716
|
-
// v1.8.5 —
|
|
717
|
-
//
|
|
718
|
-
//
|
|
719
|
-
//
|
|
720
|
-
const
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
// produces. `compile()` manages its own inner scope and returns any
|
|
724
|
-
// preludes via `WhereResult.preludes` for stitching inside EXISTS.
|
|
725
|
-
const innerResult = this.compile(value, relVar, targetNodeDef, counter, targetBundle ? { policyContext: targetBundle } : undefined);
|
|
726
|
-
const innerPreludes = innerResult.preludes ?? [];
|
|
872
|
+
// v1.8.5 — the TARGET type's `'read'` policy applies when crossing the
|
|
873
|
+
// relationship boundary (CRIT-3). v2.3.0 — the caller's filter and the
|
|
874
|
+
// target's policy compile SEPARATELY and compose per quantifier with
|
|
875
|
+
// the policy OUTSIDE every negation (see `compileTargetBody`).
|
|
876
|
+
const body = this.compileTargetBody(value, relVar, targetNodeDef, counter, policyContext);
|
|
877
|
+
const matched = stitchUserAndPolicy(body.user, body.policy);
|
|
878
|
+
const innerPreludes = body.preludes;
|
|
727
879
|
const innerPreludeFragment = innerPreludes.length
|
|
728
880
|
? ` ${innerPreludes.join(' ')}`
|
|
729
881
|
: '';
|
|
730
|
-
const whereClause =
|
|
731
|
-
? ` WHERE ${innerResult.cypher}`
|
|
732
|
-
: '';
|
|
882
|
+
const whereClause = matched ? ` WHERE ${matched}` : '';
|
|
733
883
|
switch (suffix) {
|
|
734
884
|
case '_SOME':
|
|
735
885
|
return {
|
|
736
886
|
cypher: `EXISTS { MATCH ${pattern}${innerPreludeFragment}${whereClause} }`,
|
|
737
|
-
params:
|
|
887
|
+
params: body.params,
|
|
738
888
|
};
|
|
739
889
|
case '_NONE':
|
|
740
890
|
case '_NOT':
|
|
@@ -745,14 +895,16 @@ class WhereCompiler {
|
|
|
745
895
|
// Map → NULL → silent wrong rows.
|
|
746
896
|
return {
|
|
747
897
|
cypher: `NOT EXISTS { MATCH ${pattern}${innerPreludeFragment}${whereClause} }`,
|
|
748
|
-
params:
|
|
898
|
+
params: body.params,
|
|
749
899
|
};
|
|
750
900
|
case '_ALL':
|
|
751
|
-
//
|
|
752
|
-
|
|
901
|
+
// No VISIBLE related node fails the filter. Without a caller filter
|
|
902
|
+
// the quantifier is vacuous — hidden nodes must not falsify it
|
|
903
|
+
// (pre-2.3.0 `_ALL: {}` leaked their existence).
|
|
904
|
+
if (body.user)
|
|
753
905
|
return {
|
|
754
|
-
cypher: `NOT EXISTS { MATCH ${pattern}${innerPreludeFragment} WHERE
|
|
755
|
-
params:
|
|
906
|
+
cypher: `NOT EXISTS { MATCH ${pattern}${innerPreludeFragment} WHERE ${stitchAllCounterexample(body.user, body.policy)} }`,
|
|
907
|
+
params: body.params,
|
|
756
908
|
};
|
|
757
909
|
return { cypher: '', params: {} };
|
|
758
910
|
case '_SINGLE': {
|
|
@@ -762,7 +914,7 @@ class WhereCompiler {
|
|
|
762
914
|
// produces preludes — distinguish that case so the error message
|
|
763
915
|
// points at the right cause.
|
|
764
916
|
if (innerPreludes.length > 0) {
|
|
765
|
-
if (
|
|
917
|
+
if (body.policy)
|
|
766
918
|
throw new errors_1.OGMError(`Policy on "${targetNodeDef.typeName}" requires @cypher field projection, ` +
|
|
767
919
|
`which is not supported inside _SINGLE quantifiers. Refactor the policy ` +
|
|
768
920
|
`to use stored properties, or refactor the predicate to _SOME + _NONE.`);
|
|
@@ -777,7 +929,7 @@ class WhereCompiler {
|
|
|
777
929
|
// collisions if/when a future compiler shared this counter.
|
|
778
930
|
return {
|
|
779
931
|
cypher: `size([${relVar} IN [(${pattern}${whereClause} | ${relVar})] | ${relVar}]) = 1`,
|
|
780
|
-
params:
|
|
932
|
+
params: body.params,
|
|
781
933
|
};
|
|
782
934
|
}
|
|
783
935
|
default:
|
|
@@ -823,27 +975,27 @@ class WhereCompiler {
|
|
|
823
975
|
targetVar: relVar,
|
|
824
976
|
targetLabelRaw: labelStr,
|
|
825
977
|
});
|
|
826
|
-
//
|
|
827
|
-
//
|
|
828
|
-
|
|
829
|
-
//
|
|
830
|
-
// specified). When the member has its own `'read'` policy, we still
|
|
831
|
-
// re-enter `compile()` so the policy AND-stitches into the EXISTS
|
|
832
|
-
// body even when `memberValue` is empty/null.
|
|
978
|
+
// Caller filter and the member's own `'read'` policy, compiled
|
|
979
|
+
// separately (v2.3.0) so `_ALL` can keep the policy outside its
|
|
980
|
+
// negation. `_SOME`/`_NONE`/`_SINGLE` use the stitched form, which
|
|
981
|
+
// is byte-identical to the pre-2.3.0 single compile.
|
|
833
982
|
const memberWhere = memberValue;
|
|
834
|
-
const
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
983
|
+
const body = this.compileTargetBody(memberWhere, relVar, memberDef, counter, policyContext);
|
|
984
|
+
const inner = stitchUserAndPolicy(body.user, body.policy);
|
|
985
|
+
if (inner)
|
|
986
|
+
(0, validation_1.mergeParams)(allParams, body.params);
|
|
987
|
+
const preludeFragment = body.preludes.length
|
|
988
|
+
? ` ${body.preludes.join(' ')}`
|
|
989
|
+
: '';
|
|
990
|
+
members.push({
|
|
991
|
+
memberKey,
|
|
992
|
+
pattern,
|
|
993
|
+
relVar,
|
|
994
|
+
inner,
|
|
995
|
+
user: body.user,
|
|
996
|
+
policy: body.policy,
|
|
997
|
+
preludeFragment,
|
|
998
|
+
});
|
|
847
999
|
}
|
|
848
1000
|
if (members.length === 0)
|
|
849
1001
|
return { cypher: '', params: {} };
|
|
@@ -870,9 +1022,12 @@ class WhereCompiler {
|
|
|
870
1022
|
// satisfied (same as the non-union empty-inner short-circuit),
|
|
871
1023
|
// and union members absent from the input are unconstrained —
|
|
872
1024
|
// consistent with `_SOME`, where only mentioned members count.
|
|
1025
|
+
// v2.3.0 — only a VISIBLE member node (its policy holds) can fail
|
|
1026
|
+
// the predicate; a member with no caller filter is vacuous even
|
|
1027
|
+
// when it has a policy (pre-2.3.0 its hidden nodes falsified it).
|
|
873
1028
|
const failClauses = members
|
|
874
|
-
.filter((m) => m.
|
|
875
|
-
.map((m) => `NOT EXISTS { MATCH ${m.pattern}${m.preludeFragment} WHERE
|
|
1029
|
+
.filter((m) => m.user)
|
|
1030
|
+
.map((m) => `NOT EXISTS { MATCH ${m.pattern}${m.preludeFragment} WHERE ${stitchAllCounterexample(m.user, m.policy)} }`);
|
|
876
1031
|
if (failClauses.length === 0)
|
|
877
1032
|
return { cypher: '', params: {} };
|
|
878
1033
|
return {
|
|
@@ -905,11 +1060,105 @@ class WhereCompiler {
|
|
|
905
1060
|
return null;
|
|
906
1061
|
}
|
|
907
1062
|
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Compile `<key>: null` on a node (v2.3.0). The operator suffix is split
|
|
1065
|
+
* off FIRST: pre-2.3.0 the whole key was treated as a property name, so
|
|
1066
|
+
* the common "is not null" idiom `{ deletedAt_NOT: null }` compiled to
|
|
1067
|
+
* `n.deletedAt_NOT IS NULL` — always true, silently matching every row
|
|
1068
|
+
* (and silently restricting nothing when used in a restrictive policy).
|
|
1069
|
+
*
|
|
1070
|
+
* field: null → field IS NULL
|
|
1071
|
+
* field_NOT: null → field IS NOT NULL
|
|
1072
|
+
* rel: null → NOT EXISTS { MATCH (n)-[:REL]->(…) }
|
|
1073
|
+
* rel_NOT: null → EXISTS { MATCH (n)-[:REL]->(…) }
|
|
1074
|
+
* any other operator → OGMError (null has no meaning there)
|
|
1075
|
+
* undeclared field → OGMError regardless of `strictWhere` — a null
|
|
1076
|
+
* filter on an undeclared field matches every
|
|
1077
|
+
* row (fail OPEN), unlike a non-null one
|
|
1078
|
+
*
|
|
1079
|
+
* A field literally named `key` always wins over suffix parsing.
|
|
1080
|
+
*/
|
|
1081
|
+
compileNullCondition(key, nodeVar, nodeDef, counter, scope) {
|
|
1082
|
+
if (nodeDef.relationships.has(key))
|
|
1083
|
+
return this.compileRelationshipExistence(key, nodeVar, nodeDef, counter, false);
|
|
1084
|
+
if (!nodeDef.properties.has(key))
|
|
1085
|
+
for (const suffix of RELATIONSHIP_SUFFIXES) {
|
|
1086
|
+
if (!key.endsWith(suffix))
|
|
1087
|
+
continue;
|
|
1088
|
+
const field = key.slice(0, -suffix.length);
|
|
1089
|
+
if (!nodeDef.relationships.has(field))
|
|
1090
|
+
continue;
|
|
1091
|
+
if (suffix === '_NOT')
|
|
1092
|
+
return this.compileRelationshipExistence(field, nodeVar, nodeDef, counter, true);
|
|
1093
|
+
throw new errors_1.OGMError(`Relationship filter "${key}" cannot be null. Use "${field}: null" ` +
|
|
1094
|
+
`(no related node) or "${field}_NOT: null" (at least one related node).`);
|
|
1095
|
+
}
|
|
1096
|
+
return this.compileNullScalar(key, nodeVar, nodeDef, scope);
|
|
1097
|
+
}
|
|
1098
|
+
/**
|
|
1099
|
+
* Scalar half of the null semantics above, shared by node properties and
|
|
1100
|
+
* relationship-edge properties: `IS NULL` / `IS NOT NULL`, or an
|
|
1101
|
+
* `OGMError` for any other operator and for undeclared fields.
|
|
1102
|
+
*/
|
|
1103
|
+
compileNullScalar(key, varName, propsHolder, scope) {
|
|
1104
|
+
if (propsHolder?.properties.has(key)) {
|
|
1105
|
+
(0, validation_1.assertSafeIdentifier)(key, 'where clause');
|
|
1106
|
+
// `@cypher` scalars project through the scope (NULL check on the alias).
|
|
1107
|
+
return `${this.resolveFieldRef(key, varName, propsHolder, scope)} IS NULL`;
|
|
1108
|
+
}
|
|
1109
|
+
for (const suffix of OPERATOR_SUFFIXES) {
|
|
1110
|
+
if (!key.endsWith(suffix))
|
|
1111
|
+
continue;
|
|
1112
|
+
const field = key.slice(0, -suffix.length);
|
|
1113
|
+
if (!propsHolder?.properties.has(field))
|
|
1114
|
+
continue;
|
|
1115
|
+
if (suffix !== '_NOT')
|
|
1116
|
+
throw new errors_1.OGMError(`Operator "${suffix}" cannot be used with null ("${key}"). Use ` +
|
|
1117
|
+
`"${field}: null" (IS NULL) or "${field}_NOT: null" (IS NOT NULL) to test for null.`);
|
|
1118
|
+
(0, validation_1.assertSafeIdentifier)(field, 'where clause');
|
|
1119
|
+
return `${this.resolveFieldRef(field, varName, propsHolder, scope)} IS NOT NULL`;
|
|
1120
|
+
}
|
|
1121
|
+
throw new errors_1.OGMError(`Unknown field "${key}" in where clause with a null value. A null ` +
|
|
1122
|
+
`filter on a field the type does not declare would match every row, ` +
|
|
1123
|
+
`so it is rejected (regardless of strictWhere). Check for typos.`);
|
|
1124
|
+
}
|
|
1125
|
+
/**
|
|
1126
|
+
* `NOT EXISTS` (no related node) / `EXISTS` (at least one) for a
|
|
1127
|
+
* relationship null filter. Byte-identical to the pre-2.3.0 `rel: null`
|
|
1128
|
+
* emission for the `NOT EXISTS` case.
|
|
1129
|
+
*/
|
|
1130
|
+
compileRelationshipExistence(field, nodeVar, nodeDef, counter, exists) {
|
|
1131
|
+
const relDef = nodeDef.relationships.get(field);
|
|
1132
|
+
if (!(0, utils_1.resolveTargetDef)(relDef.target, this.schema))
|
|
1133
|
+
throw new errors_1.OGMError(`Cannot resolve the target type "${relDef.target}" of relationship "${field}".`);
|
|
1134
|
+
const relVar = `r${counter.count}`;
|
|
1135
|
+
counter.count++;
|
|
1136
|
+
const pattern = (0, utils_1.buildRelPattern)({
|
|
1137
|
+
sourceVar: nodeVar,
|
|
1138
|
+
relDef,
|
|
1139
|
+
targetVar: relVar,
|
|
1140
|
+
targetLabel: 'auto',
|
|
1141
|
+
// Pass schema so that union/interface targets resolve to a
|
|
1142
|
+
// labelless target (relationship-type-only filter). Without this,
|
|
1143
|
+
// the literal abstract type name is escaped as a label that no
|
|
1144
|
+
// concrete node carries → NOT EXISTS is true for every row → ALL
|
|
1145
|
+
// rows match.
|
|
1146
|
+
schema: this.schema,
|
|
1147
|
+
});
|
|
1148
|
+
return exists
|
|
1149
|
+
? `EXISTS { MATCH ${pattern} }`
|
|
1150
|
+
: `NOT EXISTS { MATCH ${pattern} }`;
|
|
1151
|
+
}
|
|
908
1152
|
compileEdgeConditions(edgeWhere, edgeVar, propsDef, edgeScope, counter) {
|
|
909
1153
|
const clauses = [];
|
|
910
1154
|
const params = {};
|
|
911
1155
|
for (const [key, value] of Object.entries(edgeWhere)) {
|
|
912
1156
|
(0, validation_1.assertSafeKey)(key, 'edge where input');
|
|
1157
|
+
// Same null semantics as node properties (v2.3.0).
|
|
1158
|
+
if (value === null) {
|
|
1159
|
+
clauses.push(this.compileNullScalar(key, edgeVar, propsDef, edgeScope));
|
|
1160
|
+
continue;
|
|
1161
|
+
}
|
|
913
1162
|
const result = this.compileScalarCondition(key, value, edgeVar, propsDef, edgeScope, counter);
|
|
914
1163
|
clauses.push(result.cypher);
|
|
915
1164
|
(0, validation_1.mergeParams)(params, result.params);
|
|
@@ -1009,6 +1258,26 @@ function stitchUserAndPolicy(userBody, policyClause) {
|
|
|
1009
1258
|
return userBody;
|
|
1010
1259
|
return `(${userBody}) AND ${policyClause}`;
|
|
1011
1260
|
}
|
|
1261
|
+
/**
|
|
1262
|
+
* `_ALL` body (v2.3.0): a related node the caller may SEE (`policy`) that
|
|
1263
|
+
* fails the caller's filter. The policy sits OUTSIDE the negation, so
|
|
1264
|
+
* hidden related nodes neither satisfy nor falsify the quantifier.
|
|
1265
|
+
*/
|
|
1266
|
+
function stitchAllCounterexample(userBody, policyClause) {
|
|
1267
|
+
return policyClause
|
|
1268
|
+
? `${policyClause} AND NOT (${userBody})`
|
|
1269
|
+
: `NOT (${userBody})`;
|
|
1270
|
+
}
|
|
1271
|
+
/**
|
|
1272
|
+
* The bundle a traversal's USER filter compiles under (v2.3.0): no clause
|
|
1273
|
+
* for the target itself (its policy is composed separately by the caller),
|
|
1274
|
+
* but `resolveForType` stays live so deeper traversals keep enforcing.
|
|
1275
|
+
*
|
|
1276
|
+
* @internal
|
|
1277
|
+
*/
|
|
1278
|
+
function traversalBundle(policyContext) {
|
|
1279
|
+
return { ...policyContext, operation: 'read', resolved: types_1.NO_ROOT_POLICY };
|
|
1280
|
+
}
|
|
1012
1281
|
/**
|
|
1013
1282
|
* Compose compiled policy fragments into the single boolean clause
|
|
1014
1283
|
* `(P1 OR P2 …) AND R1 AND R2 …` that `compile()` AND-stitches into the
|