graphddb 0.7.9 → 0.8.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.
Files changed (45) hide show
  1. package/README.md +6 -6
  2. package/dist/cdc/index.d.ts +389 -4
  3. package/dist/cdc/index.js +4 -3
  4. package/dist/{chunk-ZPNRLOKA.js → chunk-GS4C5VGO.js} +4 -6
  5. package/dist/chunk-HNY2EJPV.js +1184 -0
  6. package/dist/{chunk-NYM7K2ST.js → chunk-I4LEJ4TF.js} +3812 -6724
  7. package/dist/{chunk-PFFPLD4B.js → chunk-L2NEDS7U.js} +725 -2112
  8. package/dist/chunk-L4QRCHRQ.js +278 -0
  9. package/dist/chunk-LGHSZIEE.js +187 -0
  10. package/dist/chunk-N4NWYNGZ.js +1987 -0
  11. package/dist/{chunk-PDUVTYC5.js → chunk-XTWXMOHD.js} +0 -1
  12. package/dist/cli.js +63 -252
  13. package/dist/index.d.ts +23 -1548
  14. package/dist/index.js +100 -1778
  15. package/dist/internal/index.d.ts +84 -0
  16. package/dist/internal/index.js +701 -0
  17. package/dist/{maintenance-view-adapter-BATUh_I8.d.ts → key-DR7_lpyk.d.ts} +538 -2975
  18. package/dist/linter/index.d.ts +39 -6
  19. package/dist/linter/index.js +22 -4
  20. package/dist/{registry-CXhP4TaE.d.ts → linter-C-vypgut.d.ts} +22 -22
  21. package/dist/prepared-artifact-BpPgkXEo.d.ts +281 -0
  22. package/dist/spec/index.d.ts +506 -4
  23. package/dist/spec/index.js +36 -17
  24. package/dist/testing/index.d.ts +2 -2
  25. package/dist/testing/index.js +4 -3
  26. package/dist/transform/index.d.ts +460 -1
  27. package/dist/transform/index.js +2085 -2
  28. package/dist/types-2PMXEn5x.d.ts +1205 -0
  29. package/dist/types-BXLzIcQD.d.ts +450 -0
  30. package/docs/cdc-projection.md +5 -5
  31. package/docs/class-hydration.md +1 -1
  32. package/docs/cqrs-contract.md +28 -20
  33. package/docs/design-patterns.md +5 -5
  34. package/docs/docs-generation.md +6 -6
  35. package/docs/middleware.md +15 -15
  36. package/docs/mutation-command-derivation.md +52 -42
  37. package/docs/prepared-statements.md +14 -14
  38. package/docs/python-bridge.md +113 -66
  39. package/docs/spec.md +153 -124
  40. package/docs/testing.md +9 -8
  41. package/package.json +20 -5
  42. package/dist/chunk-MMVHOUM4.js +0 -24
  43. package/dist/from-change-DanwjE5b.d.ts +0 -327
  44. package/dist/index-CtPJSMrc.d.ts +0 -934
  45. package/dist/relation-depth-Dg3yhl7S.d.ts +0 -36
