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