event-sourced-collection 0.0.1 → 0.0.2

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.
@@ -0,0 +1,629 @@
1
+ import { uuidv7 as generateEventId } from "uuidv7";
2
+ //#region src/sync.ts
3
+ function createHttpTransport(config) {
4
+ return {
5
+ push: createHttpPushEvents(config.push, config.headers),
6
+ pull: createHttpPullEvents(config.pull, config.headers)
7
+ };
8
+ }
9
+ function createSyncTransport(config) {
10
+ if (!config) return null;
11
+ if (isTransport(config)) return {
12
+ push: config.push,
13
+ pull: config.pull
14
+ };
15
+ const pushUrl = getPushUrl(config);
16
+ const pullUrl = getPullUrl(config);
17
+ const push = "pushEvents" in config && config.pushEvents ? config.pushEvents : pushUrl ? createHttpPushEvents(pushUrl, config.headers) : void 0;
18
+ const pullEvents = "pullEvents" in config ? config.pullEvents : void 0;
19
+ const pull = pullEvents ? createPullFromHandler(pullEvents) : pullUrl ? createHttpPullEvents(pullUrl, config.headers) : void 0;
20
+ if (!push && !pull) return null;
21
+ return {
22
+ push,
23
+ pull
24
+ };
25
+ }
26
+ function normalizePushResponse(response) {
27
+ if (isConfirmationArray(response)) return { confirmed: response };
28
+ return {
29
+ confirmed: response.confirmed,
30
+ failed: response.failed
31
+ };
32
+ }
33
+ function isConfirmationArray(response) {
34
+ return Array.isArray(response);
35
+ }
36
+ function createHttpPushEvents(url, headers) {
37
+ return async (events) => {
38
+ if (events.length === 0) return { confirmed: [] };
39
+ const resolvedHeaders = await resolveHeaders(headers);
40
+ const response = await fetch(url, {
41
+ method: "POST",
42
+ headers: {
43
+ "Content-Type": "application/json",
44
+ ...resolvedHeaders
45
+ },
46
+ body: JSON.stringify(events)
47
+ });
48
+ if (!response.ok) throw new SyncPushError(response.status, await response.text());
49
+ return response.json();
50
+ };
51
+ }
52
+ function createPullFromHandler(pullEvents) {
53
+ return (since) => pullEvents({ since });
54
+ }
55
+ function createHttpPullEvents(url, headers) {
56
+ return async (since) => {
57
+ const resolvedHeaders = await resolveHeaders(headers);
58
+ const pullUrl = appendSince(url, since);
59
+ const response = await fetch(pullUrl, { headers: {
60
+ Accept: "application/json",
61
+ ...resolvedHeaders
62
+ } });
63
+ if (!response.ok) throw new SyncPullError(response.status, await response.text());
64
+ return response.json();
65
+ };
66
+ }
67
+ async function resolveHeaders(headers) {
68
+ if (!headers) return {};
69
+ if (typeof headers === "function") return headers();
70
+ return headers;
71
+ }
72
+ function appendSince(url, since) {
73
+ return `${url}${url.includes("?") ? "&" : "?"}since=${encodeURIComponent(String(since))}`;
74
+ }
75
+ function getPushUrl(config) {
76
+ if ("pushUrl" in config && config.pushUrl) return config.pushUrl;
77
+ if ("push" in config && typeof config.push === "string") return config.push;
78
+ }
79
+ function getPullUrl(config) {
80
+ if ("pullUrl" in config && config.pullUrl) return config.pullUrl;
81
+ if ("pull" in config && typeof config.pull === "string") return config.pull;
82
+ }
83
+ var SyncPushError = class extends Error {
84
+ status;
85
+ body;
86
+ constructor(status, body) {
87
+ super(`Event push failed: HTTP ${status}`);
88
+ this.status = status;
89
+ this.body = body;
90
+ this.name = "SyncPushError";
91
+ }
92
+ };
93
+ var SyncPullError = class extends Error {
94
+ status;
95
+ body;
96
+ constructor(status, body) {
97
+ super(`Event pull failed: HTTP ${status}`);
98
+ this.status = status;
99
+ this.body = body;
100
+ this.name = "SyncPullError";
101
+ }
102
+ };
103
+ function isTransport(value) {
104
+ return "push" in value && typeof value.push === "function";
105
+ }
106
+ //#endregion
107
+ //#region src/utils/logger.ts
108
+ const noopLogger = {
109
+ debug: () => {},
110
+ info: () => {},
111
+ warn: () => {},
112
+ error: () => {}
113
+ };
114
+ const LOG_PREFIX = "[event-sourced]";
115
+ function createEventSourcedLogger(debug) {
116
+ if (debug === void 0 || debug === false) return noopLogger;
117
+ if (typeof debug === "object") return debug;
118
+ return {
119
+ debug: (message, data) => {
120
+ if (data === void 0) {
121
+ console.debug(LOG_PREFIX, message);
122
+ return;
123
+ }
124
+ console.debug(LOG_PREFIX, message, data);
125
+ },
126
+ info: (message, data) => {
127
+ if (data === void 0) {
128
+ console.info(LOG_PREFIX, message);
129
+ return;
130
+ }
131
+ console.info(LOG_PREFIX, message, data);
132
+ },
133
+ warn: (message, data) => {
134
+ if (data === void 0) {
135
+ console.warn(LOG_PREFIX, message);
136
+ return;
137
+ }
138
+ console.warn(LOG_PREFIX, message, data);
139
+ },
140
+ error: (message, data) => {
141
+ if (data === void 0) {
142
+ console.error(LOG_PREFIX, message);
143
+ return;
144
+ }
145
+ console.error(LOG_PREFIX, message, data);
146
+ }
147
+ };
148
+ }
149
+ //#endregion
150
+ //#region src/create-event-sourced-db.ts
151
+ const OUTBOX_ID = "outbox";
152
+ const INBOX_ID = "inbox";
153
+ const RESERVED_IDS = /* @__PURE__ */ new Set([OUTBOX_ID, INBOX_ID]);
154
+ async function createEventSourcedDB(config) {
155
+ assertReservedNamesAvailable(config.collections);
156
+ const log = createEventSourcedLogger(config.debug);
157
+ const transport = createSyncTransport(config.sync);
158
+ log.info("creating event-sourced db", {
159
+ collectionIds: Object.keys(config.collections),
160
+ hasTransport: transport !== null
161
+ });
162
+ const defaultSchemaVersion = config.schemaVersion ?? 1;
163
+ const seq = { value: 0 };
164
+ const outbox = createMetaCollection(config, OUTBOX_ID, (entry) => entry.eventId, defaultSchemaVersion);
165
+ const inbox = createMetaCollection(config, INBOX_ID, (entry) => entry.eventId, defaultSchemaVersion);
166
+ const userCollections = {};
167
+ for (const collectionId of Object.keys(config.collections)) {
168
+ const def = config.collections[collectionId];
169
+ const getKey = def.getKey;
170
+ const options = config.persistedCollectionOptions({
171
+ id: collectionId,
172
+ getKey,
173
+ persistence: config.persistence,
174
+ schemaVersion: def.schemaVersion ?? defaultSchemaVersion,
175
+ onInsert: createMutationHook(outbox, collectionId, "insert", seq, log),
176
+ onUpdate: createMutationHook(outbox, collectionId, "update", seq, log),
177
+ onDelete: createMutationHook(outbox, collectionId, "delete", seq, log)
178
+ });
179
+ const collection = config.createCollection(options);
180
+ const hasAcceptMutations = Boolean(collection.utils?.acceptMutations);
181
+ log.debug("registered collection", {
182
+ collectionId,
183
+ hasAcceptMutations
184
+ });
185
+ userCollections[collectionId] = collection;
186
+ }
187
+ const collections = {
188
+ ...userCollections,
189
+ outbox,
190
+ inbox
191
+ };
192
+ const replayTargets = collections;
193
+ const subscriptions = [outbox.subscribeChanges(() => {}), inbox.subscribeChanges(() => {})];
194
+ await outbox.preload();
195
+ await inbox.preload();
196
+ seq.value = nextLocalSeq(outbox);
197
+ log.info("preloaded meta collections", {
198
+ outboxCount: outbox.state.size,
199
+ inboxCount: inbox.state.size,
200
+ nextLocalSeq: seq.value
201
+ });
202
+ await replayInbox(inbox, replayTargets, log);
203
+ async function sync() {
204
+ if (!transport) {
205
+ log.warn("sync skipped: no transport configured");
206
+ return {
207
+ pushed: 0,
208
+ pulled: 0,
209
+ errors: []
210
+ };
211
+ }
212
+ log.info("sync started");
213
+ await outbox.preload();
214
+ await inbox.preload();
215
+ const errors = [];
216
+ let pushed = 0;
217
+ let pulled = 0;
218
+ try {
219
+ if (transport.push) pushed = await pushOutbox(outbox, transport.push, log);
220
+ else log.debug("push skipped: no push transport configured");
221
+ } catch (err) {
222
+ const error = toError(err);
223
+ log.error("push outbox failed", { message: error.message });
224
+ errors.push(error);
225
+ }
226
+ try {
227
+ if (transport.pull) pulled = await pullInbox(outbox, inbox, transport.pull, replayTargets, log);
228
+ else log.debug("pull skipped: no pull transport configured");
229
+ } catch (err) {
230
+ const error = toError(err);
231
+ log.error("pull inbox failed", { message: error.message });
232
+ errors.push(error);
233
+ }
234
+ log.info("sync finished", {
235
+ pushed,
236
+ pulled,
237
+ errorCount: errors.length
238
+ });
239
+ return {
240
+ pushed,
241
+ pulled,
242
+ errors
243
+ };
244
+ }
245
+ async function manualSync() {
246
+ log.info("manual sync started");
247
+ await outbox.preload();
248
+ await inbox.preload();
249
+ const errors = [];
250
+ let pushed = 0;
251
+ let pulled = 0;
252
+ let replayed = 0;
253
+ if (transport) {
254
+ try {
255
+ if (transport.push) pushed = await pushOutbox(outbox, transport.push, log);
256
+ else log.debug("manual sync push skipped: no push transport configured");
257
+ } catch (err) {
258
+ const error = toError(err);
259
+ log.error("manual sync push failed", { message: error.message });
260
+ errors.push(error);
261
+ }
262
+ try {
263
+ if (transport.pull) pulled = await pullInbox(outbox, inbox, transport.pull, replayTargets, log);
264
+ else log.debug("manual sync pull skipped: no pull transport configured");
265
+ } catch (err) {
266
+ const error = toError(err);
267
+ log.error("manual sync pull failed", { message: error.message });
268
+ errors.push(error);
269
+ }
270
+ } else log.warn("manual sync: no transport configured, skipping push/pull");
271
+ try {
272
+ replayed = await replayInbox(inbox, replayTargets, log);
273
+ } catch (err) {
274
+ const error = toError(err);
275
+ log.error("manual sync replay failed", { message: error.message });
276
+ errors.push(error);
277
+ }
278
+ log.info("manual sync finished", {
279
+ pushed,
280
+ pulled,
281
+ replayed,
282
+ errorCount: errors.length
283
+ });
284
+ return {
285
+ pushed,
286
+ pulled,
287
+ replayed,
288
+ errors
289
+ };
290
+ }
291
+ function dispose() {
292
+ log.debug("disposing event-sourced db");
293
+ for (const subscription of subscriptions) subscription.unsubscribe();
294
+ }
295
+ return {
296
+ collections,
297
+ sync,
298
+ manualSync,
299
+ dispose
300
+ };
301
+ }
302
+ function createMetaCollection(config, id, getKey, schemaVersion) {
303
+ const options = config.persistedCollectionOptions({
304
+ id,
305
+ getKey,
306
+ persistence: config.persistence,
307
+ schemaVersion
308
+ });
309
+ return config.createCollection(options);
310
+ }
311
+ function createMutationHook(outbox, collectionId, type, seq, log) {
312
+ return async (params) => {
313
+ for (const mutation of params.transaction.mutations) {
314
+ const payload = type === "delete" ? mutation.original : mutation.modified;
315
+ const entry = {
316
+ eventId: generateEventId(),
317
+ collectionId,
318
+ type,
319
+ key: mutation.key,
320
+ payload,
321
+ timestamp: Date.now(),
322
+ localSeq: seq.value++,
323
+ globalSeq: null,
324
+ sync: false,
325
+ syncStatus: "pending",
326
+ attemptCount: 0,
327
+ lastAttemptAt: null,
328
+ lastError: null,
329
+ lastErrorCode: null,
330
+ retryable: null
331
+ };
332
+ await outbox.insert(entry).isPersisted.promise;
333
+ log.debug("outbox entry created", {
334
+ eventId: entry.eventId,
335
+ collectionId,
336
+ type,
337
+ key: entry.key,
338
+ localSeq: entry.localSeq
339
+ });
340
+ }
341
+ return {};
342
+ };
343
+ }
344
+ async function pushOutbox(outbox, push, log) {
345
+ const pending = [...outbox.state.values()].filter((entry) => !entry.sync && entry.syncStatus !== "failed").sort((a, b) => a.localSeq - b.localSeq);
346
+ log.debug("push outbox", { pendingCount: pending.length });
347
+ if (pending.length === 0) return 0;
348
+ const attemptAt = Date.now();
349
+ for (const entry of pending) await outbox.update(entry.eventId, (draft) => {
350
+ draft.syncStatus = "pending";
351
+ draft.attemptCount = (draft.attemptCount ?? 0) + 1;
352
+ draft.lastAttemptAt = attemptAt;
353
+ draft.lastError = null;
354
+ draft.lastErrorCode = null;
355
+ draft.retryable = null;
356
+ }).isPersisted.promise;
357
+ const outbound = pending.map((entry) => ({
358
+ eventId: entry.eventId,
359
+ collectionId: entry.collectionId,
360
+ type: entry.type,
361
+ key: entry.key,
362
+ payload: entry.payload,
363
+ timestamp: entry.timestamp
364
+ }));
365
+ const response = normalizePushResponse(await push(outbound));
366
+ log.info("push outbox confirmed", {
367
+ sent: outbound.length,
368
+ confirmed: response.confirmed.length,
369
+ failed: response.failed?.length ?? 0
370
+ });
371
+ for (const confirmation of response.confirmed) {
372
+ await outbox.update(confirmation.eventId, (draft) => {
373
+ draft.sync = true;
374
+ draft.syncStatus = "synced";
375
+ draft.globalSeq = confirmation.globalSeq;
376
+ draft.lastError = null;
377
+ draft.lastErrorCode = null;
378
+ draft.retryable = null;
379
+ }).isPersisted.promise;
380
+ log.debug("outbox entry marked pushed", {
381
+ eventId: confirmation.eventId,
382
+ globalSeq: confirmation.globalSeq
383
+ });
384
+ }
385
+ for (const failure of response.failed ?? []) {
386
+ await outbox.update(failure.eventId, (draft) => {
387
+ draft.sync = false;
388
+ draft.syncStatus = "failed";
389
+ draft.lastError = failure.message;
390
+ draft.lastErrorCode = failure.code ?? null;
391
+ draft.retryable = failure.retryable ?? null;
392
+ }).isPersisted.promise;
393
+ log.warn("outbox entry marked failed", {
394
+ eventId: failure.eventId,
395
+ message: failure.message,
396
+ code: failure.code,
397
+ retryable: failure.retryable
398
+ });
399
+ }
400
+ return response.confirmed.length;
401
+ }
402
+ async function pullInbox(outbox, inbox, pull, targets, log) {
403
+ let pulled = 0;
404
+ let hasMore = true;
405
+ while (hasMore) {
406
+ const since = currentSince(inbox);
407
+ log.debug("pull inbox page", { since });
408
+ const response = await pull(since);
409
+ log.debug("pull inbox response", {
410
+ since,
411
+ eventCount: response.events.length,
412
+ hasMore: response.hasMore,
413
+ cursor: response.cursor
414
+ });
415
+ if (response.events.length === 0) break;
416
+ const sorted = [...response.events].sort((a, b) => a.globalSeq - b.globalSeq);
417
+ for (const event of sorted) {
418
+ if (outbox.has(event.eventId)) {
419
+ await markInboxEventSynced(inbox, event);
420
+ log.debug("pull skipped: event originated locally", {
421
+ eventId: event.eventId,
422
+ globalSeq: event.globalSeq
423
+ });
424
+ continue;
425
+ }
426
+ const existing = inbox.get(event.eventId);
427
+ if (existing?.sync) {
428
+ log.debug("pull skipped: inbox already applied", {
429
+ eventId: event.eventId,
430
+ globalSeq: event.globalSeq
431
+ });
432
+ continue;
433
+ }
434
+ if (!existing) {
435
+ await inbox.insert(toInboxEntry(event, false)).isPersisted.promise;
436
+ log.debug("inbox entry inserted", {
437
+ eventId: event.eventId,
438
+ globalSeq: event.globalSeq,
439
+ collectionId: event.collectionId
440
+ });
441
+ }
442
+ if (!await replayEvent(targets, event.collectionId, event.eventId, event.type, event.key, event.payload, log)) return pulled;
443
+ await inbox.update(event.eventId, (draft) => {
444
+ draft.sync = true;
445
+ }).isPersisted.promise;
446
+ log.info("pull replay applied", {
447
+ eventId: event.eventId,
448
+ globalSeq: event.globalSeq,
449
+ collectionId: event.collectionId,
450
+ type: event.type,
451
+ key: event.key
452
+ });
453
+ pulled++;
454
+ }
455
+ hasMore = response.hasMore;
456
+ }
457
+ log.info("pull inbox finished", { pulled });
458
+ return pulled;
459
+ }
460
+ async function replayInbox(inbox, targets, log) {
461
+ const pending = [...inbox.state.values()].filter((entry) => !entry.sync).sort((a, b) => a.globalSeq - b.globalSeq);
462
+ log.info("pending inbox replay started", { pendingCount: pending.length });
463
+ let replayed = 0;
464
+ for (const entry of pending) {
465
+ if (!await replayEvent(targets, entry.collectionId, entry.eventId, entry.type, entry.key, entry.payload, log)) break;
466
+ await inbox.update(entry.eventId, (draft) => {
467
+ draft.sync = true;
468
+ }).isPersisted.promise;
469
+ replayed++;
470
+ log.info("pending inbox replay applied", {
471
+ eventId: entry.eventId,
472
+ globalSeq: entry.globalSeq,
473
+ collectionId: entry.collectionId
474
+ });
475
+ }
476
+ log.info("pending inbox replay finished", { replayed });
477
+ return replayed;
478
+ }
479
+ async function replayEvent(targets, collectionId, eventId, type, key, payload, log) {
480
+ if (RESERVED_IDS.has(collectionId)) {
481
+ log.warn("replay skipped: reserved collection", {
482
+ eventId,
483
+ collectionId,
484
+ type,
485
+ key
486
+ });
487
+ return false;
488
+ }
489
+ const target = targets[collectionId];
490
+ if (!target) {
491
+ log.warn("replay skipped: unknown collection", {
492
+ eventId,
493
+ collectionId,
494
+ type,
495
+ key,
496
+ knownCollections: Object.keys(targets).filter((id) => !RESERVED_IDS.has(id))
497
+ });
498
+ return false;
499
+ }
500
+ if (!target.utils.acceptMutations) {
501
+ log.warn("replay skipped: collection missing acceptMutations", {
502
+ eventId,
503
+ collectionId,
504
+ type,
505
+ key,
506
+ targetId: target.id
507
+ });
508
+ return false;
509
+ }
510
+ log.debug("replay applying mutation", {
511
+ eventId,
512
+ collectionId,
513
+ type,
514
+ key
515
+ });
516
+ try {
517
+ await target.utils.acceptMutations({ mutations: [{
518
+ mutationId: eventId,
519
+ type,
520
+ key,
521
+ modified: payload,
522
+ original: payload,
523
+ changes: payload,
524
+ collection: target
525
+ }] });
526
+ log.info("replay mutation accepted", {
527
+ eventId,
528
+ collectionId,
529
+ type,
530
+ key
531
+ });
532
+ } catch (err) {
533
+ const error = toError(err);
534
+ log.error("replay mutation failed", {
535
+ eventId,
536
+ collectionId,
537
+ type,
538
+ key,
539
+ message: error.message
540
+ });
541
+ throw error;
542
+ }
543
+ return true;
544
+ }
545
+ function currentSince(inbox) {
546
+ let max = 0;
547
+ for (const entry of inbox.state.values()) if (entry.sync && entry.globalSeq > max) max = entry.globalSeq;
548
+ return max;
549
+ }
550
+ function nextLocalSeq(outbox) {
551
+ let max = -1;
552
+ for (const entry of outbox.state.values()) if (entry.localSeq > max) max = entry.localSeq;
553
+ return max + 1;
554
+ }
555
+ async function markInboxEventSynced(inbox, event) {
556
+ const existing = inbox.get(event.eventId);
557
+ if (!existing) {
558
+ await inbox.insert(toInboxEntry(event, true)).isPersisted.promise;
559
+ return;
560
+ }
561
+ if (!existing.sync || existing.globalSeq !== event.globalSeq) await inbox.update(event.eventId, (draft) => {
562
+ draft.globalSeq = event.globalSeq;
563
+ draft.sync = true;
564
+ }).isPersisted.promise;
565
+ }
566
+ function toInboxEntry(event, sync) {
567
+ return {
568
+ eventId: event.eventId,
569
+ globalSeq: event.globalSeq,
570
+ collectionId: event.collectionId,
571
+ type: event.type,
572
+ key: event.key,
573
+ payload: event.payload,
574
+ timestamp: event.timestamp,
575
+ sync
576
+ };
577
+ }
578
+ function assertReservedNamesAvailable(collections) {
579
+ for (const id of Object.keys(collections)) if (RESERVED_IDS.has(id)) throw new Error(`Collection id "${id}" is reserved. "outbox" and "inbox" are built-in collections.`);
580
+ }
581
+ function toError(err) {
582
+ return err instanceof Error ? err : new Error(String(err));
583
+ }
584
+ //#endregion
585
+ //#region src/lazy-singleton.ts
586
+ function createLazySingleton(factory, options = {}) {
587
+ let instance = null;
588
+ let initPromise = null;
589
+ const ensure = async () => {
590
+ options.guard?.();
591
+ if (instance) return instance;
592
+ if (!initPromise) initPromise = factory().then((resolved) => {
593
+ instance = resolved;
594
+ return resolved;
595
+ }).catch((error) => {
596
+ initPromise = null;
597
+ throw error;
598
+ });
599
+ return initPromise;
600
+ };
601
+ const message = options.notInitializedMessage ?? "Instance is not initialized. Call ensure() first.";
602
+ const proxy = new Proxy({}, {
603
+ get(_target, prop, receiver) {
604
+ if (!instance) throw new Error(message);
605
+ return Reflect.get(instance, prop, receiver);
606
+ },
607
+ has(_target, prop) {
608
+ if (!instance) throw new Error(message);
609
+ return Reflect.has(instance, prop);
610
+ },
611
+ getPrototypeOf() {
612
+ if (!instance) throw new Error(message);
613
+ return Reflect.getPrototypeOf(instance);
614
+ }
615
+ });
616
+ const reset = () => {
617
+ instance = null;
618
+ initPromise = null;
619
+ };
620
+ return {
621
+ ensure,
622
+ proxy,
623
+ reset
624
+ };
625
+ }
626
+ //#endregion
627
+ export { SyncPullError as a, createEventSourcedLogger as i, createEventSourcedDB as n, SyncPushError as o, generateEventId as r, createHttpTransport as s, createLazySingleton as t };
628
+
629
+ //# sourceMappingURL=lazy-singleton-DFrAsYTV.mjs.map