@@ -1,327 +0,0 @@
1
- import { C as CdcEmulatorOptions, e as ChangeHandler, U as Unsubscribe, F as FaultSpec, g as ConcurrentRecomputeRef, c as ChangeEvent, E as EventLog, R as ReplayOptions, S as ShardId, V as ViewDefinition, m as EntityMetadata } from './maintenance-view-adapter-BATUh_I8.js';
2
-
3
- /**
4
- * CDC emulator (issue #72). Subscribes to the core write-capture seam
5
- * ({@link ChangeCaptureRegistry}) and turns each {@link RawWriteRecord} into a
6
- * DynamoDB-Streams-equivalent {@link ChangeEvent}, then delivers events to a
7
- * consumer under a deterministic delivery contract (spec §6): per-shard ordered,
8
- * batched, at-least-once, with checkpoint/resume, partial-batch failure, and a
9
- * dead-letter queue. Faults (duplicate / reorder / delay / dropThenRedeliver /
10
- * partialBatchFailure) and a concurrent-recompute race are injectable and
11
- * seeded for deterministic reproduction (spec §8).
12
- *
13
- * The emulator is dev/test only. The {@link ChangeEvent} type and delivery
14
- * contract match real Streams so a consumer is portable to production (spec §1).
15
- */
16
-
17
- /**
18
- * The CDC emulator (spec §10 API). Construct via {@link createCdcEmulator}.
19
- */
20
- declare class CdcEmulator {
21
- private readonly mode;
22
- private readonly clockMode;
23
- private readonly batchSize;
24
- private readonly maxRetries;
25
- private readonly startingPosition;
26
- private readonly shardCount;
27
- private readonly seed;
28
- private rng;
29
- private faultSpec;
30
- private concurrentRecompute;
31
- private concurrentRecomputeHook;
32
- /** Per-shard monotonic sequence counter. */
33
- private seqCounters;
34
- /** Per-shard delivery queue (queued/replay modes). */
35
- private queues;
36
- /** Per-shard last successfully checkpointed sequence number. */
37
- private checkpointMap;
38
- /**
39
- * Per-shard set of acked sequence numbers (as ints) not yet folded into the
40
- * checkpoint. The checkpoint advances over the contiguous prefix of acked
41
- * sequences (ReportBatchItemFailures semantics): a sequence is checkpointed
42
- * only once every sequence at or before it has been acked.
43
- */
44
- private ackedSeqs;
45
- /** Per-shard count of sequences ever assigned, to bound the prefix scan. */
46
- private maxSeqSeen;
47
- /** Recorded event log (record mode and always, for replay()). */
48
- private log;
49
- /** Dead-letter buffer. */
50
- private dlq;
51
- private handler;
52
- /** Virtual clock (ms since EPOCH). */
53
- private clockMs;
54
- /** Pending async work from inline-mode captures, awaited by callers. */
55
- private inflight;
56
- private detachCapture;
57
- private readonly models;
58
- constructor(opts?: CdcEmulatorOptions);
59
- private onRawWrite;
60
- private toChangeEvent;
61
- /** Parse a zero-padded sequence string back to its integer value. */
62
- private seqToInt;
63
- /** Render an integer sequence as the zero-padded string form. */
64
- private intToSeq;
65
- /**
66
- * Advance a shard's checkpoint over the contiguous prefix of acked sequence
67
- * numbers. Starting just past the current checkpoint, consume every
68
- * consecutive acked sequence; stop at the first gap (an un-acked sequence
69
- * still pending redelivery). Consumed sequences are removed from the acked
70
- * set to bound its size.
71
- */
72
- private advanceCheckpoint;
73
- private deriveEventName;
74
- private nowIso;
75
- private enqueue;
76
- /**
77
- * Deliver buffered, due records to the consumer, advancing per-shard
78
- * checkpoints on success and redelivering failures. In `queued`/`replay`
79
- * modes call this (or {@link advanceClock}) to drive delivery; `inline` calls
80
- * it automatically after each capture.
81
- */
82
- pump(maxBatches?: number): Promise<void>;
83
- /**
84
- * Pull up to `batchSize` due records from a shard, preserving ascending
85
- * sequence order (spec §6). When the `reorder` fault is on, the within-shard
86
- * order is deliberately shuffled to exercise invariant detection.
87
- */
88
- private collectBatch;
89
- private deliverBatch;
90
- /** Register the consumer. Returns an unsubscribe handle. */
91
- subscribe(handler: ChangeHandler): Unsubscribe;
92
- /** Await any inline-mode async deliveries triggered by writes. */
93
- flush(): Promise<void>;
94
- /**
95
- * Advance the virtual clock by `ms` and deliver records whose due-time has
96
- * arrived (spec §7). Reproduces throttle T / sweep period / staleness S time
97
- * behavior deterministically (AC4).
98
- */
99
- advanceClock(ms: number): Promise<void>;
100
- /** Current virtual time (ms since the emulator epoch). */
101
- now(): number;
102
- /** Configure fault injection (spec §8). Merges into the current spec. */
103
- fault(spec: FaultSpec): void;
104
- /**
105
- * Force a recompute to race a mark for the given ref (spec §8, §7). The
106
- * harness supplies the actual recompute via {@link onConcurrentRecompute};
107
- * this records which ref to race and arms the hook.
108
- */
109
- injectConcurrentRecompute(ref: ConcurrentRecomputeRef): void;
110
- /**
111
- * Register the callback the emulator invokes (before each record reaches the
112
- * consumer) when a concurrent recompute has been injected. This is how the
113
- * test harness wires its recompute into the delivery race.
114
- */
115
- onConcurrentRecompute(hook: (ref: ConcurrentRecomputeRef, event: ChangeEvent) => void | Promise<void>): void;
116
- /** Snapshot the recorded event log (spec §10). */
117
- record(): EventLog;
118
- /**
119
- * Replay an event log through the delivery pipeline, optionally shuffled and
120
- * duplicated (spec §10, AC5). Used to prove **replay equivalence**: starting
121
- * from any global order plus duplicates, a consumer that respects the
122
- * per-shard ordering contract (spec §6) reaches a final aggregate equal to a
123
- * full recompute from source.
124
- *
125
- * The shuffle models cross-shard reordering and at-least-once chaos at the
126
- * delivery boundary. It must NOT, however, break the within-shard ordering
127
- * guarantee that real DynamoDB Streams provides and that incremental
128
- * aggregation depends on — so after shuffling/duplicating, each shard's queue
129
- * is re-sorted into ascending original `sequenceNumber` order before delivery.
130
- * The original `sequenceNumber` (assigned at record time) is preserved so it
131
- * remains the source-of-truth ordering the consumer can rely on.
132
- */
133
- replay(log: EventLog, opts?: ReplayOptions): Promise<void>;
134
- /** Per-shard checkpoints (last acked sequence number). */
135
- checkpoints(): Record<ShardId, string>;
136
- /** Dead-lettered events (exceeded maxRetries). */
137
- deadLetters(): ChangeEvent[];
138
- /** Reset delivery state and re-seed (keeps the subscription + model opt-in). */
139
- reset(): void;
140
- /** Tear down: detach from the core seam and disable model streaming. */
141
- close(): void;
142
- }
143
- /** Create a CDC emulator (spec §10). */
144
- declare function createCdcEmulator(opts?: CdcEmulatorOptions): CdcEmulator;
145
-
146
- /**
147
- * CDC **maintenance drain** (issue #130) — the asynchronous lower for
148
- * `updateMode: 'stream'` maintainers (Epic #118 §5.1).
149
- *
150
- * A synchronous (`updateMode: 'mutation'`) maintainer composes its owner-row write
151
- * into the SAME atomic `TransactWriteItems` as the source write (#127). A
152
- * `updateMode: 'stream'` maintainer instead emits a **maintenance-outbox** marker row
153
- * (`OUTBOX#MAINT#…`) ATOMICALLY with the source write (the #130 compile lowering in
154
- * `src/spec/mutation-command.ts`), and THIS consumer — driven by the {@link
155
- * import('./emulator.js').CdcEmulator} (or, in production, real DynamoDB Streams +
156
- * a Lambda built around {@link createMaintenanceDrainHandler}) — applies the owner-row
157
- * write asynchronously.
158
- *
159
- * ## Why a separate async path exists (not just "do it later synchronously")
160
- *
161
- * The async path realizes the maintenance operations a single synchronous
162
- * `UpdateExpression` cannot express against an unread item:
163
- *
164
- * - **collection `maxItems` trim** — keeping a bounded, ordered list requires reading
165
- * the current list, appending, sorting, and trimming (a read-modify-write); a
166
- * synchronous `list_append` can only append (Phase 1 was append-only, #127).
167
- * - **`removed`-driven splice** — removing an entry from a maintained collection on a
168
- * source `removed` event likewise needs the current list.
169
- * - **running `max(field)`** — a conditional `SET` (`#a = :v` guarded by
170
- * `attribute_not_exists(#a) OR #a < :v`) whose failed guard, if run synchronously,
171
- * would roll back the legitimate source write.
172
- *
173
- * ## Delivery guarantees (reused from the CDC substrate)
174
- *
175
- * The maintenance-outbox row is recorded ATOMICALLY with the source write, so the
176
- * intent can never be lost ("wrote the row, lost the maintenance" is impossible). The
177
- * CDC substrate then delivers it **at-least-once**, **per-shard ordered** (a shard is
178
- * `hash(pk)`, and the outbox row's `pk` is keyed off the SOURCE row's key, so all
179
- * maintenance events for one source entity land on one shard in source-write order),
180
- * with a **DLQ** for poison events. This consumer is therefore written to be
181
- * **idempotent / commutative** wherever at-least-once redelivery could double-apply:
182
- *
183
- * - a `snapshot` `SET` is naturally idempotent (re-applying the same projection is a
184
- * no-op);
185
- * - a `collection` rebuild de-duplicates by the projected item's identity key, so a
186
- * redelivered append does not grow the list twice;
187
- * - a `max` conditional `SET` is idempotent (the guard rejects a non-greater value);
188
- * - a `count` `ADD` is the ONE non-idempotent op — but the source-row create guard
189
- * (`attribute_not_exists`) means a given source `created` event is emitted at most
190
- * once into the outbox, and the per-shard checkpoint advances only over acked events,
191
- * so a `count` is applied once per genuine source lifecycle. (A duplicate DELIVERY of
192
- * the same event is folded by the consumer's per-event de-dup set within a drain.)
193
- */
194
-
195
- /**
196
- * The PK prefix a maintenance-outbox row keys on — the single source of truth shared
197
- * with the compiler ({@link import('../spec/mutation-command.js')}'s
198
- * `MAINT_OUTBOX_PK_PREFIX`). The drain selects its events by `keys.pk.startsWith` of
199
- * this. Kept as a local literal (rather than importing a non-exported compiler const)
200
- * so the cdc module does not depend on the spec module; the value is asserted equal by
201
- * the integration tests that round-trip a real stream maintainer through both.
202
- */
203
- declare const MAINT_OUTBOX_PK_PREFIX = "OUTBOX#MAINT#";
204
- /** Options for {@link createMaintenanceDrain}. */
205
- interface MaintenanceDrainOptions {
206
- /**
207
- * The model classes (or `ModelStatic`s) whose stream maintainers this drain
208
- * applies. The drain builds a scoped {@link MaintenanceGraph} over them to resolve
209
- * each maintenance-outbox event back to its declared effect. Defaults to the GLOBAL
210
- * registry when omitted.
211
- */
212
- readonly models?: readonly Function[];
213
- /**
214
- * The {@link ViewDefinition}s (`defineView`) whose maintainers this drain applies
215
- * (issue #132). A view's destination is its dedicated view model; its source
216
- * classes should also be in {@link models} so the drain can resolve their
217
- * maintenance-outbox events. Defaults to none.
218
- */
219
- readonly views?: readonly ViewDefinition[];
220
- }
221
- /**
222
- * A maintenance drain: a CDC consumer ({@link ChangeHandler}) that applies the
223
- * owner-row writes for `updateMode: 'stream'` maintainers. Construct via
224
- * {@link createMaintenanceDrain}, then `subscribe` its {@link handler} to a
225
- * {@link import('./emulator.js').CdcEmulator} (dev/test) or wire it into a real
226
- * Streams Lambda (production).
227
- */
228
- declare class MaintenanceDrain {
229
- private readonly graph;
230
- /** Owner entity name → its model class (for the owner-row key derivation). */
231
- private readonly ownerByName;
232
- /** Per-event-id de-dup within a single handler invocation (at-least-once). */
233
- private appliedSeq;
234
- /** Count of owner-row writes applied (test/diagnostic). */
235
- private applied;
236
- constructor(opts?: MaintenanceDrainOptions);
237
- /** The CDC {@link ChangeHandler} to subscribe to an emulator / Streams source. */
238
- readonly handler: ChangeHandler;
239
- /** Resolve one maintenance-outbox event to its effect and apply the owner-row write. */
240
- private applyOne;
241
- /** Resolve a maintenance-outbox event back to the declared {@link MaintainItem}. */
242
- private resolve;
243
- /** A `snapshot` SET of each projected attribute onto the owner row (idempotent). */
244
- private applySnapshot;
245
- /**
246
- * A sparse-view **membership** write (#133): evaluate the membership predicate against
247
- * the source image; PUT the view row (its projection + key fields) when the predicate
248
- * holds, DELETE it when it flips false. A source `removed` event always deletes (the
249
- * source no longer exists, so its view row must disappear regardless of the predicate).
250
- *
251
- * Both ops are idempotent under at-least-once redelivery: a repeated PUT writes the same
252
- * row, a repeated DELETE is a no-op. The view row is keyed by the source identity
253
- * (`effect.keys`), so the PUT/DELETE always targets the SAME physical row the predicate
254
- * gates.
255
- */
256
- private applyMembership;
257
- /**
258
- * A running `max`: read the owner row, and `SET` the attribute to the source value
259
- * only when it is greater (or absent). Implemented as a conditional update so a
260
- * redelivered / out-of-order older value never regresses the stored max — idempotent
261
- * and commutative under at-least-once delivery.
262
- */
263
- private applyMax;
264
- /**
265
- * A bounded `collection`: read the current list, apply the event (append for a
266
- * `created`/`updated` source, splice for a `removed`), de-duplicate by the projected
267
- * identity key, order by `orderBy` (in `orderDir`, default DESC), trim to `maxItems`, and write the whole
268
- * list back. The read-modify-write is exactly what a single synchronous
269
- * `UpdateExpression` cannot do — the reason a bounded/ordered collection is a stream
270
- * maintainer (Phase 1 sync was append-only).
271
- */
272
- private applyCollection;
273
- /** Owner-row writes applied so far (test/diagnostic). */
274
- appliedCount(): number;
275
- /** Clear the per-delivery de-dup set + counters (test reset). */
276
- reset(): void;
277
- }
278
- /** Create a {@link MaintenanceDrain} (issue #130). */
279
- declare function createMaintenanceDrain(opts?: MaintenanceDrainOptions): MaintenanceDrain;
280
- /**
281
- * Create just the CDC {@link ChangeHandler} for a maintenance drain — the form to
282
- * subscribe to an emulator or wire into a production Streams Lambda. A thin wrapper
283
- * over {@link createMaintenanceDrain} for callers that only need the handler.
284
- */
285
- declare function createMaintenanceDrainHandler(opts?: MaintenanceDrainOptions): ChangeHandler;
286
-
287
- /**
288
- * `fromChange` — the pure `(event) => [old, new]` typed mapper (issue #153).
289
- *
290
- * This is graphddb's HALF of the CDC-projection boundary (see
291
- * `docs/cdc-projection.md`): it parses a raw {@link ChangeEvent}'s `oldImage` /
292
- * `newImage` into typed model instances and does nothing downstream of that (no
293
- * sink write, no dedup, no subscription). `Model.fromChange(event)` on `DDBModel`
294
- * delegates here.
295
- *
296
- * The two responsibilities packed into one call are **routing** and **parse**:
297
- *
298
- * - Routing — an event that is not for `modelClass` yields `[null, null]`. A valid
299
- * event for the class always has at least one non-null image, so `[null, null]`
300
- * is an unambiguous "not for this model" signal and no separate `owns()` guard is
301
- * needed. The match is by `event.model` (the resolved model name the write path /
302
- * emulator stamps) when present, falling back to the model's PK prefix
303
- * (`keys.pk` begins with `<prefix>`) when the event carries no `model`.
304
- * - Parse — each present image is hydrated via the existing {@link hydrate}
305
- * (raw item → typed value, INCLUDING ISO 8601 → `Date` for `@datetime` and
306
- * embedded reconstruction), returning ALL fields (no projection — narrowing does
307
- * not reduce network cost since the image is already on the stream). The hydrated
308
- * plain record is then loaded onto a fresh model instance so the result is a
309
- * genuine `InstanceType` (like the read path's `{ hydrate }` factory option).
310
- *
311
- * The image side present per event kind mirrors DynamoDB Streams:
312
- *
313
- * - INSERT → `[null, new ]`
314
- * - MODIFY → `[old, new ]`
315
- * - REMOVE → `[old, null]`
316
- */
317
-
318
- /**
319
- * Parse a {@link ChangeEvent} into the `[oldRecord, newRecord]` tuple for the given
320
- * model. Returns `[null, null]` when the event is not for this model (routing). The
321
- * caller (`DDBModel.fromChange`) supplies the resolved metadata + class; this
322
- * function is the runtime core and is deliberately model-name-string driven so
323
- * `DDBModel.subscribe` can reuse it when routing a batch.
324
- */
325
- declare function parseChange<T extends object>(event: ChangeEvent, metadata: EntityMetadata, modelName: string, modelClass: new () => T): [T | null, T | null];
326
-
327
- export { CdcEmulator as C, MAINT_OUTBOX_PK_PREFIX as M, MaintenanceDrain as a, type MaintenanceDrainOptions as b, createCdcEmulator as c, createMaintenanceDrain as d, createMaintenanceDrainHandler as e, parseChange as p };