graphddb 0.3.0 → 0.3.1
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/README.md +1 -0
- package/dist/chunk-PNIZS37E.js +916 -0
- package/dist/{chunk-QEOFIXTN.js → chunk-VST3WOK3.js} +235 -556
- package/dist/{chunk-6AIAHP3A.js → chunk-YIXXTGZ6.js} +898 -1
- package/dist/cli.js +2 -2
- package/dist/index.d.ts +477 -3
- package/dist/index.js +412 -17
- package/dist/testing/index.d.ts +1 -1
- package/dist/testing/index.js +2 -2
- package/dist/{types-xJRn5qkv.d.ts → types-DuJ08bgc.d.ts} +675 -64
- package/package.json +1 -1
- package/dist/chunk-QQNP43JL.js +0 -461
|
@@ -3418,6 +3418,882 @@ function buildLogicalItem(modelClass, ctx) {
|
|
|
3418
3418
|
return { Delete: buildDeleteInput(modelClass, ctx.input.key ?? {}, opts) };
|
|
3419
3419
|
}
|
|
3420
3420
|
|
|
3421
|
+
// src/define/entity-writes.ts
|
|
3422
|
+
var MAINTAIN_TRIGGER_RE = /^[^.\s$[\]*]+\.(?:created|updated|removed)$/;
|
|
3423
|
+
function maintainTrigger(value) {
|
|
3424
|
+
if (typeof value !== "string" || !MAINTAIN_TRIGGER_RE.test(value)) {
|
|
3425
|
+
throw new Error(
|
|
3426
|
+
`maintainTrigger: a maintenance trigger must be an \`Entity.event\` string where event is one of \`created\` / \`updated\` / \`removed\` (e.g. \`"Post.created"\`), but received ${JSON.stringify(value)}.`
|
|
3427
|
+
);
|
|
3428
|
+
}
|
|
3429
|
+
return value;
|
|
3430
|
+
}
|
|
3431
|
+
function isMaintainTrigger(value) {
|
|
3432
|
+
return typeof value === "string" && MAINTAIN_TRIGGER_RE.test(value);
|
|
3433
|
+
}
|
|
3434
|
+
function makeProjectionTransform(path, op, args) {
|
|
3435
|
+
if (op === "preview") {
|
|
3436
|
+
const [n] = args;
|
|
3437
|
+
if (typeof n !== "number" || !Number.isInteger(n) || n <= 0) {
|
|
3438
|
+
throw new Error(
|
|
3439
|
+
`transform: the \`preview\` op requires a positive integer length bound (e.g. \`preview('$.body', 200)\`), but received ${JSON.stringify(n)}.`
|
|
3440
|
+
);
|
|
3441
|
+
}
|
|
3442
|
+
return { kind: "transform", path, op, args: [n] };
|
|
3443
|
+
}
|
|
3444
|
+
if (op === "identity") {
|
|
3445
|
+
if (args.length > 0) {
|
|
3446
|
+
throw new Error(
|
|
3447
|
+
`transform: the \`identity\` op takes no arguments, but received ${JSON.stringify(args)}.`
|
|
3448
|
+
);
|
|
3449
|
+
}
|
|
3450
|
+
return { kind: "transform", path, op, args: [] };
|
|
3451
|
+
}
|
|
3452
|
+
throw new Error(
|
|
3453
|
+
`transform: unknown projection op ${JSON.stringify(op)} (expected \`identity\` or \`preview\`).`
|
|
3454
|
+
);
|
|
3455
|
+
}
|
|
3456
|
+
function preview(path, n) {
|
|
3457
|
+
return makeProjectionTransform(path, "preview", [n]);
|
|
3458
|
+
}
|
|
3459
|
+
function identity(path) {
|
|
3460
|
+
return makeProjectionTransform(path, "identity", []);
|
|
3461
|
+
}
|
|
3462
|
+
var LIFECYCLE_CONTRACT_MARKER = /* @__PURE__ */ Symbol(
|
|
3463
|
+
"graphddb:lifecycleContract"
|
|
3464
|
+
);
|
|
3465
|
+
function isLifecycleContract(value) {
|
|
3466
|
+
return typeof value === "object" && value !== null && value[LIFECYCLE_CONTRACT_MARKER] === true;
|
|
3467
|
+
}
|
|
3468
|
+
var ENTITY_WRITES_MARKER = /* @__PURE__ */ Symbol("graphddb:entityWrites");
|
|
3469
|
+
function isEntityWritesDefinition(value) {
|
|
3470
|
+
return typeof value === "object" && value !== null && value[ENTITY_WRITES_MARKER] === true;
|
|
3471
|
+
}
|
|
3472
|
+
function freezeEffects(effects) {
|
|
3473
|
+
const out = {};
|
|
3474
|
+
if (effects.requires !== void 0) out.requires = effects.requires;
|
|
3475
|
+
if (effects.unique !== void 0) out.unique = effects.unique;
|
|
3476
|
+
if (effects.edges !== void 0) out.edges = effects.edges;
|
|
3477
|
+
if (effects.derive !== void 0) out.derive = effects.derive;
|
|
3478
|
+
if (effects.emits !== void 0) out.emits = effects.emits;
|
|
3479
|
+
if (effects.idempotency !== void 0) out.idempotency = effects.idempotency;
|
|
3480
|
+
return out;
|
|
3481
|
+
}
|
|
3482
|
+
var recorder = {
|
|
3483
|
+
lifecycle(effects = {}) {
|
|
3484
|
+
return {
|
|
3485
|
+
[LIFECYCLE_CONTRACT_MARKER]: true,
|
|
3486
|
+
effects: freezeEffects(effects)
|
|
3487
|
+
};
|
|
3488
|
+
},
|
|
3489
|
+
exists(targetFactory, keys) {
|
|
3490
|
+
return { kind: "requires", targetFactory, keys };
|
|
3491
|
+
},
|
|
3492
|
+
unique(spec) {
|
|
3493
|
+
return { kind: "unique", name: spec.name, scope: spec.scope, fields: spec.fields };
|
|
3494
|
+
},
|
|
3495
|
+
putEdge(targetFactory, relationProperty) {
|
|
3496
|
+
return { kind: "putEdge", targetFactory, relationProperty };
|
|
3497
|
+
},
|
|
3498
|
+
deleteEdge(targetFactory, relationProperty) {
|
|
3499
|
+
return { kind: "deleteEdge", targetFactory, relationProperty };
|
|
3500
|
+
},
|
|
3501
|
+
increment(targetFactory, keys, attribute, amount) {
|
|
3502
|
+
return { kind: "derive", targetFactory, keys, attribute, amount };
|
|
3503
|
+
},
|
|
3504
|
+
transform(path, op, ...args) {
|
|
3505
|
+
return makeProjectionTransform(path, op, args);
|
|
3506
|
+
},
|
|
3507
|
+
event(name, payload) {
|
|
3508
|
+
return { kind: "event", name, payload };
|
|
3509
|
+
},
|
|
3510
|
+
idempotentBy(token) {
|
|
3511
|
+
return { kind: "idempotency", token };
|
|
3512
|
+
}
|
|
3513
|
+
};
|
|
3514
|
+
function entityWrites(builder) {
|
|
3515
|
+
const shape = builder(recorder);
|
|
3516
|
+
for (const phase of ["create", "update", "remove"]) {
|
|
3517
|
+
const contract = shape[phase];
|
|
3518
|
+
if (contract !== void 0 && !isLifecycleContract(contract)) {
|
|
3519
|
+
throw new Error(
|
|
3520
|
+
`entityWrites: the '${phase}' lifecycle must be built with \`w.lifecycle({...})\` (it carries the \xA72 effect arrays), but received ${JSON.stringify(contract)}.`
|
|
3521
|
+
);
|
|
3522
|
+
}
|
|
3523
|
+
}
|
|
3524
|
+
if (shape.create === void 0 && shape.update === void 0 && shape.remove === void 0) {
|
|
3525
|
+
throw new Error(
|
|
3526
|
+
"entityWrites(...) must declare at least one lifecycle (`create` / `update` / `remove`); an empty save contract declares nothing."
|
|
3527
|
+
);
|
|
3528
|
+
}
|
|
3529
|
+
return {
|
|
3530
|
+
[ENTITY_WRITES_MARKER]: true,
|
|
3531
|
+
...shape.create !== void 0 ? { create: shape.create } : {},
|
|
3532
|
+
...shape.update !== void 0 ? { update: shape.update } : {},
|
|
3533
|
+
...shape.remove !== void 0 ? { remove: shape.remove } : {}
|
|
3534
|
+
};
|
|
3535
|
+
}
|
|
3536
|
+
function getEntityWrites(modelClass) {
|
|
3537
|
+
for (const name of Object.getOwnPropertyNames(modelClass)) {
|
|
3538
|
+
let value;
|
|
3539
|
+
try {
|
|
3540
|
+
value = modelClass[name];
|
|
3541
|
+
} catch {
|
|
3542
|
+
continue;
|
|
3543
|
+
}
|
|
3544
|
+
if (isEntityWritesDefinition(value)) return value;
|
|
3545
|
+
}
|
|
3546
|
+
return void 0;
|
|
3547
|
+
}
|
|
3548
|
+
function lifecyclePhaseForIntent(intent) {
|
|
3549
|
+
return intent;
|
|
3550
|
+
}
|
|
3551
|
+
|
|
3552
|
+
// src/define/view.ts
|
|
3553
|
+
function whenMember(field, op, value) {
|
|
3554
|
+
const path = field.startsWith("$.input.") || field.startsWith("$.entity.") ? field : `$.entity.${field}`;
|
|
3555
|
+
const needsValue = op === "eq" || op === "ne";
|
|
3556
|
+
if (needsValue && value === void 0) {
|
|
3557
|
+
throw new Error(
|
|
3558
|
+
`whenMember('${field}', '${op}', \u2026): the '${op}' op requires a comparand value (e.g. \`whenMember('${field}', '${op}', 'active')\`).`
|
|
3559
|
+
);
|
|
3560
|
+
}
|
|
3561
|
+
return needsValue ? { path, op, value } : { path, op };
|
|
3562
|
+
}
|
|
3563
|
+
var VIEW_DEFINITION_MARKER = /* @__PURE__ */ Symbol("graphddb:viewDefinition");
|
|
3564
|
+
var ViewRegistry = class {
|
|
3565
|
+
static views = /* @__PURE__ */ new Map();
|
|
3566
|
+
static gen = 0;
|
|
3567
|
+
/** Register (or replace, by view class) a declared view. Bumps the generation. */
|
|
3568
|
+
static register(def) {
|
|
3569
|
+
this.views.set(def.viewClass, def);
|
|
3570
|
+
this.gen += 1;
|
|
3571
|
+
}
|
|
3572
|
+
/** Every registered view, in declaration order. */
|
|
3573
|
+
static getAll() {
|
|
3574
|
+
return [...this.views.values()];
|
|
3575
|
+
}
|
|
3576
|
+
/** The monotonic generation counter (changes whenever the set of views changes). */
|
|
3577
|
+
static get generation() {
|
|
3578
|
+
return this.gen;
|
|
3579
|
+
}
|
|
3580
|
+
/** Forget every registered view (test reset). Bumps the generation. */
|
|
3581
|
+
static clear() {
|
|
3582
|
+
this.views.clear();
|
|
3583
|
+
this.gen += 1;
|
|
3584
|
+
}
|
|
3585
|
+
};
|
|
3586
|
+
function isViewDefinition(value) {
|
|
3587
|
+
return typeof value === "object" && value !== null && value[VIEW_DEFINITION_MARKER] === true;
|
|
3588
|
+
}
|
|
3589
|
+
function resolveSourceClass(factory) {
|
|
3590
|
+
const resolved = factory();
|
|
3591
|
+
try {
|
|
3592
|
+
return resolveModelClass(resolved);
|
|
3593
|
+
} catch {
|
|
3594
|
+
return resolved;
|
|
3595
|
+
}
|
|
3596
|
+
}
|
|
3597
|
+
function isPathRooted(value) {
|
|
3598
|
+
return value.startsWith("$.input.") || value.startsWith("$.entity.");
|
|
3599
|
+
}
|
|
3600
|
+
function buildProjection(viewName, fields) {
|
|
3601
|
+
const entries = fields ? Object.entries(fields) : [];
|
|
3602
|
+
if (entries.length === 0) {
|
|
3603
|
+
throw new Error(
|
|
3604
|
+
`defineView('${viewName}'): a source projects no \`fields\` (and the view declares no default \`fields\`). A materialized view with no captured attributes projects nothing \u2014 declare \`fields\` (e.g. \`{ postId: 'postId', textPreview: preview('$.entity.body', 120) }\`).`
|
|
3605
|
+
);
|
|
3606
|
+
}
|
|
3607
|
+
const out = {};
|
|
3608
|
+
for (const [attr, value] of entries) {
|
|
3609
|
+
if (typeof value === "string") {
|
|
3610
|
+
const path = isPathRooted(value) ? value : `$.entity.${value}`;
|
|
3611
|
+
out[attr] = identity(path);
|
|
3612
|
+
} else {
|
|
3613
|
+
out[attr] = value;
|
|
3614
|
+
}
|
|
3615
|
+
}
|
|
3616
|
+
return out;
|
|
3617
|
+
}
|
|
3618
|
+
function validateTrigger(viewName, raw) {
|
|
3619
|
+
if (!isMaintainTrigger(raw)) {
|
|
3620
|
+
throw new Error(
|
|
3621
|
+
`defineView('${viewName}'): maintenance trigger ${JSON.stringify(raw)} is not a well-formed \`Entity.event\` trigger (event must be one of 'created' / 'updated' / 'removed'). Fix the source's \`maintainedOn\`.`
|
|
3622
|
+
);
|
|
3623
|
+
}
|
|
3624
|
+
return raw;
|
|
3625
|
+
}
|
|
3626
|
+
function defineView(view, input) {
|
|
3627
|
+
const viewClass = resolveSourceClass((() => view));
|
|
3628
|
+
const name = viewClass.name;
|
|
3629
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
3630
|
+
throw new Error("defineView(view, input): the view model must be a named class.");
|
|
3631
|
+
}
|
|
3632
|
+
if (input.pattern !== void 0 && input.pattern !== "materializedView" && input.pattern !== "sparseView") {
|
|
3633
|
+
throw new Error(
|
|
3634
|
+
`defineView('${name}'): \`pattern\` must be 'materializedView' or 'sparseView' (got ${JSON.stringify(input.pattern)}).`
|
|
3635
|
+
);
|
|
3636
|
+
}
|
|
3637
|
+
const pattern = input.pattern ?? "materializedView";
|
|
3638
|
+
const sources = Array.isArray(input.source) ? input.source : [input.source];
|
|
3639
|
+
if (sources.length === 0) {
|
|
3640
|
+
throw new Error(
|
|
3641
|
+
`defineView('${name}'): at least one \`source\` is required (a view with no source is maintained by nothing).`
|
|
3642
|
+
);
|
|
3643
|
+
}
|
|
3644
|
+
const slices = [];
|
|
3645
|
+
for (const src of sources) {
|
|
3646
|
+
const keys = src.keys ?? input.keys;
|
|
3647
|
+
if (keys === void 0 || Object.keys(keys).length === 0) {
|
|
3648
|
+
throw new Error(
|
|
3649
|
+
`defineView('${name}'): a source declares no \`keys\` and the view has no default \`keys\`. The view-row key binding (view-row key field \u2192 \`$.entity.<sourceField>\`) is required to resolve which view row a source event maintains.`
|
|
3650
|
+
);
|
|
3651
|
+
}
|
|
3652
|
+
if (!src.maintainedOn || src.maintainedOn.length === 0) {
|
|
3653
|
+
throw new Error(
|
|
3654
|
+
`defineView('${name}'): a source declares no \`maintainedOn\` triggers. Each source must name the lifecycle events that maintain the view (e.g. \`['Post.created', 'Post.removed']\`).`
|
|
3655
|
+
);
|
|
3656
|
+
}
|
|
3657
|
+
const maintainedOn = src.maintainedOn.map((t) => validateTrigger(name, t));
|
|
3658
|
+
const project = buildProjection(name, src.fields ?? input.fields);
|
|
3659
|
+
const collection = src.collection ? {
|
|
3660
|
+
field: src.collection.field,
|
|
3661
|
+
...src.collection.maxItems !== void 0 ? { maxItems: src.collection.maxItems } : {},
|
|
3662
|
+
...src.collection.orderBy !== void 0 ? {
|
|
3663
|
+
orderBy: src.collection.orderBy,
|
|
3664
|
+
orderDir: src.collection.order ?? "DESC"
|
|
3665
|
+
} : {}
|
|
3666
|
+
} : void 0;
|
|
3667
|
+
const predicate = src.when;
|
|
3668
|
+
if (predicate && collection) {
|
|
3669
|
+
throw new Error(
|
|
3670
|
+
`defineView('${name}'): a source declares both \`when\` (a sparse-view membership predicate) and \`collection\`. They are mutually exclusive \u2014 a membership row appears/disappears as a whole; a collection slice holds a maintained list.`
|
|
3671
|
+
);
|
|
3672
|
+
}
|
|
3673
|
+
if (pattern === "sparseView" && !predicate) {
|
|
3674
|
+
throw new Error(
|
|
3675
|
+
`defineView('${name}'): \`pattern: 'sparseView'\` requires every source to declare a \`when\` membership predicate (e.g. \`whenMember('status', 'eq', 'active')\`) \u2014 the predicate is what decides PUT (holds) vs DELETE (flips false) of the view row.`
|
|
3676
|
+
);
|
|
3677
|
+
}
|
|
3678
|
+
if (predicate && pattern !== "sparseView") {
|
|
3679
|
+
throw new Error(
|
|
3680
|
+
`defineView('${name}'): a source declares a \`when\` membership predicate but the view \`pattern\` is '${pattern}'. Declare \`pattern: 'sparseView'\` to maintain a predicate-gated put/delete row.`
|
|
3681
|
+
);
|
|
3682
|
+
}
|
|
3683
|
+
slices.push({
|
|
3684
|
+
sourceClass: resolveSourceClass(src.entity),
|
|
3685
|
+
maintainedOn,
|
|
3686
|
+
keys,
|
|
3687
|
+
project,
|
|
3688
|
+
...collection ? { collection } : {},
|
|
3689
|
+
...predicate ? { predicate } : {}
|
|
3690
|
+
});
|
|
3691
|
+
}
|
|
3692
|
+
const def = {
|
|
3693
|
+
[VIEW_DEFINITION_MARKER]: true,
|
|
3694
|
+
name,
|
|
3695
|
+
viewClass,
|
|
3696
|
+
pattern,
|
|
3697
|
+
slices,
|
|
3698
|
+
...input.write?.updateMode !== void 0 ? { updateMode: input.write.updateMode } : {},
|
|
3699
|
+
...input.write?.consistency !== void 0 ? { consistency: input.write.consistency } : {}
|
|
3700
|
+
};
|
|
3701
|
+
ViewRegistry.register(def);
|
|
3702
|
+
return def;
|
|
3703
|
+
}
|
|
3704
|
+
function defineViews(definitions) {
|
|
3705
|
+
for (const [name, def] of Object.entries(definitions)) {
|
|
3706
|
+
if (!isViewDefinition(def)) {
|
|
3707
|
+
throw new Error(
|
|
3708
|
+
`defineViews: '${name}' is not a view definition. Use defineView(name, { pattern: 'materializedView', source, keys, fields }) to build entries.`
|
|
3709
|
+
);
|
|
3710
|
+
}
|
|
3711
|
+
}
|
|
3712
|
+
return definitions;
|
|
3713
|
+
}
|
|
3714
|
+
function defineVersioned(latest, history, input) {
|
|
3715
|
+
if (input.pattern !== void 0 && input.pattern !== "versioned") {
|
|
3716
|
+
throw new Error(
|
|
3717
|
+
`defineVersioned: \`pattern\` must be 'versioned' (got ${JSON.stringify(input.pattern)}).`
|
|
3718
|
+
);
|
|
3719
|
+
}
|
|
3720
|
+
const latestClass = resolveSourceClass((() => latest));
|
|
3721
|
+
const historyClass = resolveSourceClass((() => history));
|
|
3722
|
+
if (latestClass === historyClass) {
|
|
3723
|
+
throw new Error(
|
|
3724
|
+
`defineVersioned: the latest-pointer and history models must be DISTINCT (both are '${latestClass.name}'). The latest row is overwritten per revision and the history row is appended per version, so they live on different rows.`
|
|
3725
|
+
);
|
|
3726
|
+
}
|
|
3727
|
+
const latestKeyFields = new Set(Object.keys(input.latestKeys));
|
|
3728
|
+
const historyKeyFields = Object.keys(input.historyKeys);
|
|
3729
|
+
const discriminators = historyKeyFields.filter((f) => !latestKeyFields.has(f));
|
|
3730
|
+
if (discriminators.length === 0) {
|
|
3731
|
+
throw new Error(
|
|
3732
|
+
`defineVersioned: \`historyKeys\` [${historyKeyFields.map((f) => `'${f}'`).join(", ")}] carries no version discriminator beyond \`latestKeys\` [${[...latestKeyFields].map((f) => `'${f}'`).join(", ")}]. The history row must be keyed by a per-version field (e.g. add \`version: '$.entity.version'\`) so each revision is a NEW row; otherwise every revision overwrites one row (a latest pointer, not an append-only history).`
|
|
3733
|
+
);
|
|
3734
|
+
}
|
|
3735
|
+
const latestView = defineView(latest, {
|
|
3736
|
+
pattern: "materializedView",
|
|
3737
|
+
source: { entity: input.source, maintainedOn: input.maintainedOn, keys: input.latestKeys },
|
|
3738
|
+
fields: input.fields,
|
|
3739
|
+
...input.write ? { write: input.write } : {}
|
|
3740
|
+
});
|
|
3741
|
+
const historyView = defineView(history, {
|
|
3742
|
+
pattern: "materializedView",
|
|
3743
|
+
source: { entity: input.source, maintainedOn: input.maintainedOn, keys: input.historyKeys },
|
|
3744
|
+
fields: input.fields,
|
|
3745
|
+
...input.write ? { write: input.write } : {}
|
|
3746
|
+
});
|
|
3747
|
+
return { latest: latestView, history: historyView };
|
|
3748
|
+
}
|
|
3749
|
+
var PROJECTION_DEFINITION_MARKER = /* @__PURE__ */ Symbol(
|
|
3750
|
+
"graphddb:projectionDefinition"
|
|
3751
|
+
);
|
|
3752
|
+
function isProjectionDefinition(value) {
|
|
3753
|
+
return typeof value === "object" && value !== null && value[PROJECTION_DEFINITION_MARKER] === true;
|
|
3754
|
+
}
|
|
3755
|
+
function defineProjection(name, input) {
|
|
3756
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
3757
|
+
throw new Error("defineProjection(name, input): `name` must be a non-empty string.");
|
|
3758
|
+
}
|
|
3759
|
+
if (input.pattern !== void 0 && input.pattern !== "externalProjection") {
|
|
3760
|
+
throw new Error(
|
|
3761
|
+
`defineProjection('${name}'): \`pattern\` must be 'externalProjection' (got ${JSON.stringify(input.pattern)}).`
|
|
3762
|
+
);
|
|
3763
|
+
}
|
|
3764
|
+
const via = input.via ?? "stream";
|
|
3765
|
+
if (via !== "stream") {
|
|
3766
|
+
throw new Error(
|
|
3767
|
+
`defineProjection('${name}'): \`via\` must be 'stream' (the only supported transport this phase; got ${JSON.stringify(via)}). An external projection is maintained asynchronously off the CDC stream.`
|
|
3768
|
+
);
|
|
3769
|
+
}
|
|
3770
|
+
if (typeof input.idempotencyKey !== "string" || input.idempotencyKey.length === 0) {
|
|
3771
|
+
throw new Error(
|
|
3772
|
+
`defineProjection('${name}'): \`idempotencyKey\` must be a non-empty source attribute name (e.g. 'resultId'). It folds at-least-once delivery to one sink upsert per genuine source event.`
|
|
3773
|
+
);
|
|
3774
|
+
}
|
|
3775
|
+
const on = input.on ?? ["created", "updated", "removed"];
|
|
3776
|
+
const project = input.fields ? buildProjection(name, input.fields) : {};
|
|
3777
|
+
return {
|
|
3778
|
+
[PROJECTION_DEFINITION_MARKER]: true,
|
|
3779
|
+
name,
|
|
3780
|
+
pattern: "externalProjection",
|
|
3781
|
+
sourceClass: resolveSourceClass(input.source),
|
|
3782
|
+
via,
|
|
3783
|
+
idempotencyKey: input.idempotencyKey,
|
|
3784
|
+
on: [...on],
|
|
3785
|
+
project
|
|
3786
|
+
};
|
|
3787
|
+
}
|
|
3788
|
+
function defineProjections(definitions) {
|
|
3789
|
+
for (const [name, def] of Object.entries(definitions)) {
|
|
3790
|
+
if (!isProjectionDefinition(def)) {
|
|
3791
|
+
throw new Error(
|
|
3792
|
+
`defineProjections: '${name}' is not a projection definition. Use defineProjection(name, { pattern: 'externalProjection', source, via, idempotencyKey }) to build entries.`
|
|
3793
|
+
);
|
|
3794
|
+
}
|
|
3795
|
+
}
|
|
3796
|
+
return definitions;
|
|
3797
|
+
}
|
|
3798
|
+
|
|
3799
|
+
// src/relation/maintenance-graph.ts
|
|
3800
|
+
var COUNT_DELTA_FOR_EVENT = {
|
|
3801
|
+
created: 1,
|
|
3802
|
+
removed: -1,
|
|
3803
|
+
updated: 0
|
|
3804
|
+
};
|
|
3805
|
+
var MAINTAIN_EVENTS = ["created", "updated", "removed"];
|
|
3806
|
+
function isPathRooted2(value) {
|
|
3807
|
+
return value.startsWith("$.input.") || value.startsWith("$.entity.");
|
|
3808
|
+
}
|
|
3809
|
+
function sourceAttributeOf(path) {
|
|
3810
|
+
const stripped = path.replace(/^\$\.(input|entity)\./, "");
|
|
3811
|
+
return stripped.split(".")[0] ?? stripped;
|
|
3812
|
+
}
|
|
3813
|
+
function normalizeProjectionEntry(value) {
|
|
3814
|
+
if (typeof value === "string") {
|
|
3815
|
+
const path = isPathRooted2(value) ? value : `$.entity.${value}`;
|
|
3816
|
+
return identity(path);
|
|
3817
|
+
}
|
|
3818
|
+
return value;
|
|
3819
|
+
}
|
|
3820
|
+
function buildProjectionMap(projection, ownerEntity, relationProperty) {
|
|
3821
|
+
const entries = projection ? Object.entries(projection) : [];
|
|
3822
|
+
if (entries.length === 0) {
|
|
3823
|
+
throw new Error(
|
|
3824
|
+
`Relation '${relationProperty}' on '${ownerEntity}' declares maintenance (\`write.maintainedOn\`) but no \`projection\`: a maintained snapshot / collection with no captured attributes projects nothing. Declare a \`projection\` map (e.g. \`{ postId: 'postId', textPreview: preview('body', 120) }\`).`
|
|
3825
|
+
);
|
|
3826
|
+
}
|
|
3827
|
+
const out = {};
|
|
3828
|
+
for (const [attr, value] of entries) {
|
|
3829
|
+
out[attr] = normalizeProjectionEntry(value);
|
|
3830
|
+
}
|
|
3831
|
+
return out;
|
|
3832
|
+
}
|
|
3833
|
+
function buildDestinationKeys(keyBinding) {
|
|
3834
|
+
const keys = {};
|
|
3835
|
+
const sourceFields = [];
|
|
3836
|
+
for (const [sourceField, ownerField] of Object.entries(keyBinding)) {
|
|
3837
|
+
keys[ownerField] = `$.entity.${sourceField}`;
|
|
3838
|
+
sourceFields.push(sourceField);
|
|
3839
|
+
}
|
|
3840
|
+
return { keys, sourceFields };
|
|
3841
|
+
}
|
|
3842
|
+
function sourcePayloadAttributes(meta) {
|
|
3843
|
+
const attrs = /* @__PURE__ */ new Set();
|
|
3844
|
+
for (const f of meta.fields) attrs.add(f.propertyName);
|
|
3845
|
+
if (meta.primaryKey) {
|
|
3846
|
+
for (const f of segmentFieldNames(meta.primaryKey.segmented.pkSegments)) attrs.add(f);
|
|
3847
|
+
for (const f of segmentFieldNames(meta.primaryKey.segmented.skSegments)) attrs.add(f);
|
|
3848
|
+
}
|
|
3849
|
+
for (const gsi2 of meta.gsiDefinitions) {
|
|
3850
|
+
for (const f of segmentFieldNames(gsi2.segmented.pkSegments)) attrs.add(f);
|
|
3851
|
+
for (const f of segmentFieldNames(gsi2.segmented.skSegments)) attrs.add(f);
|
|
3852
|
+
}
|
|
3853
|
+
return attrs;
|
|
3854
|
+
}
|
|
3855
|
+
function assertMaintenanceRoundTrips(args) {
|
|
3856
|
+
const {
|
|
3857
|
+
ownerEntity,
|
|
3858
|
+
relationProperty,
|
|
3859
|
+
ownerMeta,
|
|
3860
|
+
sourceEntity,
|
|
3861
|
+
sourceMeta,
|
|
3862
|
+
destinationKeyFields,
|
|
3863
|
+
keyBindingSourceFields,
|
|
3864
|
+
projection
|
|
3865
|
+
} = args;
|
|
3866
|
+
const sourceAttrs = sourcePayloadAttributes(sourceMeta);
|
|
3867
|
+
try {
|
|
3868
|
+
resolveKey([...destinationKeyFields], ownerMeta);
|
|
3869
|
+
} catch (cause) {
|
|
3870
|
+
throw new Error(
|
|
3871
|
+
`Maintenance declaration for relation '${relationProperty}' on '${ownerEntity}' binds destination-key fields [${destinationKeyFields.map((f) => `'${f}'`).join(", ")}], which cover no access-pattern partition of the maintained entity '${ownerEntity}'. The maintained snapshot / collection row could not be resolved, so the snapshot key would not point at a real target row \u2014 bind the partition-key field(s) of the index the maintained row lives on. (${cause.message})`
|
|
3872
|
+
);
|
|
3873
|
+
}
|
|
3874
|
+
for (const field of keyBindingSourceFields) {
|
|
3875
|
+
if (!sourceAttrs.has(field)) {
|
|
3876
|
+
throw new Error(
|
|
3877
|
+
`Maintenance key binding for relation '${relationProperty}' on '${ownerEntity}' reads source field '${field}' to resolve the destination row, but the source entity '${sourceEntity}' carries no attribute '${field}' on its payload (available: ${[...sourceAttrs].sort().map((f) => `'${f}'`).join(", ")}). The destination key could not be built from the source payload \u2014 reject.`
|
|
3878
|
+
);
|
|
3879
|
+
}
|
|
3880
|
+
}
|
|
3881
|
+
for (const [attr, transform] of Object.entries(projection)) {
|
|
3882
|
+
if (!isPathRooted2(transform.path)) {
|
|
3883
|
+
throw new Error(
|
|
3884
|
+
`Maintenance projection for relation '${relationProperty}' on '${ownerEntity}' captures attribute '${attr}' from '${transform.path}', which is not a payload-rooted source path (\`$.input.*\` / \`$.entity.*\`). The maintenance graph projects only from the source payload (payload \u540C\u68B1) \u2014 there is no fetch / re-projection.`
|
|
3885
|
+
);
|
|
3886
|
+
}
|
|
3887
|
+
const srcAttr = sourceAttributeOf(transform.path);
|
|
3888
|
+
if (!sourceAttrs.has(srcAttr)) {
|
|
3889
|
+
throw new Error(
|
|
3890
|
+
`Maintenance projection for relation '${relationProperty}' on '${ownerEntity}' captures '${attr}' from '${transform.path}', but the source entity '${sourceEntity}' carries no attribute '${srcAttr}' on its payload (available: ${[...sourceAttrs].sort().map((f) => `'${f}'`).join(", ")}). Projection is payload \u540C\u68B1 only (no fetch / re-projection): an attribute the source row does not carry cannot be projected \u2014 reject.`
|
|
3891
|
+
);
|
|
3892
|
+
}
|
|
3893
|
+
}
|
|
3894
|
+
}
|
|
3895
|
+
function assertCounterRoundTrips(args) {
|
|
3896
|
+
const {
|
|
3897
|
+
ownerEntity,
|
|
3898
|
+
aggregateField,
|
|
3899
|
+
ownerMeta,
|
|
3900
|
+
sourceEntity,
|
|
3901
|
+
sourceMeta,
|
|
3902
|
+
destinationKeyFields,
|
|
3903
|
+
keyBindingSourceFields,
|
|
3904
|
+
valueSourceFields
|
|
3905
|
+
} = args;
|
|
3906
|
+
const sourceAttrs = sourcePayloadAttributes(sourceMeta);
|
|
3907
|
+
try {
|
|
3908
|
+
resolveKey([...destinationKeyFields], ownerMeta);
|
|
3909
|
+
} catch (cause) {
|
|
3910
|
+
throw new Error(
|
|
3911
|
+
`Counter aggregate '${aggregateField}' on '${ownerEntity}' binds destination-key fields [${destinationKeyFields.map((f) => `'${f}'`).join(", ")}], which cover no access-pattern partition of the maintained entity '${ownerEntity}'. The counter row could not be resolved \u2014 bind the partition-key field(s) of the index the counter row lives on. (${cause.message})`
|
|
3912
|
+
);
|
|
3913
|
+
}
|
|
3914
|
+
for (const field of keyBindingSourceFields) {
|
|
3915
|
+
if (!sourceAttrs.has(field)) {
|
|
3916
|
+
throw new Error(
|
|
3917
|
+
`Counter aggregate '${aggregateField}' on '${ownerEntity}' reads source field '${field}' to resolve the counter row, but the source entity '${sourceEntity}' carries no attribute '${field}' on its payload (available: ${[...sourceAttrs].sort().map((f) => `'${f}'`).join(", ")}). The destination key could not be built from the source payload \u2014 reject.`
|
|
3918
|
+
);
|
|
3919
|
+
}
|
|
3920
|
+
}
|
|
3921
|
+
for (const field of valueSourceFields) {
|
|
3922
|
+
if (!sourceAttrs.has(field)) {
|
|
3923
|
+
throw new Error(
|
|
3924
|
+
`Counter aggregate '${aggregateField}' on '${ownerEntity}' tracks \`max('${field}')\`, but the source entity '${sourceEntity}' carries no attribute '${field}' on its payload (available: ${[...sourceAttrs].sort().map((f) => `'${f}'`).join(", ")}). A counter aggregates only the source payload (payload \u540C\u68B1) \u2014 reject.`
|
|
3925
|
+
);
|
|
3926
|
+
}
|
|
3927
|
+
}
|
|
3928
|
+
}
|
|
3929
|
+
function logicalNameOf(meta) {
|
|
3930
|
+
return meta.prefix.endsWith("#") ? meta.prefix.slice(0, -1) : meta.prefix;
|
|
3931
|
+
}
|
|
3932
|
+
function resolveEntityByName(name, registry) {
|
|
3933
|
+
for (const [klass, meta] of registry) {
|
|
3934
|
+
if (logicalNameOf(meta) === name) return klass;
|
|
3935
|
+
}
|
|
3936
|
+
return void 0;
|
|
3937
|
+
}
|
|
3938
|
+
function parseTrigger(raw) {
|
|
3939
|
+
const idx = raw.lastIndexOf(".");
|
|
3940
|
+
return { entity: raw.slice(0, idx), event: raw.slice(idx + 1) };
|
|
3941
|
+
}
|
|
3942
|
+
function buildEffect(relation, ownerFactory, trigger, keys, project, sourceMeta, ownerEntity) {
|
|
3943
|
+
const write = relation.options?.write;
|
|
3944
|
+
const updateMode = write?.updateMode;
|
|
3945
|
+
const consistency = write?.consistency;
|
|
3946
|
+
const base = {
|
|
3947
|
+
trigger,
|
|
3948
|
+
targetFactory: ownerFactory,
|
|
3949
|
+
keys,
|
|
3950
|
+
project,
|
|
3951
|
+
...updateMode !== void 0 ? { updateMode } : {},
|
|
3952
|
+
...consistency !== void 0 ? { consistency } : {}
|
|
3953
|
+
};
|
|
3954
|
+
if (relation.type === "hasMany") {
|
|
3955
|
+
const read = relation.options?.read;
|
|
3956
|
+
const order = resolveCollectionOrder(relation, sourceMeta, ownerEntity, project);
|
|
3957
|
+
const collection = {
|
|
3958
|
+
field: relation.propertyName,
|
|
3959
|
+
...read?.maxItems !== void 0 ? { maxItems: read.maxItems } : {},
|
|
3960
|
+
...order !== void 0 ? { orderBy: order.orderBy, orderDir: order.orderDir } : {}
|
|
3961
|
+
};
|
|
3962
|
+
return { kind: "collection", ...base, collection };
|
|
3963
|
+
}
|
|
3964
|
+
return { kind: "snapshot", ...base };
|
|
3965
|
+
}
|
|
3966
|
+
function viewSliceProperty(viewName, sliceIndex, slice) {
|
|
3967
|
+
return slice.collection ? `${viewName}#${slice.collection.field}` : `${viewName}#${sliceIndex}`;
|
|
3968
|
+
}
|
|
3969
|
+
function buildViewEffect(view, slice, trigger) {
|
|
3970
|
+
const targetFactory = () => view.viewClass;
|
|
3971
|
+
const base = {
|
|
3972
|
+
trigger,
|
|
3973
|
+
targetFactory,
|
|
3974
|
+
keys: slice.keys,
|
|
3975
|
+
project: slice.project,
|
|
3976
|
+
...view.updateMode !== void 0 ? { updateMode: view.updateMode } : {},
|
|
3977
|
+
...view.consistency !== void 0 ? { consistency: view.consistency } : {}
|
|
3978
|
+
};
|
|
3979
|
+
if (slice.predicate) {
|
|
3980
|
+
return { kind: "membership", ...base, predicate: slice.predicate };
|
|
3981
|
+
}
|
|
3982
|
+
if (slice.collection) {
|
|
3983
|
+
const collection = {
|
|
3984
|
+
field: slice.collection.field,
|
|
3985
|
+
...slice.collection.maxItems !== void 0 ? { maxItems: slice.collection.maxItems } : {},
|
|
3986
|
+
...slice.collection.orderBy !== void 0 ? {
|
|
3987
|
+
orderBy: slice.collection.orderBy,
|
|
3988
|
+
orderDir: slice.collection.orderDir ?? "DESC"
|
|
3989
|
+
} : {}
|
|
3990
|
+
};
|
|
3991
|
+
return { kind: "collection", ...base, collection };
|
|
3992
|
+
}
|
|
3993
|
+
return { kind: "snapshot", ...base };
|
|
3994
|
+
}
|
|
3995
|
+
function resolveCollectionOrder(relation, sourceMeta, ownerEntity, project) {
|
|
3996
|
+
const order = relation.options?.read?.order;
|
|
3997
|
+
if (order === void 0) return void 0;
|
|
3998
|
+
const skFields = sourceMeta.primaryKey ? segmentFieldNames(sourceMeta.primaryKey.segmented.skSegments) : [];
|
|
3999
|
+
const orderField = skFields[skFields.length - 1];
|
|
4000
|
+
if (orderField === void 0) {
|
|
4001
|
+
throw new Error(
|
|
4002
|
+
`Relation '${relation.propertyName}' on '${ownerEntity}' declares \`read.order: '${order}'\` for its maintained collection, but its source entity '${logicalNameOf(sourceMeta)}' has no sort key to order by. A maintained collection orders by the source's sort-key field (the field the live read sorts on); declare a sort key on '${logicalNameOf(sourceMeta)}' or drop \`read.order\`.`
|
|
4003
|
+
);
|
|
4004
|
+
}
|
|
4005
|
+
if (!Object.prototype.hasOwnProperty.call(project, orderField)) {
|
|
4006
|
+
throw new Error(
|
|
4007
|
+
`Relation '${relation.propertyName}' on '${ownerEntity}' declares \`read.order: '${order}'\` for its maintained collection, which orders by the source entity '${logicalNameOf(sourceMeta)}'s sort-key field '${orderField}', but that field is not in the relation's \`projection\` (projected: ${Object.keys(project).map((a) => `'${a}'`).join(", ")}). The maintained collection sorts its projected items, so the order field must be projected \u2014 add '${orderField}' to the projection (e.g. \`{ ${orderField}: '${orderField}', \u2026 }\`) or drop \`read.order\`.`
|
|
4008
|
+
);
|
|
4009
|
+
}
|
|
4010
|
+
return { orderBy: `$.entity.${orderField}`, orderDir: order };
|
|
4011
|
+
}
|
|
4012
|
+
function buildCounterEffect(aggregate, ownerFactory, trigger, event, keys) {
|
|
4013
|
+
const write = aggregate.options.write;
|
|
4014
|
+
const updateMode = write?.updateMode;
|
|
4015
|
+
const consistency = write?.consistency;
|
|
4016
|
+
const value = aggregate.options.value.op === "count" ? { op: "count" } : { op: "max", field: aggregate.options.value.field };
|
|
4017
|
+
return {
|
|
4018
|
+
kind: "counter",
|
|
4019
|
+
trigger,
|
|
4020
|
+
targetFactory: ownerFactory,
|
|
4021
|
+
keys,
|
|
4022
|
+
attribute: aggregate.propertyName,
|
|
4023
|
+
value,
|
|
4024
|
+
...value.op === "count" ? { delta: COUNT_DELTA_FOR_EVENT[event] } : {},
|
|
4025
|
+
...updateMode !== void 0 ? { updateMode } : {},
|
|
4026
|
+
...consistency !== void 0 ? { consistency } : {}
|
|
4027
|
+
};
|
|
4028
|
+
}
|
|
4029
|
+
function destinationRowKeyOf(ownerEntity, keys) {
|
|
4030
|
+
const parts = Object.entries(keys).map(([field, path]) => `${field}=${path}`).sort();
|
|
4031
|
+
return `${ownerEntity}#${parts.join("&")}`;
|
|
4032
|
+
}
|
|
4033
|
+
function assertNoCycle(items) {
|
|
4034
|
+
const adjacency = /* @__PURE__ */ new Map();
|
|
4035
|
+
for (const item of items) {
|
|
4036
|
+
if (!adjacency.has(item.sourceEntity)) adjacency.set(item.sourceEntity, /* @__PURE__ */ new Set());
|
|
4037
|
+
adjacency.get(item.sourceEntity).add(item.ownerEntity);
|
|
4038
|
+
}
|
|
4039
|
+
const WHITE = 0;
|
|
4040
|
+
const GRAY = 1;
|
|
4041
|
+
const BLACK = 2;
|
|
4042
|
+
const color = /* @__PURE__ */ new Map();
|
|
4043
|
+
for (const node of adjacency.keys()) color.set(node, WHITE);
|
|
4044
|
+
for (const start of adjacency.keys()) {
|
|
4045
|
+
if (color.get(start) !== WHITE) continue;
|
|
4046
|
+
const stack = [];
|
|
4047
|
+
const path = [];
|
|
4048
|
+
color.set(start, GRAY);
|
|
4049
|
+
path.push(start);
|
|
4050
|
+
stack.push({ node: start, iter: (adjacency.get(start) ?? /* @__PURE__ */ new Set()).values() });
|
|
4051
|
+
while (stack.length > 0) {
|
|
4052
|
+
const top = stack[stack.length - 1];
|
|
4053
|
+
const next = top.iter.next();
|
|
4054
|
+
if (next.done) {
|
|
4055
|
+
color.set(top.node, BLACK);
|
|
4056
|
+
stack.pop();
|
|
4057
|
+
path.pop();
|
|
4058
|
+
continue;
|
|
4059
|
+
}
|
|
4060
|
+
const child = next.value;
|
|
4061
|
+
const childColor = color.get(child) ?? WHITE;
|
|
4062
|
+
if (childColor === GRAY) {
|
|
4063
|
+
const from = path.indexOf(child);
|
|
4064
|
+
const cycle = [...path.slice(from), child].join(" \u2192 ");
|
|
4065
|
+
throw new Error(
|
|
4066
|
+
`Maintenance dependency cycle detected: ${cycle}. A maintenance trigger chain that loops back on itself is an unbounded cascade \u2014 break the cycle (an entity's maintained shape must not, transitively, be triggered by its own maintenance).`
|
|
4067
|
+
);
|
|
4068
|
+
}
|
|
4069
|
+
if (childColor === WHITE) {
|
|
4070
|
+
color.set(child, GRAY);
|
|
4071
|
+
path.push(child);
|
|
4072
|
+
stack.push({ node: child, iter: (adjacency.get(child) ?? /* @__PURE__ */ new Set()).values() });
|
|
4073
|
+
}
|
|
4074
|
+
}
|
|
4075
|
+
}
|
|
4076
|
+
}
|
|
4077
|
+
function buildMaintenanceGraph(registry, views) {
|
|
4078
|
+
const useGlobal = registry === void 0;
|
|
4079
|
+
const resolvedRegistry = registry ?? MetadataRegistry.getAll();
|
|
4080
|
+
const resolvedViews = views ?? (useGlobal ? ViewRegistry.getAll() : []);
|
|
4081
|
+
return buildMaintenanceGraphImpl(resolvedRegistry, resolvedViews);
|
|
4082
|
+
}
|
|
4083
|
+
function buildMaintenanceGraphImpl(registry, views) {
|
|
4084
|
+
const items = [];
|
|
4085
|
+
const owners = [...registry.entries()].map(([klass, meta]) => ({ klass, meta })).sort((a, b) => a.klass.name < b.klass.name ? -1 : a.klass.name > b.klass.name ? 1 : 0);
|
|
4086
|
+
for (const { klass: ownerClass, meta: ownerMeta } of owners) {
|
|
4087
|
+
const ownerEntity = logicalNameOf(ownerMeta);
|
|
4088
|
+
const relations = [...ownerMeta.relations].sort(
|
|
4089
|
+
(a, b) => a.propertyName < b.propertyName ? -1 : a.propertyName > b.propertyName ? 1 : 0
|
|
4090
|
+
);
|
|
4091
|
+
for (const relation of relations) {
|
|
4092
|
+
const maintainedOn = relation.options?.write?.maintainedOn;
|
|
4093
|
+
if (!maintainedOn || maintainedOn.length === 0) continue;
|
|
4094
|
+
const sourceClass = relation.targetFactory();
|
|
4095
|
+
const sourceMeta = MetadataRegistry.get(sourceClass);
|
|
4096
|
+
const relationTargetEntity = logicalNameOf(sourceMeta);
|
|
4097
|
+
const project = buildProjectionMap(
|
|
4098
|
+
relation.options?.projection,
|
|
4099
|
+
ownerEntity,
|
|
4100
|
+
relation.propertyName
|
|
4101
|
+
);
|
|
4102
|
+
const { keys, sourceFields } = buildDestinationKeys(relation.keyBinding);
|
|
4103
|
+
const destinationKeyFields = Object.keys(keys);
|
|
4104
|
+
for (const raw of maintainedOn) {
|
|
4105
|
+
if (!isMaintainTrigger(raw)) {
|
|
4106
|
+
const { event } = parseTrigger(raw);
|
|
4107
|
+
throw new Error(
|
|
4108
|
+
`Maintenance trigger ${JSON.stringify(raw)} on relation '${relation.propertyName}' of '${ownerEntity}' is not a well-formed \`Entity.event\` trigger (event must be one of ${MAINTAIN_EVENTS.map((e) => `'${e}'`).join(" / ")}; got '${event}'). Fix the \`write.maintainedOn\` entry.`
|
|
4109
|
+
);
|
|
4110
|
+
}
|
|
4111
|
+
const trigger = raw;
|
|
4112
|
+
const { entity: triggerEntity } = parseTrigger(trigger);
|
|
4113
|
+
if (!resolveEntityByName(triggerEntity, registry)) {
|
|
4114
|
+
throw new Error(
|
|
4115
|
+
`Maintenance trigger '${trigger}' on relation '${relation.propertyName}' of '${ownerEntity}' names source entity '${triggerEntity}', which is not a registered model. Declare a model named '${triggerEntity}' or fix the trigger's entity segment.`
|
|
4116
|
+
);
|
|
4117
|
+
}
|
|
4118
|
+
if (triggerEntity !== relationTargetEntity) {
|
|
4119
|
+
throw new Error(
|
|
4120
|
+
`Maintenance trigger '${trigger}' on relation '${relation.propertyName}' of '${ownerEntity}' names source entity '${triggerEntity}', but the relation's target (the projected source) is '${relationTargetEntity}'. #124 projects only from the relation target's payload (payload \u540C\u68B1); a trigger on a different entity needs a write-time context fetch, which is Phase 2 \u2014 restrict \`maintainedOn\` to '${relationTargetEntity}.*' triggers.`
|
|
4121
|
+
);
|
|
4122
|
+
}
|
|
4123
|
+
assertMaintenanceRoundTrips({
|
|
4124
|
+
ownerEntity,
|
|
4125
|
+
relationProperty: relation.propertyName,
|
|
4126
|
+
ownerMeta,
|
|
4127
|
+
sourceEntity: triggerEntity,
|
|
4128
|
+
sourceMeta,
|
|
4129
|
+
destinationKeyFields,
|
|
4130
|
+
keyBindingSourceFields: sourceFields,
|
|
4131
|
+
projection: project
|
|
4132
|
+
});
|
|
4133
|
+
const effect = buildEffect(
|
|
4134
|
+
relation,
|
|
4135
|
+
() => ownerClass,
|
|
4136
|
+
trigger,
|
|
4137
|
+
keys,
|
|
4138
|
+
project,
|
|
4139
|
+
sourceMeta,
|
|
4140
|
+
ownerEntity
|
|
4141
|
+
);
|
|
4142
|
+
items.push({
|
|
4143
|
+
trigger,
|
|
4144
|
+
sourceEntity: triggerEntity,
|
|
4145
|
+
sourceClass,
|
|
4146
|
+
ownerEntity,
|
|
4147
|
+
ownerClass,
|
|
4148
|
+
relationProperty: relation.propertyName,
|
|
4149
|
+
destinationRowKey: destinationRowKeyOf(ownerEntity, keys),
|
|
4150
|
+
effect
|
|
4151
|
+
});
|
|
4152
|
+
}
|
|
4153
|
+
}
|
|
4154
|
+
const aggregates = [...ownerMeta.aggregates].sort(
|
|
4155
|
+
(a, b) => a.propertyName < b.propertyName ? -1 : a.propertyName > b.propertyName ? 1 : 0
|
|
4156
|
+
);
|
|
4157
|
+
for (const agg of aggregates) {
|
|
4158
|
+
const maintainedOn = agg.options.write?.maintainedOn;
|
|
4159
|
+
if (!maintainedOn || maintainedOn.length === 0) continue;
|
|
4160
|
+
const sourceClass = agg.targetFactory();
|
|
4161
|
+
const sourceMeta = MetadataRegistry.get(sourceClass);
|
|
4162
|
+
const aggSourceEntity = logicalNameOf(sourceMeta);
|
|
4163
|
+
const { keys, sourceFields } = buildDestinationKeys(agg.keyBinding);
|
|
4164
|
+
const destinationKeyFields = Object.keys(keys);
|
|
4165
|
+
const valueSourceFields = agg.options.value.op === "max" ? [agg.options.value.field] : [];
|
|
4166
|
+
for (const raw of maintainedOn) {
|
|
4167
|
+
if (!isMaintainTrigger(raw)) {
|
|
4168
|
+
const { event: event2 } = parseTrigger(raw);
|
|
4169
|
+
throw new Error(
|
|
4170
|
+
`Maintenance trigger ${JSON.stringify(raw)} on \`@aggregate\` field '${agg.propertyName}' of '${ownerEntity}' is not a well-formed \`Entity.event\` trigger (event must be one of ${MAINTAIN_EVENTS.map((e) => `'${e}'`).join(" / ")}; got '${event2}'). Fix the \`write.maintainedOn\` entry.`
|
|
4171
|
+
);
|
|
4172
|
+
}
|
|
4173
|
+
const trigger = raw;
|
|
4174
|
+
const parsed = parseTrigger(trigger);
|
|
4175
|
+
const triggerEntity = parsed.entity;
|
|
4176
|
+
const event = parsed.event;
|
|
4177
|
+
if (!resolveEntityByName(triggerEntity, registry)) {
|
|
4178
|
+
throw new Error(
|
|
4179
|
+
`Maintenance trigger '${trigger}' on \`@aggregate\` field '${agg.propertyName}' of '${ownerEntity}' names source entity '${triggerEntity}', which is not a registered model. Declare a model named '${triggerEntity}' or fix the trigger's entity segment.`
|
|
4180
|
+
);
|
|
4181
|
+
}
|
|
4182
|
+
if (triggerEntity !== aggSourceEntity) {
|
|
4183
|
+
throw new Error(
|
|
4184
|
+
`Maintenance trigger '${trigger}' on \`@aggregate\` field '${agg.propertyName}' of '${ownerEntity}' names source entity '${triggerEntity}', but the aggregate's source (the entity being counted) is '${aggSourceEntity}'. A counter aggregates only the rows of its own source (payload \u540C\u68B1); a trigger on a different entity is Phase 2 \u2014 restrict \`maintainedOn\` to '${aggSourceEntity}.*' triggers.`
|
|
4185
|
+
);
|
|
4186
|
+
}
|
|
4187
|
+
if (agg.options.value.op === "count" && COUNT_DELTA_FOR_EVENT[event] === 0) {
|
|
4188
|
+
throw new Error(
|
|
4189
|
+
`\`@aggregate\` field '${agg.propertyName}' of '${ownerEntity}' is a \`count()\` counter maintained on '${trigger}', but a 'updated' event does not change a row count (it adds 0). A \`count()\` counter is maintained on 'created' (+1) and 'removed' (-1); drop the 'updated' trigger.`
|
|
4190
|
+
);
|
|
4191
|
+
}
|
|
4192
|
+
assertCounterRoundTrips({
|
|
4193
|
+
ownerEntity,
|
|
4194
|
+
aggregateField: agg.propertyName,
|
|
4195
|
+
ownerMeta,
|
|
4196
|
+
sourceEntity: triggerEntity,
|
|
4197
|
+
sourceMeta,
|
|
4198
|
+
destinationKeyFields,
|
|
4199
|
+
keyBindingSourceFields: sourceFields,
|
|
4200
|
+
valueSourceFields
|
|
4201
|
+
});
|
|
4202
|
+
const effect = buildCounterEffect(agg, () => ownerClass, trigger, event, keys);
|
|
4203
|
+
items.push({
|
|
4204
|
+
trigger,
|
|
4205
|
+
sourceEntity: triggerEntity,
|
|
4206
|
+
sourceClass,
|
|
4207
|
+
ownerEntity,
|
|
4208
|
+
ownerClass,
|
|
4209
|
+
relationProperty: agg.propertyName,
|
|
4210
|
+
destinationRowKey: destinationRowKeyOf(ownerEntity, keys),
|
|
4211
|
+
effect
|
|
4212
|
+
});
|
|
4213
|
+
}
|
|
4214
|
+
}
|
|
4215
|
+
}
|
|
4216
|
+
for (const view of [...views].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0)) {
|
|
4217
|
+
const ownerClass = view.viewClass;
|
|
4218
|
+
const ownerMeta = MetadataRegistry.get(ownerClass);
|
|
4219
|
+
const ownerEntity = logicalNameOf(ownerMeta);
|
|
4220
|
+
view.slices.forEach((slice, sliceIndex) => {
|
|
4221
|
+
const sourceClass = slice.sourceClass;
|
|
4222
|
+
const sourceMeta = MetadataRegistry.get(sourceClass);
|
|
4223
|
+
const sourceEntity = logicalNameOf(sourceMeta);
|
|
4224
|
+
const keys = slice.keys;
|
|
4225
|
+
const destinationKeyFields = Object.keys(keys);
|
|
4226
|
+
const keyBindingSourceFields = Object.values(keys).map((p) => sourceAttributeOf(p));
|
|
4227
|
+
for (const trigger of slice.maintainedOn) {
|
|
4228
|
+
const { entity: triggerEntity } = parseTrigger(trigger);
|
|
4229
|
+
if (triggerEntity !== sourceEntity) {
|
|
4230
|
+
throw new Error(
|
|
4231
|
+
`defineView '${view.name}': source slice trigger '${trigger}' names entity '${triggerEntity}', but the slice's source entity is '${sourceEntity}'. A view source projects only from its own payload (payload \u540C\u68B1); a trigger on a different entity needs a write-time context fetch (RFC \xA74.A.6), which is a deferred follow-up \u2014 restrict the slice's \`maintainedOn\` to '${sourceEntity}.*' triggers.`
|
|
4232
|
+
);
|
|
4233
|
+
}
|
|
4234
|
+
assertMaintenanceRoundTrips({
|
|
4235
|
+
ownerEntity,
|
|
4236
|
+
relationProperty: viewSliceProperty(view.name, sliceIndex, slice),
|
|
4237
|
+
ownerMeta,
|
|
4238
|
+
sourceEntity,
|
|
4239
|
+
sourceMeta,
|
|
4240
|
+
destinationKeyFields,
|
|
4241
|
+
keyBindingSourceFields,
|
|
4242
|
+
projection: slice.project
|
|
4243
|
+
});
|
|
4244
|
+
if (slice.predicate) {
|
|
4245
|
+
if (!isPathRooted2(slice.predicate.path)) {
|
|
4246
|
+
throw new Error(
|
|
4247
|
+
`defineView '${view.name}': sparse-view predicate path ${JSON.stringify(slice.predicate.path)} is not a payload-rooted source path (\`$.input.*\` / \`$.entity.*\`). The drain evaluates the predicate against the source image \u2014 use a source attribute path.`
|
|
4248
|
+
);
|
|
4249
|
+
}
|
|
4250
|
+
const predAttr = sourceAttributeOf(slice.predicate.path);
|
|
4251
|
+
if (!sourcePayloadAttributes(sourceMeta).has(predAttr)) {
|
|
4252
|
+
throw new Error(
|
|
4253
|
+
`defineView '${view.name}': sparse-view predicate reads source attribute '${predAttr}' (from '${slice.predicate.path}'), but the source entity '${sourceEntity}' carries no such attribute on its payload (available: ${[...sourcePayloadAttributes(sourceMeta)].sort().map((f) => `'${f}'`).join(", ")}). A membership predicate is payload \u540C\u68B1 only \u2014 reject.`
|
|
4254
|
+
);
|
|
4255
|
+
}
|
|
4256
|
+
}
|
|
4257
|
+
const effect = buildViewEffect(view, slice, trigger);
|
|
4258
|
+
items.push({
|
|
4259
|
+
trigger,
|
|
4260
|
+
sourceEntity,
|
|
4261
|
+
sourceClass,
|
|
4262
|
+
ownerEntity,
|
|
4263
|
+
ownerClass,
|
|
4264
|
+
relationProperty: viewSliceProperty(view.name, sliceIndex, slice),
|
|
4265
|
+
destinationRowKey: destinationRowKeyOf(ownerEntity, keys),
|
|
4266
|
+
effect
|
|
4267
|
+
});
|
|
4268
|
+
}
|
|
4269
|
+
});
|
|
4270
|
+
}
|
|
4271
|
+
assertNoCycle(items);
|
|
4272
|
+
const byTrigger = /* @__PURE__ */ new Map();
|
|
4273
|
+
const sameRow = /* @__PURE__ */ new Map();
|
|
4274
|
+
for (const item of items) {
|
|
4275
|
+
const triggerBucket = byTrigger.get(item.trigger);
|
|
4276
|
+
if (triggerBucket) triggerBucket.push(item);
|
|
4277
|
+
else byTrigger.set(item.trigger, [item]);
|
|
4278
|
+
const rowKey = `${item.trigger}\0${item.destinationRowKey}`;
|
|
4279
|
+
const rowBucket = sameRow.get(rowKey);
|
|
4280
|
+
if (rowBucket) rowBucket.push(item);
|
|
4281
|
+
else sameRow.set(rowKey, [item]);
|
|
4282
|
+
}
|
|
4283
|
+
const multiMaintainerTargets = /* @__PURE__ */ new Map();
|
|
4284
|
+
for (const [rowKey, bucket] of sameRow) {
|
|
4285
|
+
if (bucket.length > 1) multiMaintainerTargets.set(rowKey, bucket);
|
|
4286
|
+
}
|
|
4287
|
+
return {
|
|
4288
|
+
items,
|
|
4289
|
+
byTrigger,
|
|
4290
|
+
effectsFor(trigger) {
|
|
4291
|
+
return byTrigger.get(trigger) ?? [];
|
|
4292
|
+
},
|
|
4293
|
+
multiMaintainerTargets
|
|
4294
|
+
};
|
|
4295
|
+
}
|
|
4296
|
+
|
|
3421
4297
|
export {
|
|
3422
4298
|
isColumn,
|
|
3423
4299
|
createColumnMap,
|
|
@@ -3480,5 +4356,26 @@ export {
|
|
|
3480
4356
|
attachModelClass,
|
|
3481
4357
|
resolveModelClass,
|
|
3482
4358
|
TransactionContext,
|
|
3483
|
-
executeTransaction
|
|
4359
|
+
executeTransaction,
|
|
4360
|
+
maintainTrigger,
|
|
4361
|
+
isMaintainTrigger,
|
|
4362
|
+
preview,
|
|
4363
|
+
identity,
|
|
4364
|
+
LIFECYCLE_CONTRACT_MARKER,
|
|
4365
|
+
isLifecycleContract,
|
|
4366
|
+
ENTITY_WRITES_MARKER,
|
|
4367
|
+
isEntityWritesDefinition,
|
|
4368
|
+
entityWrites,
|
|
4369
|
+
getEntityWrites,
|
|
4370
|
+
lifecyclePhaseForIntent,
|
|
4371
|
+
whenMember,
|
|
4372
|
+
ViewRegistry,
|
|
4373
|
+
isViewDefinition,
|
|
4374
|
+
defineView,
|
|
4375
|
+
defineViews,
|
|
4376
|
+
defineVersioned,
|
|
4377
|
+
isProjectionDefinition,
|
|
4378
|
+
defineProjection,
|
|
4379
|
+
defineProjections,
|
|
4380
|
+
buildMaintenanceGraph
|
|
3484
4381
|
};
|