@korajs/store 1.0.0-beta.11 → 1.0.0-beta.13

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 (92) hide show
  1. package/README.md +29 -37
  2. package/dist/adapters/better-sqlite3.cjs +90 -4
  3. package/dist/adapters/better-sqlite3.cjs.map +1 -1
  4. package/dist/adapters/better-sqlite3.d.cts +21 -12
  5. package/dist/adapters/better-sqlite3.d.ts +21 -12
  6. package/dist/adapters/better-sqlite3.js +69 -5
  7. package/dist/adapters/better-sqlite3.js.map +1 -1
  8. package/dist/adapters/indexeddb.cjs +1316 -610
  9. package/dist/adapters/indexeddb.cjs.map +1 -1
  10. package/dist/adapters/indexeddb.d.cts +29 -3
  11. package/dist/adapters/indexeddb.d.ts +29 -3
  12. package/dist/adapters/indexeddb.js +247 -221
  13. package/dist/adapters/indexeddb.js.map +1 -1
  14. package/dist/adapters/sqlite-wasm-worker.cjs +803 -62
  15. package/dist/adapters/sqlite-wasm-worker.cjs.map +1 -1
  16. package/dist/adapters/sqlite-wasm-worker.js +480 -62
  17. package/dist/adapters/sqlite-wasm-worker.js.map +1 -1
  18. package/dist/adapters/sqlite-wasm.cjs +1032 -344
  19. package/dist/adapters/sqlite-wasm.cjs.map +1 -1
  20. package/dist/adapters/sqlite-wasm.d.cts +239 -18
  21. package/dist/adapters/sqlite-wasm.d.ts +239 -18
  22. package/dist/adapters/sqlite-wasm.js +111 -5
  23. package/dist/adapters/sqlite-wasm.js.map +1 -1
  24. package/dist/backup-4NUPDEMB.js +25 -0
  25. package/dist/chunk-32EDKWMI.js +876 -0
  26. package/dist/chunk-32EDKWMI.js.map +1 -0
  27. package/dist/{chunk-CYO3YKFL.js → chunk-5ZRRF6R2.js} +2 -2
  28. package/dist/chunk-A6GFH5IG.js +36 -0
  29. package/dist/chunk-A6GFH5IG.js.map +1 -0
  30. package/dist/chunk-DUKTWVZW.js +175 -0
  31. package/dist/chunk-DUKTWVZW.js.map +1 -0
  32. package/dist/chunk-HZTDBLDB.js +607 -0
  33. package/dist/chunk-HZTDBLDB.js.map +1 -0
  34. package/dist/{chunk-GSJ7EEUC.js → chunk-J5AGXF3D.js} +58 -9
  35. package/dist/chunk-J5AGXF3D.js.map +1 -0
  36. package/dist/chunk-MCQKEHOD.js +112 -0
  37. package/dist/chunk-MCQKEHOD.js.map +1 -0
  38. package/dist/chunk-P4V375FY.js +810 -0
  39. package/dist/chunk-P4V375FY.js.map +1 -0
  40. package/dist/chunk-QDUSJMFV.js +1095 -0
  41. package/dist/chunk-QDUSJMFV.js.map +1 -0
  42. package/dist/chunk-SJP2HTYZ.js +1003 -0
  43. package/dist/chunk-SJP2HTYZ.js.map +1 -0
  44. package/dist/chunk-W5F2LWG6.js +591 -0
  45. package/dist/chunk-W5F2LWG6.js.map +1 -0
  46. package/dist/chunk-Y5LRH7KI.js +5341 -0
  47. package/dist/chunk-Y5LRH7KI.js.map +1 -0
  48. package/dist/{export-audit-ODGQTQIK.js → export-audit-J2QXRHD2.js} +3 -3
  49. package/dist/index.cjs +9354 -4157
  50. package/dist/index.cjs.map +1 -1
  51. package/dist/index.d.cts +1064 -108
  52. package/dist/index.d.ts +1064 -108
  53. package/dist/index.js +249 -3092
  54. package/dist/index.js.map +1 -1
  55. package/dist/internal.cjs +580 -264
  56. package/dist/internal.cjs.map +1 -1
  57. package/dist/internal.d.cts +15 -85
  58. package/dist/internal.d.ts +15 -85
  59. package/dist/internal.js +10 -8
  60. package/dist/restore-FR7XIKT2.js +299 -0
  61. package/dist/restore-FR7XIKT2.js.map +1 -0
  62. package/dist/sql-builder-C4u55jKW.d.cts +608 -0
  63. package/dist/sql-builder-CGckdBDL.d.ts +608 -0
  64. package/dist/sqlite-wasm-channel-DVX93Uiq.d.cts +141 -0
  65. package/dist/sqlite-wasm-channel-DVX93Uiq.d.ts +141 -0
  66. package/dist/sqlite-wasm-channel-MLHLKEED.js +12 -0
  67. package/dist/store-IMWKZLWB.js +16 -0
  68. package/dist/store-IMWKZLWB.js.map +1 -0
  69. package/dist/{types-BslDOTk0.d.ts → types-Dh-xyUjP.d.cts} +99 -26
  70. package/dist/{types-BslDOTk0.d.cts → types-Dh-xyUjP.d.ts} +99 -26
  71. package/package.json +3 -2
  72. package/dist/backup-5S7K2Z35.js +0 -14
  73. package/dist/chunk-GSJ7EEUC.js.map +0 -1
  74. package/dist/chunk-HS6DBMST.js +0 -383
  75. package/dist/chunk-HS6DBMST.js.map +0 -1
  76. package/dist/chunk-RJIEKEN4.js +0 -751
  77. package/dist/chunk-RJIEKEN4.js.map +0 -1
  78. package/dist/chunk-TUR7RGEP.js +0 -84
  79. package/dist/chunk-TUR7RGEP.js.map +0 -1
  80. package/dist/chunk-VDBJQ75K.js +0 -78
  81. package/dist/chunk-VDBJQ75K.js.map +0 -1
  82. package/dist/chunk-YT7MRFCP.js +0 -687
  83. package/dist/chunk-YT7MRFCP.js.map +0 -1
  84. package/dist/sqlite-wasm-channel-GGHA2BXU.js +0 -12
  85. package/dist/sqlite-wasm-channel-L6o3bWQK.d.cts +0 -92
  86. package/dist/sqlite-wasm-channel-L6o3bWQK.d.ts +0 -92
  87. package/dist/types-BOO9EPt1.d.ts +0 -170
  88. package/dist/types-C_GDavG-.d.cts +0 -170
  89. /package/dist/{backup-5S7K2Z35.js.map → backup-4NUPDEMB.js.map} +0 -0
  90. /package/dist/{chunk-CYO3YKFL.js.map → chunk-5ZRRF6R2.js.map} +0 -0
  91. /package/dist/{export-audit-ODGQTQIK.js.map → export-audit-J2QXRHD2.js.map} +0 -0
  92. /package/dist/{sqlite-wasm-channel-GGHA2BXU.js.map → sqlite-wasm-channel-MLHLKEED.js.map} +0 -0
package/dist/index.js CHANGED
@@ -2,3102 +2,193 @@ import {
2
2
  decodeAuditExport,
3
3
  readAuditExportManifest,
4
4
  verifyAuditExportChecksum
5
- } from "./chunk-CYO3YKFL.js";
5
+ } from "./chunk-5ZRRF6R2.js";
6
6
  import {
7
7
  persistedAuditTraceFromEvent
8
8
  } from "./chunk-MYSJN3DF.js";
9
9
  import {
10
- BlobIntegrityError,
11
- MemoryBlobStore
12
- } from "./chunk-W4MTTNEZ.js";
13
- import {
14
- InvalidStateTransitionError,
15
- allocateNextSequenceInTransaction,
16
- allocateNextSequenceNumber,
17
- buildCountQuery,
18
- buildFieldFastForwardUpdateQuery,
19
- buildInsertQuery,
20
- buildLwwSoftDeleteQuery,
21
- buildSelectQuery,
22
- buildSoftDeleteQuery,
23
- buildUpdateQuery,
24
- executeDelete,
25
- executeInsert,
26
- executeUpdate,
27
- fieldVersionsForFields,
28
- isIncomingNewerThanRow,
29
- parseFieldVersions,
30
- readSequenceNumber,
31
- resolvePerFieldLww,
32
- serializeFieldVersions,
33
- serializeRowVersion,
34
- validateStateTransition,
35
- validateUpdateStateMachine
36
- } from "./chunk-YT7MRFCP.js";
37
- import {
38
- AdapterError,
39
- NoLeaderError,
40
- OptimisticLockError,
41
- PersistenceError,
42
- QueryError,
43
- RecordNotFoundError,
44
- StoreNotOpenError,
45
- WorkerInitError,
46
- WorkerTimeoutError
47
- } from "./chunk-TUR7RGEP.js";
48
- import {
49
- exportBackup,
50
- readBackupManifest,
51
- restoreBackup,
52
- verifyBackupChecksum
53
- } from "./chunk-HS6DBMST.js";
54
- import {
55
- decodeRichtext,
56
- decodeRichtextFieldsFromOpData,
57
- deserializeOperationWithCollection,
58
- deserializeRecord,
59
- encodeRichtext,
60
- encodeRichtextFieldsForOpData,
61
- richtextStatesEqual,
62
- richtextToPlainText,
63
- serializeOperation,
64
- serializeRecord
65
- } from "./chunk-GSJ7EEUC.js";
66
-
67
- // src/compaction/types.ts
68
- var COMPACTION_BASELINE_META_KEY = "compaction_baseline_at";
69
-
70
- // src/compaction/compact-operation-log.ts
71
- import { HybridLogicalClock, createVersionVector, quoteIdent } from "@korajs/core";
72
- function computeAckCompactionWatermark(serverVector) {
73
- const watermark = createVersionVector();
74
- for (const [nodeId, seq] of serverVector) {
75
- if (seq > 0) {
76
- watermark.set(nodeId, seq);
77
- }
78
- }
79
- return watermark;
80
- }
81
- async function compactOperationLog(adapter, schema, strategy, serverVector) {
82
- if (strategy.mode === "never") {
83
- return { deletedCount: 0, watermark: createVersionVector() };
84
- }
85
- const watermark = computeAckCompactionWatermark(serverVector);
86
- if (watermark.size === 0) {
87
- return { deletedCount: 0, watermark };
88
- }
89
- const ageCutoffWall = strategy.mode === "after-days" ? Date.now() - strategy.days * 24 * 60 * 60 * 1e3 : null;
90
- const ageCutoffSerialized = ageCutoffWall !== null ? HybridLogicalClock.serialize({
91
- wallTime: ageCutoffWall,
92
- logical: 0,
93
- nodeId: ""
94
- }).slice(0, 15) : null;
95
- let deletedCount = 0;
96
- const collectionNames = Object.keys(schema.collections);
97
- await adapter.transaction(async (tx) => {
98
- for (const collectionName of collectionNames) {
99
- const table = quoteIdent(`_kora_ops_${collectionName}`);
100
- for (const [nodeId, maxSeq] of watermark) {
101
- if (maxSeq <= 0) continue;
102
- const countSql = buildCountSql(table, ageCutoffSerialized !== null);
103
- const countParams = ageCutoffSerialized !== null ? [nodeId, maxSeq, ageCutoffSerialized] : [nodeId, maxSeq];
104
- const countRows = await tx.query(countSql, countParams);
105
- deletedCount += countRows[0]?.count ?? 0;
106
- const deleteSql = buildDeleteSql(table, ageCutoffSerialized !== null);
107
- await tx.execute(deleteSql, countParams);
108
- }
109
- }
110
- await tx.execute("INSERT OR REPLACE INTO _kora_meta (key, value) VALUES (?, ?)", [
111
- COMPACTION_BASELINE_META_KEY,
112
- String(Date.now())
113
- ]);
114
- });
115
- return { deletedCount, watermark };
116
- }
117
- function buildCountSql(table, withAge) {
118
- if (withAge) {
119
- return `SELECT COUNT(*) as count FROM ${table} WHERE node_id = ? AND sequence_number <= ? AND SUBSTR(timestamp, 1, 15) < ?`;
120
- }
121
- return `SELECT COUNT(*) as count FROM ${table} WHERE node_id = ? AND sequence_number <= ?`;
122
- }
123
- function buildDeleteSql(table, withAge) {
124
- if (withAge) {
125
- return `DELETE FROM ${table} WHERE node_id = ? AND sequence_number <= ? AND SUBSTR(timestamp, 1, 15) < ?`;
126
- }
127
- return `DELETE FROM ${table} WHERE node_id = ? AND sequence_number <= ?`;
128
- }
129
-
130
- // src/sync/sync-state.ts
131
- import { createVersionVector as createVersionVector2 } from "@korajs/core";
132
- var LAST_ACKED_SERVER_VECTOR_META_KEY = "last_acked_server_vector";
133
- var DELTA_CURSOR_META_KEY = "delta_cursor";
134
- var DELIVERY_WATERMARK_META_KEY = "delivery_watermark";
135
- function serializeVersionVectorToMeta(vector) {
136
- const record = {};
137
- for (const [nodeId, seq] of vector) {
138
- record[nodeId] = seq;
139
- }
140
- return JSON.stringify(record);
141
- }
142
- function deserializeVersionVectorFromMeta(value) {
143
- const parsed = JSON.parse(value);
144
- const vector = createVersionVector2();
145
- for (const [nodeId, seq] of Object.entries(parsed)) {
146
- if (typeof seq === "number" && seq >= 0) {
147
- vector.set(nodeId, seq);
148
- }
149
- }
150
- return vector;
151
- }
152
- function mergeVersionVectors(a, b) {
153
- const merged = new Map(a);
154
- for (const [nodeId, seq] of b) {
155
- merged.set(nodeId, Math.max(merged.get(nodeId) ?? 0, seq));
156
- }
157
- return merged;
158
- }
159
- async function collectOperationsAheadOfServer(localVector, serverVector, fetchRange) {
160
- const missing = [];
161
- for (const [nodeId, localSeq] of localVector) {
162
- const serverSeq = serverVector.get(nodeId) ?? 0;
163
- if (localSeq > serverSeq) {
164
- const ops = await fetchRange(nodeId, serverSeq + 1, localSeq);
165
- missing.push(...ops);
166
- }
167
- }
168
- missing.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
169
- return missing;
170
- }
171
- async function loadLastAckedServerVector(adapter) {
172
- const rows = await adapter.query("SELECT value FROM _kora_meta WHERE key = ?", [
173
- LAST_ACKED_SERVER_VECTOR_META_KEY
174
- ]);
175
- const row = rows[0];
176
- if (!row?.value) {
177
- return createVersionVector2();
178
- }
179
- try {
180
- return deserializeVersionVectorFromMeta(row.value);
181
- } catch {
182
- return createVersionVector2();
183
- }
184
- }
185
- async function saveLastAckedServerVector(adapter, vector) {
186
- await adapter.execute("INSERT OR REPLACE INTO _kora_meta (key, value) VALUES (?, ?)", [
187
- LAST_ACKED_SERVER_VECTOR_META_KEY,
188
- serializeVersionVectorToMeta(vector)
189
- ]);
190
- }
191
- async function loadDeltaCursor(adapter) {
192
- const rows = await adapter.query("SELECT value FROM _kora_meta WHERE key = ?", [
193
- DELTA_CURSOR_META_KEY
194
- ]);
195
- return rows[0]?.value ?? null;
196
- }
197
- async function saveDeltaCursor(adapter, cursor) {
198
- if (cursor === null) {
199
- await adapter.execute("DELETE FROM _kora_meta WHERE key = ?", [DELTA_CURSOR_META_KEY]);
200
- return;
201
- }
202
- await adapter.execute("INSERT OR REPLACE INTO _kora_meta (key, value) VALUES (?, ?)", [
203
- DELTA_CURSOR_META_KEY,
204
- cursor
205
- ]);
206
- }
207
- function deliveryWatermarkKey(signature) {
208
- return signature === "" ? DELIVERY_WATERMARK_META_KEY : `${DELIVERY_WATERMARK_META_KEY}:${signature}`;
209
- }
210
- async function loadDeliveryWatermark(adapter, signature) {
211
- const rows = await adapter.query("SELECT value FROM _kora_meta WHERE key = ?", [
212
- deliveryWatermarkKey(signature)
213
- ]);
214
- const value = rows[0]?.value;
215
- if (value === void 0 || value === null) {
216
- return 0;
217
- }
218
- const parsed = Number(value);
219
- return Number.isFinite(parsed) && parsed >= 0 ? parsed : 0;
220
- }
221
- async function saveDeliveryWatermark(adapter, signature, watermark) {
222
- await adapter.execute("INSERT OR REPLACE INTO _kora_meta (key, value) VALUES (?, ?)", [
223
- deliveryWatermarkKey(signature),
224
- String(watermark)
225
- ]);
226
- }
227
- async function deleteDeliveryWatermark(adapter, signature) {
228
- await adapter.execute("DELETE FROM _kora_meta WHERE key = ?", [deliveryWatermarkKey(signature)]);
229
- }
230
- async function loadAllDeliveryWatermarks(adapter) {
231
- const rows = await adapter.query(
232
- "SELECT key, value FROM _kora_meta WHERE key = ? OR key LIKE ?",
233
- [DELIVERY_WATERMARK_META_KEY, `${DELIVERY_WATERMARK_META_KEY}:%`]
234
- );
235
- const result = {};
236
- const prefix = `${DELIVERY_WATERMARK_META_KEY}:`;
237
- for (const row of rows) {
238
- const signature = row.key === DELIVERY_WATERMARK_META_KEY ? "" : row.key.slice(prefix.length);
239
- const parsed = Number(row.value);
240
- if (Number.isFinite(parsed) && parsed >= 0) {
241
- result[signature] = parsed;
242
- }
243
- }
244
- return result;
245
- }
246
-
247
- // src/store/store.ts
248
- import {
249
- CausalTracker,
250
- HybridLogicalClock as HybridLogicalClock3,
251
- createVersionVector as createVersionVector3,
252
- generateUUIDv7 as generateUUIDv73,
253
- migrationStepsToSQL,
254
- quoteIdent as quoteIdent7,
255
- replayOperationsForRecord
256
- } from "@korajs/core";
257
-
258
- // src/collection/collection.ts
259
- import { quoteIdent as quoteIdent2 } from "@korajs/core";
260
- var Collection = class {
261
- constructor(name, definition, schema, adapter, clock, nodeId, allocateSequenceNumber, onMutation, relationEnforcer, mutationHandler, causalTracker, secretKeyProvider) {
262
- this.name = name;
263
- this.definition = definition;
264
- this.schema = schema;
265
- this.adapter = adapter;
266
- this.clock = clock;
267
- this.nodeId = nodeId;
268
- this.allocateSequenceNumber = allocateSequenceNumber;
269
- this.onMutation = onMutation;
270
- this.relationEnforcer = relationEnforcer;
271
- this.mutationHandler = mutationHandler;
272
- this.causalTracker = causalTracker;
273
- this.secretKeyProvider = secretKeyProvider;
274
- }
275
- name;
276
- definition;
277
- schema;
278
- adapter;
279
- clock;
280
- nodeId;
281
- allocateSequenceNumber;
282
- onMutation;
283
- relationEnforcer;
284
- mutationHandler;
285
- causalTracker;
286
- secretKeyProvider;
287
- mutationContext() {
288
- return {
289
- collection: this.name,
290
- definition: this.definition,
291
- schema: this.schema,
292
- adapter: this.adapter,
293
- clock: this.clock,
294
- nodeId: this.nodeId,
295
- allocateSequenceNumber: this.allocateSequenceNumber,
296
- onMutation: this.onMutation,
297
- relationEnforcer: this.relationEnforcer,
298
- causalTracker: this.causalTracker,
299
- inTransaction: false,
300
- secretKeyProvider: this.secretKeyProvider
301
- };
302
- }
303
- async insert(data) {
304
- if (this.mutationHandler) {
305
- return this.mutationHandler.insert(this.name, data);
306
- }
307
- return executeInsert(this.mutationContext(), data);
308
- }
309
- async findById(id) {
310
- const rows = await this.adapter.query(
311
- `SELECT * FROM ${quoteIdent2(this.name)} WHERE id = ? AND _deleted = 0`,
312
- [id]
313
- );
314
- const row = rows[0];
315
- if (!row) return null;
316
- return deserializeRecord(row, this.definition.fields);
317
- }
318
- async update(id, data) {
319
- if (this.mutationHandler) {
320
- return this.mutationHandler.update(this.name, id, data);
321
- }
322
- return executeUpdate(this.mutationContext(), id, data);
323
- }
324
- async delete(id) {
325
- if (this.mutationHandler) {
326
- await this.mutationHandler.delete(this.name, id);
327
- return;
328
- }
329
- await executeDelete(this.mutationContext(), id);
330
- }
331
- getName() {
332
- return this.name;
333
- }
334
- getDefinition() {
335
- return this.definition;
336
- }
337
- /** Replace the mutation handler (e.g. after ApplyPipeline is wired in createApp). */
338
- setMutationHandler(handler) {
339
- this.mutationHandler = handler;
340
- }
341
- };
342
-
343
- // src/query/query-builder.ts
344
- import { quoteIdent as quoteIdent3 } from "@korajs/core";
345
-
346
- // src/query/pluralize.ts
347
- function isVowel(char) {
348
- if (!char) return false;
349
- return "aeiouAEIOU".includes(char);
350
- }
351
- function pluralize(word) {
352
- if (word.endsWith("s")) return word;
353
- if (word.endsWith("y") && !isVowel(word[word.length - 2])) {
354
- return `${word.slice(0, -1)}ies`;
355
- }
356
- if (word.endsWith("sh") || word.endsWith("ch") || word.endsWith("x") || word.endsWith("z")) {
357
- return `${word}es`;
358
- }
359
- return `${word}s`;
360
- }
361
- function singularize(word) {
362
- if (word.endsWith("ies") && !isVowel(word[word.length - 4])) {
363
- return `${word.slice(0, -3)}y`;
364
- }
365
- if (word.endsWith("shes") || word.endsWith("ches") || word.endsWith("xes") || word.endsWith("zes")) {
366
- return word.slice(0, -2);
367
- }
368
- if (word.endsWith("ses")) {
369
- return word.slice(0, -2);
370
- }
371
- if (word.endsWith("s") && !word.endsWith("ss")) {
372
- return word.slice(0, -1);
373
- }
374
- return word;
375
- }
376
-
377
- // src/query/query-builder.ts
378
- var QueryBuilder = class _QueryBuilder {
379
- constructor(collectionName, definition, adapter, subscriptionManager, initialWhere = {}, schema) {
380
- this.collectionName = collectionName;
381
- this.definition = definition;
382
- this.adapter = adapter;
383
- this.subscriptionManager = subscriptionManager;
384
- this.schema = schema;
385
- this.descriptor = {
386
- collection: collectionName,
387
- where: { ...initialWhere },
388
- orderBy: []
389
- };
390
- }
391
- collectionName;
392
- definition;
393
- adapter;
394
- subscriptionManager;
395
- schema;
396
- descriptor;
397
- /**
398
- * Add WHERE conditions (AND semantics, merged with existing conditions).
399
- */
400
- where(conditions) {
401
- const clone = this.clone();
402
- clone.descriptor = {
403
- ...clone.descriptor,
404
- where: { ...clone.descriptor.where, ...conditions }
405
- };
406
- return clone;
407
- }
408
- /**
409
- * Add ORDER BY clause.
410
- */
411
- orderBy(field, direction = "asc") {
412
- const clone = this.clone();
413
- clone.descriptor = {
414
- ...clone.descriptor,
415
- orderBy: [...clone.descriptor.orderBy, { field, direction }]
416
- };
417
- return clone;
418
- }
419
- /**
420
- * Set result limit.
421
- */
422
- limit(n) {
423
- const clone = this.clone();
424
- clone.descriptor = { ...clone.descriptor, limit: n };
425
- return clone;
426
- }
427
- /**
428
- * Set result offset.
429
- */
430
- offset(n) {
431
- const clone = this.clone();
432
- clone.descriptor = { ...clone.descriptor, offset: n };
433
- return clone;
434
- }
435
- /**
436
- * Include related records in the query results.
437
- * Follows relations defined in the schema to batch-fetch related data.
438
- *
439
- * @param targets - Relation target names (collection names or relation names)
440
- * @returns A new QueryBuilder with include targets added
441
- *
442
- * @example
443
- * ```typescript
444
- * const todosWithProject = await app.todos
445
- * .where({ completed: false })
446
- * .include('project')
447
- * .exec()
448
- * ```
449
- */
450
- include(...targets) {
451
- const clone = this.clone();
452
- const existing = clone.descriptor.include ?? [];
453
- clone.descriptor = {
454
- ...clone.descriptor,
455
- include: [...existing, ...targets]
456
- };
457
- return clone;
458
- }
459
- /**
460
- * Execute the query and return results.
461
- */
462
- async exec() {
463
- const { sql, params } = buildSelectQuery(this.descriptor, this.definition.fields);
464
- const rows = await this.adapter.query(sql, params);
465
- const records = rows.map((row) => deserializeRecord(row, this.definition.fields));
466
- if (this.descriptor.include && this.descriptor.include.length > 0 && this.schema) {
467
- await this.resolveIncludes(records);
468
- }
469
- return records;
470
- }
471
- /**
472
- * Execute a COUNT query and return the count.
473
- */
474
- async count() {
475
- const { sql, params } = buildCountQuery(this.descriptor, this.definition.fields);
476
- const rows = await this.adapter.query(sql, params);
477
- return rows[0]?.count ?? 0;
478
- }
479
- /**
480
- * Subscribe to query results. Callback is called immediately with current results,
481
- * then again whenever the results change due to mutations.
482
- *
483
- * @returns An unsubscribe function
484
- */
485
- subscribe(callback) {
486
- const executeFn = () => this.exec();
487
- const descriptorCopy = { ...this.descriptor };
488
- if (descriptorCopy.include && descriptorCopy.include.length > 0 && this.schema) {
489
- descriptorCopy.includeCollections = this.resolveIncludeCollections(descriptorCopy.include);
490
- }
491
- return this.subscriptionManager.registerAndFetch(
492
- descriptorCopy,
493
- callback,
494
- executeFn
495
- );
496
- }
497
- /** Get the internal descriptor (for testing/debugging) */
498
- getDescriptor() {
499
- return { ...this.descriptor };
500
- }
501
- clone() {
502
- const qb = new _QueryBuilder(
503
- this.collectionName,
504
- this.definition,
505
- this.adapter,
506
- this.subscriptionManager,
507
- {},
508
- this.schema
509
- );
510
- qb.descriptor = {
511
- ...this.descriptor,
512
- where: { ...this.descriptor.where },
513
- orderBy: [...this.descriptor.orderBy],
514
- include: this.descriptor.include ? [...this.descriptor.include] : void 0,
515
- includeCollections: this.descriptor.includeCollections ? [...this.descriptor.includeCollections] : void 0
516
- };
517
- return qb;
518
- }
519
- /**
520
- * Resolve include targets to their actual collection names for subscription tracking.
521
- */
522
- resolveIncludeCollections(targets) {
523
- if (!this.schema) return [];
524
- const collections = [];
525
- for (const target of targets) {
526
- const relation = this.findRelation(target);
527
- if (relation) {
528
- if (relation.from === this.collectionName) {
529
- collections.push(relation.to);
530
- } else {
531
- collections.push(relation.from);
532
- }
533
- }
534
- }
535
- return collections;
536
- }
537
- /**
538
- * Resolve includes after primary query, batch-fetching related records.
539
- */
540
- async resolveIncludes(records) {
541
- if (!this.schema || !this.descriptor.include || records.length === 0) return;
542
- for (const target of this.descriptor.include) {
543
- const relation = this.findRelation(target);
544
- if (!relation) {
545
- throw new QueryError2(
546
- `No relation found for include target "${target}" on collection "${this.collectionName}". Check that a relation is defined in your schema that connects "${this.collectionName}" to "${target}".`
547
- );
548
- }
549
- if (relation.from === this.collectionName) {
550
- await this.resolveManyToOneInclude(records, relation, target);
551
- } else {
552
- await this.resolveOneToManyInclude(records, relation, target);
553
- }
554
- }
555
- }
556
- /**
557
- * Many-to-one: collect FK values from primary results, batch-fetch related, attach as singular.
558
- */
559
- async resolveManyToOneInclude(records, relation, target) {
560
- const fkField = relation.field;
561
- const fkValues = records.map((r) => r[fkField]).filter((v) => v !== null && v !== void 0 && typeof v === "string");
562
- if (fkValues.length === 0) {
563
- const propName2 = singularize(target);
564
- for (const record of records) {
565
- ;
566
- record[propName2] = null;
567
- }
568
- return;
569
- }
570
- const uniqueFks = [...new Set(fkValues)];
571
- const relatedCollection = relation.to;
572
- const relatedDef = this.schema?.collections[relatedCollection];
573
- if (!relatedDef) return;
574
- const placeholders = uniqueFks.map(() => "?").join(", ");
575
- const sql = `SELECT * FROM ${quoteIdent3(relatedCollection)} WHERE id IN (${placeholders}) AND _deleted = 0`;
576
- const rows = await this.adapter.query(sql, uniqueFks);
577
- const relatedRecords = rows.map((row) => deserializeRecord(row, relatedDef.fields));
578
- const lookup = /* @__PURE__ */ new Map();
579
- for (const r of relatedRecords) {
580
- lookup.set(r.id, r);
581
- }
582
- const propName = singularize(target);
583
- for (const record of records) {
584
- const fk = record[fkField];
585
- record[propName] = fk ? lookup.get(fk) ?? null : null;
586
- }
587
- }
588
- /**
589
- * One-to-many: collect primary IDs, batch-fetch children, attach as array.
590
- */
591
- async resolveOneToManyInclude(records, relation, target) {
592
- const primaryIds = records.map((r) => r.id);
593
- const relatedCollection = relation.from;
594
- const relatedDef = this.schema?.collections[relatedCollection];
595
- if (!relatedDef) return;
596
- const fkField = relation.field;
597
- const placeholders = primaryIds.map(() => "?").join(", ");
598
- const sql = `SELECT * FROM ${quoteIdent3(relatedCollection)} WHERE ${quoteIdent3(fkField)} IN (${placeholders}) AND _deleted = 0`;
599
- const rows = await this.adapter.query(sql, primaryIds);
600
- const relatedRecords = rows.map((row) => deserializeRecord(row, relatedDef.fields));
601
- const grouped = /* @__PURE__ */ new Map();
602
- for (const r of relatedRecords) {
603
- const fk = r[fkField];
604
- if (!grouped.has(fk)) {
605
- grouped.set(fk, []);
606
- }
607
- grouped.get(fk)?.push(r);
608
- }
609
- const propName = pluralize(target);
610
- for (const record of records) {
611
- ;
612
- record[propName] = grouped.get(record.id) ?? [];
613
- }
614
- }
615
- /**
616
- * Find a relation definition matching the include target.
617
- * Searches by relation name, target collection name, and singularized/pluralized variants.
618
- */
619
- findRelation(target) {
620
- if (!this.schema) return null;
621
- for (const [_name, rel] of Object.entries(this.schema.relations)) {
622
- if (rel.from === this.collectionName && rel.to === target) return rel;
623
- if (rel.to === this.collectionName && rel.from === target) return rel;
624
- if (rel.from === this.collectionName && rel.to === pluralize(target)) return rel;
625
- if (rel.to === this.collectionName && rel.from === pluralize(target)) return rel;
626
- if (rel.from === this.collectionName && rel.to === singularize(target)) return rel;
627
- if (rel.to === this.collectionName && rel.from === singularize(target)) return rel;
628
- }
629
- return null;
630
- }
631
- };
632
- var QueryError2 = class extends Error {
633
- constructor(message) {
634
- super(message);
635
- this.name = "QueryError";
636
- }
637
- };
638
-
639
- // src/relations/relation-enforcer.ts
640
- import { KoraError, createOperation, quoteIdent as quoteIdent4 } from "@korajs/core";
641
-
642
- // src/relations/relation-lookup.ts
643
- function buildRelationLookup(schema) {
644
- const lookup = /* @__PURE__ */ new Map();
645
- for (const [relationName, relation] of Object.entries(schema.relations)) {
646
- const targetCollection = relation.to;
647
- const existing = lookup.get(targetCollection) ?? [];
648
- existing.push({
649
- relationName,
650
- sourceCollection: relation.from,
651
- foreignKeyField: relation.field,
652
- onDelete: relation.onDelete,
653
- relation
654
- });
655
- lookup.set(targetCollection, existing);
656
- }
657
- return lookup;
658
- }
659
- function getIncomingRelations(lookup, collection) {
660
- return lookup.get(collection) ?? [];
661
- }
662
-
663
- // src/relations/relation-enforcer.ts
664
- var ReferentialIntegrityError = class extends KoraError {
665
- constructor(collection, recordId, referencingCollection, relationName, referencingCount) {
666
- super(
667
- `Cannot delete record "${recordId}" from "${collection}": ${referencingCount} record(s) in "${referencingCollection}" reference it via relation "${relationName}" with onDelete: 'restrict'. Delete or reassign the referencing records first.`,
668
- "REFERENTIAL_INTEGRITY",
669
- {
670
- collection,
671
- recordId,
672
- referencingCollection,
673
- relationName,
674
- referencingCount
675
- }
676
- );
677
- this.name = "ReferentialIntegrityError";
678
- }
679
- };
680
- var RelationEnforcer = class {
681
- lookup;
682
- schema;
683
- adapter;
684
- clock;
685
- nodeId;
686
- constructor(config) {
687
- this.schema = config.schema;
688
- this.adapter = config.adapter;
689
- this.clock = config.clock;
690
- this.nodeId = config.nodeId;
691
- this.lookup = buildRelationLookup(config.schema);
692
- }
693
- /**
694
- * Enforce referential integrity after deleting a record.
695
- *
696
- * Must be called within a transaction. The transaction handle is used
697
- * for all cascaded writes to ensure atomicity.
698
- *
699
- * @param collection - The collection the deleted record belongs to
700
- * @param recordId - The ID of the deleted record
701
- * @param tx - The active transaction handle
702
- * @param causalDeps - Causal dependencies for generated operations
703
- * @returns All additional operations created as side effects
704
- * @throws {ReferentialIntegrityError} If a 'restrict' policy is violated
705
- */
706
- async enforceDelete(collection, recordId, tx, causalDeps) {
707
- const incomingRelations = getIncomingRelations(this.lookup, collection);
708
- if (incomingRelations.length === 0) {
709
- return { operations: [] };
710
- }
711
- const allOperations = [];
712
- const sortedRelations = [...incomingRelations].sort(
713
- (a, b) => a.relationName.localeCompare(b.relationName)
714
- );
715
- for (const incoming of sortedRelations) {
716
- const ops = await this.enforceRelation(incoming, recordId, tx, causalDeps);
717
- allOperations.push(...ops);
718
- }
719
- return { operations: allOperations };
720
- }
721
- /**
722
- * Enforce a single relation's onDelete policy.
723
- */
724
- async enforceRelation(incoming, deletedRecordId, tx, causalDeps) {
725
- switch (incoming.onDelete) {
726
- case "cascade":
727
- return this.enforceCascade(incoming, deletedRecordId, tx, causalDeps);
728
- case "set-null":
729
- return this.enforceSetNull(incoming, deletedRecordId, tx, causalDeps);
730
- case "restrict":
731
- return this.enforceRestrict(incoming, deletedRecordId, tx);
732
- case "no-action":
733
- return [];
734
- }
735
- }
736
- /**
737
- * Cascade: delete all records in the source collection that reference
738
- * the deleted record, then recursively cascade those deletes.
739
- */
740
- async enforceCascade(incoming, deletedRecordId, tx, causalDeps) {
741
- const { sourceCollection, foreignKeyField } = incoming;
742
- const referencingRows = await tx.query(
743
- `SELECT id FROM ${quoteIdent4(sourceCollection)} WHERE ${quoteIdent4(foreignKeyField)} = ? AND _deleted = 0`,
744
- [deletedRecordId]
745
- );
746
- if (referencingRows.length === 0) {
747
- return [];
748
- }
749
- const operations = [];
750
- for (const row of referencingRows) {
751
- const now = Date.now();
752
- const sequenceNumber = await allocateNextSequenceInTransaction(tx, this.nodeId);
753
- const operation = await createOperation(
754
- {
755
- nodeId: this.nodeId,
756
- type: "delete",
757
- collection: sourceCollection,
758
- recordId: row.id,
759
- data: null,
760
- previousData: null,
761
- sequenceNumber,
762
- causalDeps: [...causalDeps],
763
- schemaVersion: this.schema.version
764
- },
765
- this.clock
766
- );
767
- const deleteQuery = buildSoftDeleteQuery(sourceCollection, row.id, now);
768
- await tx.execute(deleteQuery.sql, deleteQuery.params);
769
- const opInsert = buildInsertQuery(
770
- `_kora_ops_${sourceCollection}`,
771
- serializeOperation(operation)
772
- );
773
- await tx.execute(opInsert.sql, opInsert.params);
774
- operations.push(operation);
775
- const cascadeResult = await this.enforceDelete(sourceCollection, row.id, tx, [operation.id]);
776
- operations.push(...cascadeResult.operations);
777
- }
778
- return operations;
779
- }
780
- /**
781
- * Set-null: update all referencing records to set the foreign key to null.
782
- */
783
- async enforceSetNull(incoming, deletedRecordId, tx, causalDeps) {
784
- const { sourceCollection, foreignKeyField } = incoming;
785
- const collectionDef = this.schema.collections[sourceCollection];
786
- if (!collectionDef) {
787
- return [];
788
- }
789
- const referencingRows = await tx.query(
790
- `SELECT id FROM ${quoteIdent4(sourceCollection)} WHERE ${quoteIdent4(foreignKeyField)} = ? AND _deleted = 0`,
791
- [deletedRecordId]
792
- );
793
- if (referencingRows.length === 0) {
794
- return [];
795
- }
796
- const operations = [];
797
- for (const row of referencingRows) {
798
- const now = Date.now();
799
- const sequenceNumber = await allocateNextSequenceInTransaction(tx, this.nodeId);
800
- const updateData = { [foreignKeyField]: null };
801
- const previousData = { [foreignKeyField]: deletedRecordId };
802
- const operation = await createOperation(
803
- {
804
- nodeId: this.nodeId,
805
- type: "update",
806
- collection: sourceCollection,
807
- recordId: row.id,
808
- data: { ...updateData },
809
- previousData,
810
- sequenceNumber,
811
- causalDeps: [...causalDeps],
812
- schemaVersion: this.schema.version
813
- },
814
- this.clock
815
- );
816
- const serializedChanges = serializeRecord(updateData, collectionDef.fields);
817
- const updateQuery = buildUpdateQuery(sourceCollection, row.id, {
818
- ...serializedChanges,
819
- _updated_at: now
820
- });
821
- await tx.execute(updateQuery.sql, updateQuery.params);
822
- const opRow = serializeOperation(operation);
823
- const opInsert = buildInsertQuery(
824
- `_kora_ops_${sourceCollection}`,
825
- opRow
826
- );
827
- await tx.execute(opInsert.sql, opInsert.params);
828
- operations.push(operation);
829
- }
830
- return operations;
831
- }
832
- /**
833
- * Restrict: refuse the delete if any referencing records exist.
834
- */
835
- async enforceRestrict(incoming, deletedRecordId, tx) {
836
- const { sourceCollection, foreignKeyField, relationName } = incoming;
837
- const countRows = await tx.query(
838
- `SELECT COUNT(*) as cnt FROM ${quoteIdent4(sourceCollection)} WHERE ${quoteIdent4(foreignKeyField)} = ? AND _deleted = 0`,
839
- [deletedRecordId]
840
- );
841
- const count = countRows[0]?.cnt ?? 0;
842
- if (count > 0) {
843
- const targetCollection = incoming.relation.to;
844
- throw new ReferentialIntegrityError(
845
- targetCollection,
846
- deletedRecordId,
847
- sourceCollection,
848
- relationName,
849
- count
850
- );
851
- }
852
- return [];
853
- }
854
- /**
855
- * Get the relation lookup map for external use (e.g., by the merge engine).
856
- */
857
- getRelationLookup() {
858
- return this.lookup;
859
- }
860
- };
861
-
862
- // src/replay/replay-to.ts
863
- import { OperationError } from "@korajs/core";
864
- import { topologicalSort } from "@korajs/core/internal";
865
- function collectCausalClosure(allOps, targetOperationId) {
866
- const opMap = /* @__PURE__ */ new Map();
867
- for (const op of allOps) {
868
- opMap.set(op.id, op);
869
- }
870
- const target = opMap.get(targetOperationId);
871
- if (!target) {
872
- throw new OperationError(
873
- `Operation "${targetOperationId}" not found in the local operation log`,
874
- {
875
- operationId: targetOperationId
876
- }
877
- );
878
- }
879
- const included = /* @__PURE__ */ new Set();
880
- const stack = [targetOperationId];
881
- while (stack.length > 0) {
882
- const id = stack.pop();
883
- if (id === void 0 || included.has(id)) {
884
- continue;
885
- }
886
- included.add(id);
887
- const op = opMap.get(id);
888
- if (!op) {
889
- continue;
890
- }
891
- for (const depId of op.causalDeps) {
892
- if (opMap.has(depId)) {
893
- stack.push(depId);
894
- }
895
- }
896
- }
897
- const subset = allOps.filter((op) => included.has(op.id));
898
- return topologicalSort(subset);
899
- }
900
- function buildReplaySnapshot(schema, allOps, targetOperationId) {
901
- const operationsApplied = collectCausalClosure(allOps, targetOperationId);
902
- const targetOperation = operationsApplied.find((op) => op.id === targetOperationId);
903
- if (!targetOperation) {
904
- throw new OperationError(`Operation "${targetOperationId}" not found after causal sort`, {
905
- operationId: targetOperationId
906
- });
907
- }
908
- const memory = /* @__PURE__ */ new Map();
909
- for (const op of operationsApplied) {
910
- applyOperationToMemory(memory, op, schema);
911
- }
912
- const collections = materializeCollections(schema, memory);
913
- return {
914
- targetOperation,
915
- operationsApplied,
916
- collections,
917
- findRecord(collection, recordId) {
918
- const colMap = memory.get(collection);
919
- const record = colMap?.get(recordId);
920
- if (!record || record.deleted) {
921
- return null;
922
- }
923
- const definition = schema.collections[collection];
924
- if (!definition) {
925
- return null;
926
- }
927
- return toCollectionRecord(record, definition);
928
- }
929
- };
930
- }
931
- function applyOperationToMemory(state, op, schema) {
932
- const definition = schema.collections[op.collection];
933
- if (!definition) {
934
- return;
935
- }
936
- let colMap = state.get(op.collection);
937
- if (!colMap) {
938
- colMap = /* @__PURE__ */ new Map();
939
- state.set(op.collection, colMap);
940
- }
941
- const wallTime = op.timestamp.wallTime;
942
- switch (op.type) {
943
- case "insert": {
944
- if (!op.data) {
945
- return;
946
- }
947
- colMap.set(op.recordId, {
948
- id: op.recordId,
949
- // op.data stores binary richtext as tagged JSON; snapshots must
950
- // expose record-shaped values (Uint8Array/string).
951
- fields: decodeRichtextFieldsFromOpData(op.data, definition.fields),
952
- deleted: false,
953
- createdAt: wallTime,
954
- updatedAt: wallTime
955
- });
956
- break;
957
- }
958
- case "update": {
959
- if (!op.data) {
960
- return;
961
- }
962
- const decodedData = decodeRichtextFieldsFromOpData(op.data, definition.fields);
963
- const existing = colMap.get(op.recordId);
964
- if (existing && !existing.deleted) {
965
- existing.fields = { ...existing.fields, ...decodedData };
966
- existing.updatedAt = wallTime;
967
- break;
968
- }
969
- if (existing?.deleted) {
970
- return;
971
- }
972
- colMap.set(op.recordId, {
973
- id: op.recordId,
974
- fields: decodedData,
975
- deleted: false,
976
- createdAt: wallTime,
977
- updatedAt: wallTime
978
- });
979
- break;
980
- }
981
- case "delete": {
982
- const existing = colMap.get(op.recordId);
983
- if (existing) {
984
- existing.deleted = true;
985
- existing.updatedAt = wallTime;
986
- return;
987
- }
988
- colMap.set(op.recordId, {
989
- id: op.recordId,
990
- fields: {},
991
- deleted: true,
992
- createdAt: wallTime,
993
- updatedAt: wallTime
994
- });
995
- break;
996
- }
997
- }
998
- }
999
- function materializeCollections(schema, memory) {
1000
- const collections = {};
1001
- for (const [collectionName, definition] of Object.entries(schema.collections)) {
1002
- const colMap = memory.get(collectionName);
1003
- const records = [];
1004
- if (colMap) {
1005
- for (const record of colMap.values()) {
1006
- if (!record.deleted) {
1007
- records.push(toCollectionRecord(record, definition));
1008
- }
1009
- }
1010
- }
1011
- collections[collectionName] = records;
1012
- }
1013
- return collections;
1014
- }
1015
- function toCollectionRecord(record, definition) {
1016
- const result = {
1017
- id: record.id,
1018
- createdAt: record.createdAt,
1019
- updatedAt: record.updatedAt
1020
- };
1021
- for (const [fieldName] of Object.entries(definition.fields)) {
1022
- if (fieldName in record.fields) {
1023
- result[fieldName] = record.fields[fieldName];
1024
- }
1025
- }
1026
- return result;
1027
- }
1028
-
1029
- // src/sequences/sequence-manager.ts
1030
- import { defaultSequenceFormat, formatSequenceValue } from "@korajs/core";
1031
- var SequenceManager = class {
1032
- adapter;
1033
- nodeId;
1034
- constructor(adapter, nodeId) {
1035
- this.adapter = adapter;
1036
- this.nodeId = nodeId;
1037
- }
1038
- /**
1039
- * Get the next value in a sequence, atomically incrementing the counter.
1040
- *
1041
- * @param name - The sequence name (e.g., 'receipt', 'invoice')
1042
- * @param config - Optional configuration for scope, format, and starting value
1043
- * @returns The formatted sequence value
1044
- */
1045
- async next(name, config) {
1046
- const scope = config?.scope ?? "";
1047
- const startAt = config?.startAt ?? 1;
1048
- const format = config?.format ?? defaultSequenceFormat(name);
1049
- let counter = 0;
1050
- await this.adapter.transaction(async (tx) => {
1051
- const rows = await tx.query(
1052
- "SELECT counter FROM _kora_sequences WHERE name = ? AND scope = ? AND node_id = ?",
1053
- [name, scope, this.nodeId]
1054
- );
1055
- if (rows.length > 0) {
1056
- const row = rows[0];
1057
- counter = row.counter + 1;
1058
- await tx.execute(
1059
- "UPDATE _kora_sequences SET counter = ? WHERE name = ? AND scope = ? AND node_id = ?",
1060
- [counter, name, scope, this.nodeId]
1061
- );
1062
- } else {
1063
- counter = startAt;
1064
- await tx.execute(
1065
- "INSERT INTO _kora_sequences (name, scope, node_id, counter) VALUES (?, ?, ?, ?)",
1066
- [name, scope, this.nodeId, counter]
1067
- );
1068
- }
1069
- });
1070
- return formatSequenceValue(format, counter, this.nodeId);
1071
- }
1072
- /**
1073
- * Get the current counter value without incrementing.
1074
- *
1075
- * @param name - The sequence name
1076
- * @param config - Optional scope
1077
- * @returns The current counter value, or 0 if the sequence has never been used
1078
- */
1079
- async current(name, config) {
1080
- const scope = config?.scope ?? "";
1081
- const rows = await this.adapter.query(
1082
- "SELECT counter FROM _kora_sequences WHERE name = ? AND scope = ? AND node_id = ?",
1083
- [name, scope, this.nodeId]
1084
- );
1085
- if (rows.length > 0) {
1086
- return rows[0].counter;
1087
- }
1088
- return 0;
1089
- }
1090
- /**
1091
- * Reset a sequence counter.
1092
- *
1093
- * @param name - The sequence name
1094
- * @param config - Optional scope and target value
1095
- */
1096
- async reset(name, config) {
1097
- const scope = config?.scope ?? "";
1098
- const to = config?.to ?? 0;
1099
- await this.adapter.execute(
1100
- "DELETE FROM _kora_sequences WHERE name = ? AND scope = ? AND node_id = ?",
1101
- [name, scope, this.nodeId]
1102
- );
1103
- if (to > 0) {
1104
- await this.adapter.execute(
1105
- "INSERT INTO _kora_sequences (name, scope, node_id, counter) VALUES (?, ?, ?, ?)",
1106
- [name, scope, this.nodeId, to]
1107
- );
1108
- }
1109
- }
1110
- };
1111
-
1112
- // src/subscription/bloom-filter.ts
1113
- var FNV_OFFSET_BASIS = 2166136261;
1114
- var FNV_PRIME = 16777619;
1115
- function fnv1a32(input) {
1116
- let hash = FNV_OFFSET_BASIS;
1117
- for (let i = 0; i < input.length; i++) {
1118
- hash ^= input.charCodeAt(i);
1119
- hash = Math.imul(hash, FNV_PRIME);
1120
- }
1121
- return hash >>> 0;
1122
- }
1123
- function optimalBitCount(expectedItems, falsePositiveRate) {
1124
- if (expectedItems <= 0) return 32;
1125
- if (falsePositiveRate <= 0 || falsePositiveRate >= 1) return 32;
1126
- const ln2Squared = Math.LN2 * Math.LN2;
1127
- const rawBits = -(expectedItems * Math.log(falsePositiveRate)) / ln2Squared;
1128
- const aligned = Math.ceil(rawBits / 32) * 32;
1129
- return Math.max(32, aligned);
1130
- }
1131
- function optimalHashCount(bitCount, expectedItems) {
1132
- if (expectedItems <= 0) return 1;
1133
- const raw = bitCount / expectedItems * Math.LN2;
1134
- return Math.max(1, Math.min(30, Math.round(raw)));
1135
- }
1136
- var SubscriptionBloomFilter = class {
1137
- bits;
1138
- bitCount;
1139
- hashCount;
1140
- itemCount = 0;
1141
- constructor(expectedItems, falsePositiveRate = 0.01) {
1142
- this.bitCount = optimalBitCount(expectedItems, falsePositiveRate);
1143
- this.hashCount = optimalHashCount(this.bitCount, expectedItems);
1144
- this.bits = new Uint32Array(this.bitCount / 32);
1145
- }
1146
- /**
1147
- * Add a collection (and optional field) to the bloom filter.
1148
- *
1149
- * @param collection - Collection name (e.g., "todos")
1150
- * @param field - Optional field name (e.g., "completed")
1151
- */
1152
- add(collection, field) {
1153
- const key = field !== void 0 ? `${collection}:${field}` : collection;
1154
- const positions = this.getPositions(key);
1155
- for (const pos of positions) {
1156
- const wordIndex = pos >>> 5;
1157
- const bitIndex = pos & 31;
1158
- const current = this.bits[wordIndex] ?? 0;
1159
- this.bits[wordIndex] = current | 1 << bitIndex;
1160
- }
1161
- this.itemCount++;
1162
- }
1163
- /**
1164
- * Check if a collection (and optional field) might be in the filter.
1165
- *
1166
- * A return value of `false` means the item is DEFINITELY NOT in the filter.
1167
- * A return value of `true` means the item MIGHT be in the filter (possible false positive).
1168
- *
1169
- * @param collection - Collection name to check
1170
- * @param field - Optional field name to check
1171
- * @returns false = definitely absent, true = possibly present
1172
- */
1173
- mightContain(collection, field) {
1174
- const key = field !== void 0 ? `${collection}:${field}` : collection;
1175
- const positions = this.getPositions(key);
1176
- for (const pos of positions) {
1177
- const wordIndex = pos >>> 5;
1178
- const bitIndex = pos & 31;
1179
- if (((this.bits[wordIndex] ?? 0) & 1 << bitIndex) === 0) {
1180
- return false;
1181
- }
1182
- }
1183
- return true;
1184
- }
1185
- /**
1186
- * Reset the bloom filter, clearing all bits and the item count.
1187
- */
1188
- clear() {
1189
- this.bits.fill(0);
1190
- this.itemCount = 0;
1191
- }
1192
- /**
1193
- * Estimate the current false positive rate based on the number of items inserted.
1194
- *
1195
- * Formula: (1 - e^(-kn/m))^k
1196
- * where k = hash count, n = item count, m = bit count.
1197
- *
1198
- * @returns Estimated false positive rate as a number between 0 and 1
1199
- */
1200
- estimatedFalsePositiveRate() {
1201
- if (this.itemCount === 0) return 0;
1202
- const exponent = -(this.hashCount * this.itemCount) / this.bitCount;
1203
- return (1 - Math.exp(exponent)) ** this.hashCount;
1204
- }
1205
- /**
1206
- * @returns The number of items that have been added to the filter
1207
- */
1208
- getItemCount() {
1209
- return this.itemCount;
1210
- }
1211
- /**
1212
- * @returns The total number of bits in the filter
1213
- */
1214
- getBitCount() {
1215
- return this.bitCount;
1216
- }
1217
- /**
1218
- * @returns The number of hash functions used
1219
- */
1220
- getHashCount() {
1221
- return this.hashCount;
1222
- }
1223
- /**
1224
- * Count the number of bits currently set to 1.
1225
- * Uses Brian Kernighan's algorithm: each iteration clears the lowest set bit,
1226
- * so the loop runs exactly as many times as there are set bits.
1227
- *
1228
- * @returns Number of bits set to 1
1229
- */
1230
- getSetBitCount() {
1231
- let count = 0;
1232
- for (let i = 0; i < this.bits.length; i++) {
1233
- let word = this.bits[i] ?? 0;
1234
- while (word !== 0) {
1235
- word &= word - 1;
1236
- count++;
1237
- }
1238
- }
1239
- return count;
1240
- }
1241
- /**
1242
- * Compute k bit positions for a given key using Kirsch-Mitzenmacker double hashing.
1243
- *
1244
- * Instead of computing k independent hash functions, we compute two base hashes
1245
- * (h1 and h2) and derive the rest as: h_i = (h1 + i * h2) mod m.
1246
- * This technique is proven to have the same asymptotic false positive rate as
1247
- * k independent hash functions.
1248
- *
1249
- * We derive h1 and h2 from a single FNV-1a hash by splitting and mixing:
1250
- * h1 = fnv1a(key), h2 = fnv1a(key + "\0salt") to ensure independence.
1251
- */
1252
- getPositions(key) {
1253
- const h1 = fnv1a32(key);
1254
- const h2 = fnv1a32(`${key}\0bloom`);
1255
- const positions = new Array(this.hashCount);
1256
- for (let i = 0; i < this.hashCount; i++) {
1257
- positions[i] = (h1 + Math.imul(i, h2) >>> 0) % this.bitCount;
1258
- }
1259
- return positions;
1260
- }
1261
- };
1262
-
1263
- // src/subscription/subscription-manager.ts
1264
- var nextSubId = 0;
1265
- var DEFAULT_BLOOM_THRESHOLD = 100;
1266
- var DEFAULT_BLOOM_EXPECTED_ITEMS = 500;
1267
- var DEFAULT_BLOOM_FALSE_POSITIVE_RATE = 0.01;
1268
- var SubscriptionManager = class {
1269
- subscriptions = /* @__PURE__ */ new Map();
1270
- pendingCollections = /* @__PURE__ */ new Set();
1271
- flushScheduled = false;
1272
- onQuerySubscribed;
1273
- // Bloom filter state
1274
- bloomFilter = null;
1275
- bloomDirty = false;
1276
- bloomThreshold;
1277
- bloomExpectedItems;
1278
- bloomFalsePositiveRate;
1279
- // Performance stats
1280
- totalChecks = 0;
1281
- bloomFilterHits = 0;
1282
- bloomFilterMisses = 0;
1283
- falsePositives = 0;
1284
- totalCheckTimeMs = 0;
1285
- constructor(options) {
1286
- this.bloomThreshold = options?.bloomThreshold ?? DEFAULT_BLOOM_THRESHOLD;
1287
- this.bloomExpectedItems = options?.bloomExpectedItems ?? DEFAULT_BLOOM_EXPECTED_ITEMS;
1288
- this.bloomFalsePositiveRate = options?.bloomFalsePositiveRate ?? DEFAULT_BLOOM_FALSE_POSITIVE_RATE;
1289
- this.onQuerySubscribed = options?.onQuerySubscribed;
1290
- }
1291
- /**
1292
- * Register a new subscription.
1293
- *
1294
- * @param descriptor - The query descriptor defining what this subscription watches
1295
- * @param callback - Called with results whenever they change
1296
- * @param executeFn - Function to re-execute the query and get current results
1297
- * @returns An unsubscribe function
1298
- */
1299
- register(descriptor, callback, executeFn) {
1300
- const id = `sub_${++nextSubId}`;
1301
- const subscription = {
1302
- id,
1303
- descriptor,
1304
- callback,
1305
- executeFn,
1306
- lastResults: []
1307
- };
1308
- this.subscriptions.set(id, subscription);
1309
- this.bloomDirty = true;
1310
- const externalCleanup = this.onQuerySubscribed?.(descriptor);
1311
- return () => {
1312
- this.subscriptions.delete(id);
1313
- this.bloomDirty = true;
1314
- externalCleanup?.();
1315
- };
1316
- }
1317
- /**
1318
- * Register a subscription and immediately execute the query.
1319
- * The initial results are stored as lastResults so subsequent flushes
1320
- * correctly diff against the initial state.
1321
- *
1322
- * @returns An unsubscribe function
1323
- */
1324
- registerAndFetch(descriptor, callback, executeFn) {
1325
- const id = `sub_${++nextSubId}`;
1326
- const subscription = {
1327
- id,
1328
- descriptor,
1329
- callback,
1330
- executeFn,
1331
- lastResults: []
1332
- };
1333
- this.subscriptions.set(id, subscription);
1334
- this.bloomDirty = true;
1335
- const externalCleanup = this.onQuerySubscribed?.(descriptor);
1336
- executeFn().then((results) => {
1337
- if (this.subscriptions.has(id)) {
1338
- subscription.lastResults = results;
1339
- callback(results);
1340
- }
1341
- });
1342
- return () => {
1343
- this.subscriptions.delete(id);
1344
- this.bloomDirty = true;
1345
- externalCleanup?.();
1346
- };
1347
- }
1348
- /**
1349
- * Notify the manager that a mutation occurred on a collection.
1350
- * Schedules a microtask flush to batch multiple mutations in the same tick.
1351
- */
1352
- notify(collection, _operation) {
1353
- this.pendingCollections.add(collection);
1354
- this.scheduleFlush();
1355
- }
1356
- /**
1357
- * Immediately flush all pending notifications.
1358
- * Useful for testing. In production, flushing happens via microtask.
1359
- */
1360
- async flush() {
1361
- if (this.pendingCollections.size === 0) return;
1362
- const collections = new Set(this.pendingCollections);
1363
- this.pendingCollections.clear();
1364
- this.flushScheduled = false;
1365
- const affected = this.findAffectedSubscriptions(collections);
1366
- for (const sub of affected) {
1367
- try {
1368
- const newResults = await sub.executeFn();
1369
- if (!this.resultsEqual(sub.lastResults, newResults)) {
1370
- sub.lastResults = newResults;
1371
- sub.callback(newResults);
1372
- }
1373
- } catch {
1374
- }
1375
- }
1376
- }
1377
- /**
1378
- * Remove all subscriptions. Called on store close.
1379
- */
1380
- clear() {
1381
- this.subscriptions.clear();
1382
- this.pendingCollections.clear();
1383
- this.flushScheduled = false;
1384
- this.bloomFilter = null;
1385
- this.bloomDirty = false;
1386
- this.totalChecks = 0;
1387
- this.bloomFilterHits = 0;
1388
- this.bloomFilterMisses = 0;
1389
- this.falsePositives = 0;
1390
- this.totalCheckTimeMs = 0;
1391
- }
1392
- /** Number of active subscriptions (for testing/debugging) */
1393
- get size() {
1394
- return this.subscriptions.size;
1395
- }
1396
- /**
1397
- * Get performance statistics for monitoring subscription checking efficiency.
1398
- * Useful for DevTools integration and performance tuning.
1399
- */
1400
- getStats() {
1401
- return {
1402
- totalChecks: this.totalChecks,
1403
- bloomFilterHits: this.bloomFilterHits,
1404
- bloomFilterMisses: this.bloomFilterMisses,
1405
- falsePositives: this.falsePositives,
1406
- averageCheckTimeMs: this.totalChecks > 0 ? this.totalCheckTimeMs / this.totalChecks : 0,
1407
- bloomFilterActive: this.isBloomActive(),
1408
- subscriptionCount: this.subscriptions.size
1409
- };
1410
- }
1411
- /**
1412
- * Check if bloom filter is currently active.
1413
- * Active when subscription count meets or exceeds the threshold.
1414
- */
1415
- isBloomActive() {
1416
- return this.subscriptions.size >= this.bloomThreshold;
1417
- }
1418
- /**
1419
- * Find subscriptions affected by mutations to the given collections.
1420
- * Uses two-level checking when bloom filter is active:
1421
- *
1422
- * Level 1: Bloom filter pre-check -- if no subscription depends on any
1423
- * of the mutated collections, skip everything (O(k) per collection).
1424
- *
1425
- * Level 2: Precise check -- linear scan of subscriptions, matching
1426
- * against the mutated collections.
1427
- */
1428
- findAffectedSubscriptions(collections) {
1429
- const startTime = performance.now();
1430
- this.totalChecks++;
1431
- const useBloom = this.isBloomActive();
1432
- if (useBloom) {
1433
- if (this.bloomDirty || this.bloomFilter === null) {
1434
- this.rebuildBloomFilter();
1435
- }
1436
- const filter = this.bloomFilter;
1437
- if (filter !== null) {
1438
- let anyPossibleMatch = false;
1439
- for (const col of collections) {
1440
- if (filter.mightContain(col)) {
1441
- anyPossibleMatch = true;
1442
- break;
1443
- }
1444
- }
1445
- if (!anyPossibleMatch) {
1446
- this.bloomFilterMisses++;
1447
- this.totalCheckTimeMs += performance.now() - startTime;
1448
- return [];
1449
- }
1450
- this.bloomFilterHits++;
1451
- }
1452
- }
1453
- const affected = [];
1454
- let anyPreciseMatch = false;
1455
- for (const sub of this.subscriptions.values()) {
1456
- if (collections.has(sub.descriptor.collection)) {
1457
- affected.push(sub);
1458
- anyPreciseMatch = true;
1459
- } else if (sub.descriptor.includeCollections) {
1460
- for (const incCol of sub.descriptor.includeCollections) {
1461
- if (collections.has(incCol)) {
1462
- affected.push(sub);
1463
- anyPreciseMatch = true;
1464
- break;
1465
- }
1466
- }
1467
- }
1468
- }
1469
- if (useBloom && !anyPreciseMatch) {
1470
- this.falsePositives++;
1471
- }
1472
- this.totalCheckTimeMs += performance.now() - startTime;
1473
- return affected;
1474
- }
1475
- /**
1476
- * Rebuild the bloom filter from all current subscriptions.
1477
- * Adds collection-level dependencies for every subscription, plus
1478
- * any included collection dependencies.
1479
- */
1480
- rebuildBloomFilter() {
1481
- const filter = new SubscriptionBloomFilter(this.bloomExpectedItems, this.bloomFalsePositiveRate);
1482
- for (const sub of this.subscriptions.values()) {
1483
- filter.add(sub.descriptor.collection);
1484
- if (sub.descriptor.includeCollections) {
1485
- for (const incCol of sub.descriptor.includeCollections) {
1486
- filter.add(incCol);
1487
- }
1488
- }
1489
- }
1490
- this.bloomFilter = filter;
1491
- this.bloomDirty = false;
1492
- }
1493
- scheduleFlush() {
1494
- if (this.flushScheduled) return;
1495
- this.flushScheduled = true;
1496
- queueMicrotask(() => {
1497
- this.flush();
1498
- });
1499
- }
1500
- /**
1501
- * Compare two result sets. Uses ID-based comparison first (same length, same IDs),
1502
- * then deep comparison of remaining fields when IDs match.
1503
- * Avoids JSON.stringify which is O(n) and fragile to field ordering differences.
1504
- */
1505
- resultsEqual(prev, next) {
1506
- if (prev.length !== next.length) return false;
1507
- if (prev.length === 0) return true;
1508
- for (let i = 0; i < prev.length; i++) {
1509
- if (prev[i]?.id !== next[i]?.id) return false;
1510
- }
1511
- for (let i = 0; i < prev.length; i++) {
1512
- const a = prev[i];
1513
- const b = next[i];
1514
- const keysA = Object.keys(a);
1515
- const keysB = Object.keys(b);
1516
- if (keysA.length !== keysB.length) return false;
1517
- for (const key of keysA) {
1518
- if (a[key] !== b[key]) return false;
1519
- }
1520
- }
1521
- return true;
1522
- }
1523
- };
1524
-
1525
- // src/sync/rebase-unsynced-operations.ts
1526
- import { HybridLogicalClock as HybridLogicalClock2, MAX_LOGICAL, quoteIdent as quoteIdent5 } from "@korajs/core";
1527
- import { computeOperationId } from "@korajs/core/internal";
1528
- var LOGICAL_COUNTER_LIMIT = MAX_LOGICAL + 1;
1529
- function emptyResult() {
1530
- return { operations: [], idMapping: {}, rebasedCount: 0, newMaxTimestamp: null };
1531
- }
1532
- async function rebaseUnsyncedOperationsInLog(adapter, schema, unsyncedOpIds, correctedNowMs) {
1533
- if (unsyncedOpIds.length === 0) {
1534
- return emptyResult();
1535
- }
1536
- const rebaseIds = new Set(unsyncedOpIds);
1537
- const rebased = [];
1538
- let maxOtherSerializedTs = null;
1539
- for (const collectionName of Object.keys(schema.collections)) {
1540
- const rows = await adapter.query(
1541
- `SELECT * FROM ${quoteIdent5(`_kora_ops_${collectionName}`)}`
1542
- );
1543
- for (const row of rows) {
1544
- if (rebaseIds.has(row.id)) {
1545
- rebased.push(deserializeOperationWithCollection(row, collectionName));
1546
- } else if (maxOtherSerializedTs === null || row.timestamp > maxOtherSerializedTs) {
1547
- maxOtherSerializedTs = row.timestamp;
1548
- }
1549
- }
1550
- }
1551
- if (rebased.length === 0) {
1552
- return emptyResult();
1553
- }
1554
- const maxOtherWallTime = maxOtherSerializedTs === null ? Number.NEGATIVE_INFINITY : HybridLogicalClock2.deserialize(maxOtherSerializedTs).wallTime;
1555
- const baseWallTime = Math.max(correctedNowMs, maxOtherWallTime + 1);
1556
- rebased.sort((a, b) => HybridLogicalClock2.compare(a.timestamp, b.timestamp));
1557
- const newTimestamps = rebased.map((op, i) => ({
1558
- // Spill into wallTime past the 5-digit logical serialization limit so the
1559
- // serialized order stays correct even for enormous offline queues.
1560
- wallTime: baseWallTime + Math.floor(i / LOGICAL_COUNTER_LIMIT),
1561
- logical: i % LOGICAL_COUNTER_LIMIT,
1562
- nodeId: op.nodeId
1563
- }));
1564
- const idMapping = {};
1565
- const newIds = [];
1566
- for (let i = 0; i < rebased.length; i++) {
1567
- const op = rebased[i];
1568
- const ts = newTimestamps[i];
1569
- if (!op || !ts) {
1570
- continue;
1571
- }
1572
- const input = {
1573
- nodeId: op.nodeId,
1574
- type: op.type,
1575
- collection: op.collection,
1576
- recordId: op.recordId,
1577
- data: op.data,
1578
- previousData: op.previousData,
1579
- sequenceNumber: op.sequenceNumber,
1580
- causalDeps: op.causalDeps,
1581
- schemaVersion: op.schemaVersion,
1582
- ...op.atomicOps !== void 0 ? { atomicOps: op.atomicOps } : {}
1583
- };
1584
- const newId = await computeOperationId(input, HybridLogicalClock2.serialize(ts));
1585
- idMapping[op.id] = newId;
1586
- newIds.push(newId);
1587
- }
1588
- const newOperations = [];
1589
- for (let i = 0; i < rebased.length; i++) {
1590
- const op = rebased[i];
1591
- const ts = newTimestamps[i];
1592
- const newId = newIds[i];
1593
- if (!op || !ts || newId === void 0) {
1594
- continue;
1595
- }
1596
- newOperations.push({
1597
- ...op,
1598
- id: newId,
1599
- timestamp: ts,
1600
- causalDeps: op.causalDeps.map((dep) => idMapping[dep] ?? dep)
1601
- });
1602
- }
1603
- await adapter.transaction(async (tx) => {
1604
- for (let i = 0; i < rebased.length; i++) {
1605
- const oldOp = rebased[i];
1606
- const newOp = newOperations[i];
1607
- if (!oldOp || !newOp) {
1608
- continue;
1609
- }
1610
- const opsTable = `_kora_ops_${oldOp.collection}`;
1611
- await tx.execute(`DELETE FROM ${quoteIdent5(opsTable)} WHERE id = ?`, [oldOp.id]);
1612
- const opInsert = buildInsertQuery(
1613
- opsTable,
1614
- serializeOperation(newOp)
1615
- );
1616
- await tx.execute(opInsert.sql, opInsert.params);
1617
- const oldVersion = serializeRowVersion(oldOp.timestamp);
1618
- const newVersion = serializeRowVersion(newOp.timestamp);
1619
- await tx.execute(
1620
- `UPDATE ${quoteIdent5(oldOp.collection)} SET _version = ?, _updated_at = ? WHERE id = ? AND _version = ?`,
1621
- [newVersion, newOp.timestamp.wallTime, oldOp.recordId, oldVersion]
1622
- );
1623
- const rows = await tx.query(
1624
- `SELECT _field_versions FROM ${quoteIdent5(oldOp.collection)} WHERE id = ?`,
1625
- [oldOp.recordId]
1626
- );
1627
- const fieldVersions = parseFieldVersions(rows[0]?._field_versions);
1628
- let changed = false;
1629
- for (const [field, version] of Object.entries(fieldVersions)) {
1630
- if (version === oldVersion) {
1631
- fieldVersions[field] = newVersion;
1632
- changed = true;
1633
- }
1634
- }
1635
- if (changed) {
1636
- await tx.execute(
1637
- `UPDATE ${quoteIdent5(oldOp.collection)} SET _field_versions = ? WHERE id = ?`,
1638
- [serializeFieldVersions(fieldVersions), oldOp.recordId]
1639
- );
1640
- }
1641
- if (oldOp.type === "insert") {
1642
- await tx.execute(
1643
- `UPDATE ${quoteIdent5(oldOp.collection)} SET _created_at = ? WHERE id = ? AND _created_at = ?`,
1644
- [newOp.timestamp.wallTime, oldOp.recordId, oldOp.timestamp.wallTime]
1645
- );
1646
- }
1647
- }
1648
- });
1649
- const newMaxTimestamp = newTimestamps[newTimestamps.length - 1] ?? null;
1650
- return {
1651
- operations: newOperations,
1652
- idMapping,
1653
- rebasedCount: newOperations.length,
1654
- newMaxTimestamp
1655
- };
1656
- }
1657
-
1658
- // src/transaction/transaction-context.ts
1659
- import {
1660
- createOperation as createOperation2,
1661
- generateUUIDv7,
1662
- isAtomicOp,
1663
- quoteIdent as quoteIdent6,
1664
- resolveAtomicOp,
1665
- toAtomicOp,
1666
- validateRecord
1667
- } from "@korajs/core";
1668
- var TransactionContext = class {
1669
- transactionId;
1670
- mutationName;
1671
- buffer = [];
1672
- committed = false;
1673
- rolledBack = false;
1674
- config;
1675
- constructor(config) {
1676
- this.config = config;
1677
- this.transactionId = generateUUIDv7();
1678
- }
1679
- /**
1680
- * Set a human-readable mutation name for this transaction.
1681
- * Propagated to all operations for DevTools display.
1682
- */
1683
- setMutationName(name) {
1684
- this.mutationName = name;
1685
- }
1686
- /**
1687
- * Get the mutation name, if set.
1688
- */
1689
- getMutationName() {
1690
- return this.mutationName;
1691
- }
1692
- /**
1693
- * Get a collection accessor for buffered operations within this transaction.
1694
- */
1695
- collection(name) {
1696
- const definition = this.config.schema.collections[name];
1697
- if (!definition) {
1698
- throw new Error(
1699
- `Unknown collection "${name}". Available: ${Object.keys(this.config.schema.collections).join(", ")}`
1700
- );
1701
- }
1702
- return {
1703
- insert: (data) => this.insert(name, definition, data),
1704
- update: (id, data) => this.update(name, definition, id, data),
1705
- delete: (id) => this.deleteRecord(name, definition, id),
1706
- findById: (id) => this.findById(name, definition, id)
1707
- };
1708
- }
1709
- /**
1710
- * Commit all buffered operations atomically.
1711
- * Returns the list of operations and affected collections for subscription notification.
1712
- */
1713
- async commit() {
1714
- if (this.committed) {
1715
- throw new Error("Transaction already committed.");
1716
- }
1717
- if (this.rolledBack) {
1718
- throw new Error("Transaction was rolled back and cannot be committed.");
1719
- }
1720
- this.committed = true;
1721
- if (this.buffer.length === 0) {
1722
- return { operations: [], affectedCollections: /* @__PURE__ */ new Set() };
1723
- }
1724
- const handler = this.config.localMutationHandler;
1725
- if (handler?.commitTransaction) {
1726
- return handler.commitTransaction({
1727
- entries: this.buffer.map((entry) => ({
1728
- operation: entry.operation,
1729
- commands: entry.commands,
1730
- collection: entry.collection
1731
- })),
1732
- transactionId: this.transactionId,
1733
- ...this.mutationName !== void 0 ? { mutationName: this.mutationName } : {}
1734
- });
1735
- }
1736
- const operations = [];
1737
- const affectedCollections = /* @__PURE__ */ new Set();
1738
- await this.config.adapter.transaction(async (tx) => {
1739
- for (const entry of this.buffer) {
1740
- if (entry.operation.type === "delete" && this.config.relationEnforcer) {
1741
- const cascadeResult = await this.config.relationEnforcer.enforceDelete(
1742
- entry.collection,
1743
- entry.operation.recordId,
1744
- tx,
1745
- [entry.operation.id]
1746
- );
1747
- for (const cascadedOp of cascadeResult.operations) {
1748
- operations.push(cascadedOp);
1749
- affectedCollections.add(cascadedOp.collection);
1750
- this.config.causalTracker?.afterOperation(cascadedOp.collection, cascadedOp.id, true);
1751
- }
1752
- }
1753
- for (const cmd of entry.commands) {
1754
- await tx.execute(cmd.sql, cmd.params);
1755
- }
1756
- operations.push(entry.operation);
1757
- affectedCollections.add(entry.collection);
1758
- }
1759
- const finalSeq = this.config.sequenceAllocator.getHighWaterMark();
1760
- if (finalSeq > 0) {
1761
- await tx.execute(
1762
- "INSERT OR REPLACE INTO _kora_version_vector (node_id, sequence_number) VALUES (?, ?)",
1763
- [this.config.nodeId, finalSeq]
1764
- );
1765
- }
1766
- });
1767
- return { operations, affectedCollections };
1768
- }
1769
- /**
1770
- * Mark the transaction as rolled back. No operations will be committed.
1771
- */
1772
- rollback() {
1773
- this.rolledBack = true;
1774
- this.buffer.length = 0;
1775
- }
1776
- /**
1777
- * Get the transaction ID shared by all operations in this transaction.
1778
- */
1779
- getTransactionId() {
1780
- return this.transactionId;
1781
- }
1782
- ensureActive() {
1783
- if (this.committed) {
1784
- throw new Error("Cannot perform operations on a committed transaction.");
1785
- }
1786
- if (this.rolledBack) {
1787
- throw new Error("Cannot perform operations on a rolled-back transaction.");
1788
- }
1789
- }
1790
- async insert(collectionName, definition, data) {
1791
- this.ensureActive();
1792
- const validated = validateRecord(collectionName, definition, data, "insert");
1793
- const recordId = generateUUIDv7();
1794
- const now = Date.now();
1795
- for (const [fieldName, descriptor] of Object.entries(definition.fields)) {
1796
- if (descriptor.auto && descriptor.kind === "timestamp") {
1797
- validated[fieldName] = now;
1798
- }
1799
- }
1800
- const sequenceNumber = await this.config.sequenceAllocator.allocate();
1801
- const causalDeps = this.config.causalTracker?.nextCausalDeps(collectionName, true) ?? [];
1802
- const operation = await createOperation2(
1803
- {
1804
- nodeId: this.config.nodeId,
1805
- type: "insert",
1806
- collection: collectionName,
1807
- recordId,
1808
- // Binary richtext values are tagged as canonical JSON before the
1809
- // operation is content-hashed (see encodeRichtextFieldsForOpData).
1810
- data: encodeRichtextFieldsForOpData(validated, definition.fields),
1811
- previousData: null,
1812
- sequenceNumber,
1813
- causalDeps,
1814
- schemaVersion: this.config.schema.version,
1815
- transactionId: this.transactionId,
1816
- ...this.mutationName !== void 0 ? { mutationName: this.mutationName } : {}
1817
- },
1818
- this.config.clock
1819
- );
1820
- this.config.causalTracker?.afterOperation(collectionName, operation.id, true);
1821
- const serializedData = serializeRecord(validated, definition.fields);
1822
- const version = serializeRowVersion(operation.timestamp);
1823
- const record = {
1824
- id: recordId,
1825
- ...serializedData,
1826
- _created_at: operation.timestamp.wallTime,
1827
- _updated_at: operation.timestamp.wallTime,
1828
- _version: version
1829
- };
1830
- const insertQuery = buildInsertQuery(collectionName, record);
1831
- const opRow = serializeOperation(operation);
1832
- const opInsert = buildInsertQuery(
1833
- `_kora_ops_${collectionName}`,
1834
- opRow
1835
- );
1836
- this.buffer.push({
1837
- operation,
1838
- collection: collectionName,
1839
- commands: [
1840
- { sql: insertQuery.sql, params: insertQuery.params },
1841
- { sql: opInsert.sql, params: opInsert.params }
1842
- ]
1843
- });
1844
- return {
1845
- id: recordId,
1846
- ...validated,
1847
- createdAt: now,
1848
- updatedAt: now
1849
- };
1850
- }
1851
- async update(collectionName, definition, id, data) {
1852
- this.ensureActive();
1853
- const currentRecord = await this.getEffectiveRecord(collectionName, definition, id);
1854
- if (!currentRecord) {
1855
- throw new RecordNotFoundError(collectionName, id);
1856
- }
1857
- const validated = validateRecord(collectionName, definition, data, "update");
1858
- const now = Date.now();
1859
- const previousData = {};
1860
- const resolvedData = {};
1861
- const atomicOps = {};
1862
- for (const key of Object.keys(validated)) {
1863
- const value = validated[key];
1864
- previousData[key] = currentRecord[key];
1865
- if (isAtomicOp(value)) {
1866
- resolvedData[key] = resolveAtomicOp(currentRecord[key], value);
1867
- atomicOps[key] = toAtomicOp(value);
1868
- } else {
1869
- resolvedData[key] = value;
1870
- }
1871
- }
1872
- const hasAtomicOps = Object.keys(atomicOps).length > 0;
1873
- const sequenceNumber = await this.config.sequenceAllocator.allocate();
1874
- const causalDeps = this.config.causalTracker?.nextCausalDeps(collectionName, true) ?? [];
1875
- const operation = await createOperation2(
1876
- {
1877
- nodeId: this.config.nodeId,
1878
- type: "update",
1879
- collection: collectionName,
1880
- recordId: id,
1881
- // Binary richtext values (new and previous) are tagged as canonical
1882
- // JSON before the operation is content-hashed.
1883
- data: encodeRichtextFieldsForOpData(resolvedData, definition.fields),
1884
- previousData: encodeRichtextFieldsForOpData(previousData, definition.fields),
1885
- sequenceNumber,
1886
- causalDeps,
1887
- schemaVersion: this.config.schema.version,
1888
- transactionId: this.transactionId,
1889
- ...this.mutationName !== void 0 ? { mutationName: this.mutationName } : {},
1890
- ...hasAtomicOps ? { atomicOps } : {}
1891
- },
1892
- this.config.clock
1893
- );
1894
- this.config.causalTracker?.afterOperation(collectionName, operation.id, true);
1895
- const serializedChanges = serializeRecord(resolvedData, definition.fields);
1896
- const version = serializeRowVersion(operation.timestamp);
1897
- const updateQuery = buildUpdateQuery(collectionName, id, {
1898
- ...serializedChanges,
1899
- _updated_at: operation.timestamp.wallTime,
1900
- _version: version
1901
- });
1902
- const opRow = serializeOperation(operation);
1903
- const opInsert = buildInsertQuery(
1904
- `_kora_ops_${collectionName}`,
1905
- opRow
1906
- );
1907
- this.buffer.push({
1908
- operation,
1909
- collection: collectionName,
1910
- commands: [
1911
- { sql: updateQuery.sql, params: updateQuery.params },
1912
- { sql: opInsert.sql, params: opInsert.params }
1913
- ]
1914
- });
1915
- return {
1916
- ...currentRecord,
1917
- ...resolvedData,
1918
- updatedAt: now
1919
- };
1920
- }
1921
- async deleteRecord(collectionName, definition, id) {
1922
- this.ensureActive();
1923
- const currentRecord = await this.getEffectiveRecord(collectionName, definition, id);
1924
- if (!currentRecord) {
1925
- throw new RecordNotFoundError(collectionName, id);
1926
- }
1927
- const now = Date.now();
1928
- const sequenceNumber = await this.config.sequenceAllocator.allocate();
1929
- const causalDeps = this.config.causalTracker?.nextCausalDeps(collectionName, true) ?? [];
1930
- const operation = await createOperation2(
1931
- {
1932
- nodeId: this.config.nodeId,
1933
- type: "delete",
1934
- collection: collectionName,
1935
- recordId: id,
1936
- data: null,
1937
- previousData: null,
1938
- sequenceNumber,
1939
- causalDeps,
1940
- schemaVersion: this.config.schema.version,
1941
- transactionId: this.transactionId,
1942
- ...this.mutationName !== void 0 ? { mutationName: this.mutationName } : {}
1943
- },
1944
- this.config.clock
1945
- );
1946
- this.config.causalTracker?.afterOperation(collectionName, operation.id, true);
1947
- const deleteQuery = buildSoftDeleteQuery(collectionName, id, now);
1948
- const opRow = serializeOperation(operation);
1949
- const opInsert = buildInsertQuery(
1950
- `_kora_ops_${collectionName}`,
1951
- opRow
1952
- );
1953
- this.buffer.push({
1954
- operation,
1955
- collection: collectionName,
1956
- commands: [
1957
- { sql: deleteQuery.sql, params: deleteQuery.params },
1958
- { sql: opInsert.sql, params: opInsert.params }
1959
- ]
1960
- });
1961
- }
1962
- async findById(collectionName, definition, id) {
1963
- return this.getEffectiveRecord(collectionName, definition, id);
1964
- }
1965
- /**
1966
- * Get the effective state of a record, considering both the database and buffered operations.
1967
- * Buffered inserts/updates take precedence over the database state.
1968
- */
1969
- async getEffectiveRecord(collectionName, definition, id) {
1970
- for (let i = this.buffer.length - 1; i >= 0; i--) {
1971
- const entry = this.buffer[i];
1972
- if (entry.collection === collectionName && entry.operation.recordId === id) {
1973
- if (entry.operation.type === "delete") {
1974
- return null;
1975
- }
1976
- break;
1977
- }
1978
- }
1979
- const rows = await this.config.adapter.query(
1980
- `SELECT * FROM ${quoteIdent6(collectionName)} WHERE id = ? AND _deleted = 0`,
1981
- [id]
1982
- );
1983
- let record = rows[0] ? deserializeRecord(rows[0], definition.fields) : null;
1984
- for (const entry of this.buffer) {
1985
- if (entry.collection !== collectionName || entry.operation.recordId !== id) {
1986
- continue;
1987
- }
1988
- if (entry.operation.type === "insert" && entry.operation.data) {
1989
- record = {
1990
- id,
1991
- // op.data stores binary richtext as tagged JSON; the effective
1992
- // record must expose record-shaped values (Uint8Array/string).
1993
- ...decodeRichtextFieldsFromOpData(entry.operation.data, definition.fields),
1994
- createdAt: entry.operation.timestamp.wallTime,
1995
- updatedAt: entry.operation.timestamp.wallTime
1996
- };
1997
- } else if (entry.operation.type === "update" && entry.operation.data && record) {
1998
- record = {
1999
- ...record,
2000
- ...decodeRichtextFieldsFromOpData(entry.operation.data, definition.fields),
2001
- updatedAt: entry.operation.timestamp.wallTime
2002
- };
2003
- } else if (entry.operation.type === "delete") {
2004
- record = null;
2005
- }
2006
- }
2007
- return record;
2008
- }
2009
- };
2010
-
2011
- // src/transaction/transaction-sequence.ts
2012
- var TransactionSequenceAllocator = class {
2013
- constructor(adapter, nodeId) {
2014
- this.adapter = adapter;
2015
- this.nodeId = nodeId;
2016
- }
2017
- adapter;
2018
- nodeId;
2019
- loaded = false;
2020
- watermark = 0;
2021
- async allocate() {
2022
- if (!this.loaded) {
2023
- this.watermark = await readSequenceNumber(this.adapter, this.nodeId);
2024
- this.loaded = true;
2025
- }
2026
- this.watermark++;
2027
- return this.watermark;
2028
- }
2029
- getHighWaterMark() {
2030
- return this.watermark;
2031
- }
2032
- };
2033
-
2034
- // src/store/tab-node-id.ts
2035
- import { generateUUIDv7 as generateUUIDv72 } from "@korajs/core";
2036
- function perTabStorageKey(dbName) {
2037
- return `kora-tab-node-${dbName}`;
2038
- }
2039
- function loadPerTabNodeId(dbName) {
2040
- if (typeof sessionStorage === "undefined") {
2041
- return null;
2042
- }
2043
- return sessionStorage.getItem(perTabStorageKey(dbName));
2044
- }
2045
- function savePerTabNodeId(dbName, nodeId) {
2046
- if (typeof sessionStorage === "undefined") {
2047
- return;
2048
- }
2049
- sessionStorage.setItem(perTabStorageKey(dbName), nodeId);
2050
- }
2051
- function resolvePerTabNodeId(dbName) {
2052
- const existing = loadPerTabNodeId(dbName);
2053
- if (existing) {
2054
- return existing;
2055
- }
2056
- const nodeId = generateUUIDv72();
2057
- savePerTabNodeId(dbName, nodeId);
2058
- return nodeId;
2059
- }
2060
-
2061
- // src/store/store.ts
2062
- var Store = class {
2063
- opened = false;
2064
- nodeId = "";
2065
- sequenceNumber = 0;
2066
- versionVector = createVersionVector3();
2067
- clock = null;
2068
- collections = /* @__PURE__ */ new Map();
2069
- subscriptionManager;
2070
- sequenceManager = null;
2071
- schema;
2072
- adapter;
2073
- configNodeId;
2074
- dbName;
2075
- isolation;
2076
- emitter;
2077
- localMutationHandler;
2078
- relationEnforcer = null;
2079
- causalTracker = null;
2080
- secretKeyProvider;
2081
- constructor(config) {
2082
- this.schema = config.schema;
2083
- this.adapter = config.adapter;
2084
- this.configNodeId = config.nodeId;
2085
- this.dbName = config.dbName ?? "kora-db";
2086
- this.isolation = config.isolation ?? "shared";
2087
- this.emitter = config.emitter ?? null;
2088
- this.localMutationHandler = config.localMutationHandler ?? null;
2089
- this.secretKeyProvider = config.secretKeyProvider;
2090
- this.subscriptionManager = new SubscriptionManager({
2091
- onQuerySubscribed: config.onQuerySubscribed
2092
- });
2093
- }
2094
- /**
2095
- * Open the store: initialize the database, load or generate a node ID,
2096
- * restore the sequence number and version vector, and create Collection instances.
2097
- */
2098
- async open() {
2099
- await this.adapter.open(this.schema);
2100
- await this.runMigrationsIfNeeded();
2101
- this.nodeId = await this.loadOrGenerateNodeId();
2102
- this.clock = new HybridLogicalClock3(this.nodeId);
2103
- this.causalTracker = new CausalTracker();
2104
- this.sequenceManager = new SequenceManager(this.adapter, this.nodeId);
2105
- this.sequenceNumber = await this.loadSequenceNumber();
2106
- this.versionVector = await this.loadVersionVector();
2107
- const hasRelations = Object.keys(this.schema.relations).length > 0;
2108
- this.relationEnforcer = hasRelations ? new RelationEnforcer({
2109
- schema: this.schema,
2110
- adapter: this.adapter,
2111
- clock: this.clock,
2112
- nodeId: this.nodeId
2113
- }) : null;
2114
- for (const [name, definition] of Object.entries(this.schema.collections)) {
2115
- const col = new Collection(
2116
- name,
2117
- definition,
2118
- this.schema,
2119
- this.adapter,
2120
- this.clock,
2121
- this.nodeId,
2122
- () => this.allocateSequenceNumber(),
2123
- (collectionName, operation) => {
2124
- this.recordOperationSequence(operation);
2125
- this.subscriptionManager.notify(collectionName, operation);
2126
- if (this.emitter) {
2127
- this.emitter.emit({ type: "operation:created", operation });
2128
- }
2129
- },
2130
- this.relationEnforcer,
2131
- this.localMutationHandler,
2132
- this.causalTracker,
2133
- this.secretKeyProvider
2134
- );
2135
- this.collections.set(name, col);
2136
- }
2137
- this.opened = true;
2138
- }
2139
- /**
2140
- * Close the store: clear subscriptions and close the adapter.
2141
- */
2142
- async close() {
2143
- this.subscriptionManager.clear();
2144
- this.collections.clear();
2145
- this.opened = false;
2146
- await this.adapter.close();
2147
- }
2148
- /**
2149
- * Get a Collection instance for CRUD operations.
2150
- * @throws {StoreNotOpenError} If the store is not open
2151
- * @throws {Error} If the collection name is not in the schema
2152
- */
2153
- collection(name) {
2154
- this.ensureOpen();
2155
- const col = this.collections.get(name);
2156
- if (!col) {
2157
- throw new Error(
2158
- `Unknown collection "${name}". Available: ${[...this.collections.keys()].join(", ")}`
2159
- );
2160
- }
2161
- const definition = this.schema.collections[name];
2162
- if (!definition) {
2163
- throw new Error(`Collection definition not found for "${name}"`);
2164
- }
2165
- return {
2166
- insert: (data) => col.insert(data),
2167
- findById: (id) => col.findById(id),
2168
- update: (id, data) => col.update(id, data),
2169
- delete: (id) => col.delete(id),
2170
- where: (conditions) => new QueryBuilder(
2171
- name,
2172
- definition,
2173
- this.adapter,
2174
- this.subscriptionManager,
2175
- conditions,
2176
- this.schema
2177
- )
2178
- };
2179
- }
2180
- /**
2181
- * Get the current version vector.
2182
- */
2183
- getVersionVector() {
2184
- this.ensureOpen();
2185
- return new Map(this.versionVector);
2186
- }
2187
- /**
2188
- * Get the node ID for this store instance.
2189
- */
2190
- /**
2191
- * Records the offset between server time and this device's clock
2192
- * (serverTime - localTime), learned at sync handshake. Lets the store's HLC
2193
- * validate remote timestamps correctly even when the local clock is wrong.
2194
- */
2195
- setClockReferenceOffset(offsetMs) {
2196
- this.clock?.setReferenceOffset(offsetMs);
2197
- }
2198
- getNodeId() {
2199
- this.ensureOpen();
2200
- return this.nodeId;
2201
- }
2202
- /**
2203
- * Apply a remote operation received from sync.
2204
- * Checks for duplicates, applies to the data table, persists the operation,
2205
- * and updates the version vector.
2206
- */
2207
- async applyRemoteOperation(op, options) {
2208
- this.ensureOpen();
2209
- const collection = op.collection;
2210
- const definition = this.schema.collections[collection];
2211
- if (!definition) {
2212
- return "skipped";
2213
- }
2214
- const existing = await this.adapter.query(
2215
- `SELECT id FROM ${quoteIdent7(`_kora_ops_${collection}`)} WHERE id = ?`,
2216
- [op.id]
2217
- );
2218
- if (existing.length > 0) {
2219
- return "duplicate";
2220
- }
2221
- if (this.clock) {
2222
- this.clock.receive(op.timestamp);
2223
- }
2224
- const materializeTimestamp = options?.materializeTimestamp ?? op.timestamp;
2225
- const remoteVersion = serializeRowVersion(materializeTimestamp);
2226
- const wallTime = materializeTimestamp.wallTime;
2227
- const materializeSource = options?.materializeData ?? op.data;
2228
- await this.adapter.transaction(async (tx) => {
2229
- const checkGuard = (row) => {
2230
- const guard = options?.guardRowState;
2231
- if (!guard) {
2232
- return;
2233
- }
2234
- const version = typeof row?._version === "string" ? row._version : null;
2235
- const fieldVersions = typeof row?._field_versions === "string" ? row._field_versions : null;
2236
- if (version !== guard.version || fieldVersions !== guard.fieldVersions) {
2237
- throw new OptimisticLockError(collection, op.recordId);
2238
- }
2239
- };
2240
- if (options?.logOnly) {
2241
- } else if (op.type === "insert" && materializeSource) {
2242
- const serializedData = serializeRecord(materializeSource, definition.fields);
2243
- const existing2 = await tx.query(
2244
- `SELECT _updated_at, _version, _field_versions, _deleted FROM ${collection} WHERE id = ?`,
2245
- [op.recordId]
2246
- );
2247
- const row = existing2[0];
2248
- checkGuard(row);
2249
- if (!row) {
2250
- const record = {
2251
- id: op.recordId,
2252
- ...serializedData,
2253
- _created_at: wallTime,
2254
- _updated_at: wallTime,
2255
- _version: remoteVersion,
2256
- // Stamp every inserted field so per-field LWW has a real baseline.
2257
- _field_versions: serializeFieldVersions(
2258
- fieldVersionsForFields(Object.keys(serializedData), remoteVersion)
2259
- )
2260
- };
2261
- const insertQuery = buildInsertQuery(collection, record);
2262
- await tx.execute(insertQuery.sql, insertQuery.params);
2263
- await this.foldOrphanedOperations(tx, collection, definition, op.recordId, op);
2264
- } else if (row._deleted === 1) {
2265
- if (isIncomingNewerThanRow(materializeTimestamp, row)) {
2266
- const fieldChanges = {
2267
- ...serializedData,
2268
- _deleted: 0,
2269
- _field_versions: serializeFieldVersions(
2270
- fieldVersionsForFields(Object.keys(serializedData), remoteVersion)
2271
- )
2272
- };
2273
- const upsert = buildFieldFastForwardUpdateQuery(
2274
- collection,
2275
- op.recordId,
2276
- fieldChanges,
2277
- remoteVersion,
2278
- wallTime,
2279
- { maxCreatedAt: wallTime }
2280
- );
2281
- await tx.execute(upsert.sql, upsert.params);
2282
- }
2283
- } else {
2284
- const currentVersions = parseFieldVersions(row._field_versions);
2285
- const rowVersion = typeof row._version === "string" ? row._version : void 0;
2286
- const fieldChanges = {};
2287
- let fieldVersionsJson;
2288
- if (options?.forceMaterialize) {
2289
- const merged = { ...currentVersions };
2290
- for (const field of Object.keys(serializedData)) {
2291
- fieldChanges[field] = serializedData[field];
2292
- merged[field] = remoteVersion;
2293
- }
2294
- fieldVersionsJson = serializeFieldVersions(merged);
2295
- } else {
2296
- const { winners, merged } = resolvePerFieldLww(
2297
- currentVersions,
2298
- Object.keys(serializedData),
2299
- remoteVersion,
2300
- rowVersion
2301
- );
2302
- for (const field of winners) {
2303
- fieldChanges[field] = serializedData[field];
2304
- }
2305
- fieldVersionsJson = serializeFieldVersions(merged);
2306
- }
2307
- const upsert = buildFieldFastForwardUpdateQuery(
2308
- collection,
2309
- op.recordId,
2310
- { ...fieldChanges, _field_versions: fieldVersionsJson },
2311
- remoteVersion,
2312
- wallTime,
2313
- { maxCreatedAt: wallTime }
2314
- );
2315
- await tx.execute(upsert.sql, upsert.params);
2316
- }
2317
- } else if (op.type === "update" && materializeSource) {
2318
- const serializedChanges = serializeRecord(materializeSource, definition.fields);
2319
- const changedFields = Object.keys(serializedChanges);
2320
- const currentRows = await tx.query(
2321
- `SELECT _version, _field_versions FROM ${quoteIdent7(collection)} WHERE id = ?`,
2322
- [op.recordId]
2323
- );
2324
- const currentRow = currentRows[0];
2325
- checkGuard(currentRow);
2326
- if (currentRow) {
2327
- const currentVersions = parseFieldVersions(currentRow._field_versions);
2328
- const rowVersion = typeof currentRow._version === "string" ? currentRow._version : void 0;
2329
- if (options?.forceMaterialize) {
2330
- const merged = { ...currentVersions };
2331
- for (const field of changedFields) {
2332
- merged[field] = remoteVersion;
2333
- }
2334
- const fieldChanges = {
2335
- ...serializedChanges,
2336
- _field_versions: serializeFieldVersions(merged)
2337
- };
2338
- if (options?.reactivateIfDeleted) {
2339
- fieldChanges._deleted = 0;
2340
- }
2341
- const forceQuery = buildFieldFastForwardUpdateQuery(
2342
- collection,
2343
- op.recordId,
2344
- fieldChanges,
2345
- remoteVersion,
2346
- wallTime
2347
- );
2348
- await tx.execute(forceQuery.sql, forceQuery.params);
2349
- } else {
2350
- const { winners, merged } = resolvePerFieldLww(
2351
- currentVersions,
2352
- changedFields,
2353
- remoteVersion,
2354
- rowVersion
2355
- );
2356
- if (winners.length > 0 || options?.reactivateIfDeleted) {
2357
- const winningChanges = {
2358
- _field_versions: serializeFieldVersions(merged)
2359
- };
2360
- for (const field of winners) {
2361
- winningChanges[field] = serializedChanges[field];
2362
- }
2363
- if (options?.reactivateIfDeleted) {
2364
- winningChanges._deleted = 0;
2365
- }
2366
- const forceQuery = buildFieldFastForwardUpdateQuery(
2367
- collection,
2368
- op.recordId,
2369
- winningChanges,
2370
- remoteVersion,
2371
- wallTime
2372
- );
2373
- await tx.execute(forceQuery.sql, forceQuery.params);
2374
- }
2375
- }
2376
- }
2377
- } else if (op.type === "delete") {
2378
- const deleteQuery = buildLwwSoftDeleteQuery(
2379
- collection,
2380
- op.recordId,
2381
- wallTime,
2382
- remoteVersion
2383
- );
2384
- await tx.execute(deleteQuery.sql, deleteQuery.params);
2385
- }
2386
- const opRow = serializeOperation(op);
2387
- const opInsert = buildInsertQuery(
2388
- `_kora_ops_${collection}`,
2389
- opRow
2390
- );
2391
- await tx.execute(opInsert.sql, opInsert.params);
2392
- const currentSeq = this.versionVector.get(op.nodeId) ?? 0;
2393
- if (op.sequenceNumber > currentSeq) {
2394
- this.versionVector.set(op.nodeId, op.sequenceNumber);
2395
- await tx.execute(
2396
- "INSERT OR REPLACE INTO _kora_version_vector (node_id, sequence_number) VALUES (?, ?)",
2397
- [op.nodeId, op.sequenceNumber]
2398
- );
2399
- }
2400
- });
2401
- this.subscriptionManager.notify(collection, op);
2402
- return "applied";
2403
- }
2404
- /**
2405
- * Materialize update/delete operations that were logged for a record BEFORE
2406
- * its insert arrived (reordered delivery). Runs inside the insert's write
2407
- * transaction, folding each orphan in timestamp order through the exact
2408
- * per-field LWW / LWW-delete rules the normal apply paths use, so the final
2409
- * row state equals what a device that received the operations in causal
2410
- * order computed. Called only when the insert created a previously-absent
2411
- * row — in that case every logged update/delete for the record is
2412
- * necessarily an orphan (rows are soft-deleted, never removed, so a row
2413
- * that is absent now was never materialized).
2414
- */
2415
- async foldOrphanedOperations(tx, collection, definition, recordId, insertOp) {
2416
- const orphanRows = await tx.query(
2417
- `SELECT * FROM ${quoteIdent7(`_kora_ops_${collection}`)} WHERE record_id = ? AND type IN ('update', 'delete')`,
2418
- [recordId]
2419
- );
2420
- if (orphanRows.length === 0) {
2421
- return;
2422
- }
2423
- const orphans = orphanRows.map((r) => deserializeOperationWithCollection(r, collection)).sort((a, b) => HybridLogicalClock3.compare(a.timestamp, b.timestamp));
2424
- for (const orphan of orphans) {
2425
- const orphanVersion = serializeRowVersion(orphan.timestamp);
2426
- const orphanWall = orphan.timestamp.wallTime;
2427
- if (orphan.type === "update" && orphan.data) {
2428
- const orphanChanges = serializeRecord(orphan.data, definition.fields);
2429
- const rows = await tx.query(
2430
- `SELECT _version, _field_versions FROM ${quoteIdent7(collection)} WHERE id = ?`,
2431
- [recordId]
2432
- );
2433
- const current = rows[0];
2434
- if (!current) {
2435
- continue;
2436
- }
2437
- const { winners, merged } = resolvePerFieldLww(
2438
- parseFieldVersions(current._field_versions),
2439
- Object.keys(orphanChanges),
2440
- orphanVersion,
2441
- typeof current._version === "string" ? current._version : void 0
2442
- );
2443
- if (winners.length > 0) {
2444
- const winningChanges = {
2445
- _field_versions: serializeFieldVersions(merged)
2446
- };
2447
- for (const field of winners) {
2448
- winningChanges[field] = orphanChanges[field];
2449
- }
2450
- const query = buildFieldFastForwardUpdateQuery(
2451
- collection,
2452
- recordId,
2453
- winningChanges,
2454
- orphanVersion,
2455
- orphanWall
2456
- );
2457
- await tx.execute(query.sql, query.params);
2458
- }
2459
- } else if (orphan.type === "delete") {
2460
- const query = buildLwwSoftDeleteQuery(collection, recordId, orphanWall, orphanVersion);
2461
- await tx.execute(query.sql, query.params);
2462
- }
2463
- }
2464
- const atomicFields = /* @__PURE__ */ new Set();
2465
- for (const orphan of orphans) {
2466
- if (orphan.type === "update" && orphan.atomicOps) {
2467
- for (const field of Object.keys(orphan.atomicOps)) {
2468
- atomicFields.add(field);
2469
- }
2470
- }
2471
- }
2472
- if (atomicFields.size > 0) {
2473
- const ordered = [insertOp, ...orphans].sort(
2474
- (a, b) => HybridLogicalClock3.compare(a.timestamp, b.timestamp)
2475
- );
2476
- const folded = replayOperationsForRecord(ordered);
2477
- if (folded) {
2478
- const composed = {};
2479
- for (const field of atomicFields) {
2480
- if (field in folded) {
2481
- composed[field] = folded[field];
2482
- }
2483
- }
2484
- const serialized = serializeRecord(composed, definition.fields);
2485
- const cols = Object.keys(serialized);
2486
- if (cols.length > 0) {
2487
- const setClause = cols.map((c) => `${quoteIdent7(c)} = ?`).join(", ");
2488
- await tx.execute(`UPDATE ${quoteIdent7(collection)} SET ${setClause} WHERE id = ?`, [
2489
- ...cols.map((c) => serialized[c]),
2490
- recordId
2491
- ]);
2492
- }
2493
- }
2494
- }
2495
- }
2496
- /**
2497
- * Get operations from a node within a sequence number range.
2498
- * Implements the OperationLog interface for computeDelta.
2499
- */
2500
- async getRange(nodeId, fromSeq, toSeq) {
2501
- return this.getOperationRange(nodeId, fromSeq, toSeq);
2502
- }
2503
- /**
2504
- * Get operations from a node within a sequence number range.
2505
- */
2506
- async getOperationRange(nodeId, fromSeq, toSeq) {
2507
- this.ensureOpen();
2508
- const allOps = [];
2509
- for (const collectionName of Object.keys(this.schema.collections)) {
2510
- const rows = await this.adapter.query(
2511
- `SELECT * FROM ${quoteIdent7(`_kora_ops_${collectionName}`)} WHERE node_id = ? AND sequence_number >= ? AND sequence_number <= ? ORDER BY sequence_number ASC`,
2512
- [nodeId, fromSeq, toSeq]
2513
- );
2514
- for (const row of rows) {
2515
- allOps.push(deserializeOperationWithCollection(row, collectionName));
2516
- }
2517
- }
2518
- allOps.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
2519
- return allOps;
2520
- }
2521
- /**
2522
- * Load every operation from the local append-only log across all collections.
2523
- * Used by sync delta computation, backup export, and time-travel replay.
2524
- */
2525
- async getAllOperations() {
2526
- this.ensureOpen();
2527
- const allOps = [];
2528
- for (const collectionName of Object.keys(this.schema.collections)) {
2529
- const rows = await this.adapter.query(
2530
- `SELECT * FROM ${quoteIdent7(`_kora_ops_${collectionName}`)} ORDER BY sequence_number ASC`
2531
- );
2532
- for (const row of rows) {
2533
- allOps.push(deserializeOperationWithCollection(row, collectionName));
2534
- }
2535
- }
2536
- return allOps;
2537
- }
2538
- /**
2539
- * Rebuild an in-memory snapshot of materialized state at a causal cut in the op log.
2540
- * Does not mutate the live store — intended for DevTools time-travel inspection.
2541
- *
2542
- * @param operationId - Content-addressed id of the operation to replay through (inclusive)
2543
- * @throws {OperationError} When the operation id is not present in the local log
2544
- */
2545
- async replayTo(operationId) {
2546
- this.ensureOpen();
2547
- const start = Date.now();
2548
- const allOps = await this.getAllOperations();
2549
- const snapshot = buildReplaySnapshot(this.schema, allOps, operationId);
2550
- if (this.emitter) {
2551
- this.emitter.emit({
2552
- type: "replay:completed",
2553
- targetOperationId: operationId,
2554
- operationsApplied: snapshot.operationsApplied.length,
2555
- duration: Date.now() - start
2556
- });
2557
- }
2558
- return snapshot;
2559
- }
2560
- /**
2561
- * Persist a merge trace to the durable audit log.
2562
- */
2563
- async appendAuditTrace(trace) {
2564
- this.ensureOpen();
2565
- const { appendAuditTrace: append } = await import("./audit-trace-store-AM4EWQNZ.js");
2566
- await append(this.adapter, trace);
2567
- }
2568
- /**
2569
- * Read persisted audit traces with optional filters.
2570
- */
2571
- async getAuditTraces(query) {
2572
- this.ensureOpen();
2573
- const { readAuditTraces: readAuditTraces2 } = await import("./audit-trace-store-AM4EWQNZ.js");
2574
- return readAuditTraces2(this.adapter, query);
2575
- }
2576
- /**
2577
- * Export operations and merge traces as a portable audit bundle.
2578
- */
2579
- async exportAudit(options) {
2580
- this.ensureOpen();
2581
- const { exportAudit: doExport } = await import("./export-audit-ODGQTQIK.js");
2582
- return doExport(this.adapter, this.schema, this.nodeId, this.schema.version, options);
2583
- }
2584
- /**
2585
- * Get the schema definition.
2586
- */
2587
- getSchema() {
2588
- return this.schema;
2589
- }
2590
- /**
2591
- * Route local CRUD through the unified apply pipeline (korajs ApplyPipeline).
2592
- */
2593
- setLocalMutationHandler(handler) {
2594
- this.localMutationHandler = handler;
2595
- for (const col of this.collections.values()) {
2596
- col.setMutationHandler(handler);
2597
- }
2598
- }
2599
- /**
2600
- * Notify this store that another same-origin runtime committed an operation to
2601
- * the shared local database. The operation has already been persisted and
2602
- * materialized by the database owner; this method only advances in-memory causal
2603
- * watermarks and invalidates local reactive queries so already-open tabs stay
2604
- * live without reapplying or rewriting the operation.
2605
- */
2606
- notifyExternalOperation(operation) {
2607
- this.ensureOpen();
2608
- const definition = this.schema.collections[operation.collection];
2609
- if (!definition) {
2610
- return;
2611
- }
2612
- this.recordOperationSequence(operation);
2613
- this.subscriptionManager.notify(operation.collection, operation);
2614
- }
2615
- /**
2616
- * Build mutation context for a collection (used by ApplyPipeline side effects).
2617
- */
2618
- createMutationContext(collection, options) {
2619
- this.ensureOpen();
2620
- const definition = this.schema.collections[collection];
2621
- if (!definition || !this.clock) {
2622
- throw new StoreNotOpenError();
2623
- }
2624
- return {
2625
- collection,
2626
- definition,
2627
- schema: this.schema,
2628
- adapter: this.adapter,
2629
- clock: this.clock,
2630
- nodeId: this.nodeId,
2631
- allocateSequenceNumber: () => this.allocateSequenceNumber(),
2632
- onMutation: (collectionName, operation) => {
2633
- this.recordOperationSequence(operation);
2634
- this.subscriptionManager.notify(collectionName, operation);
2635
- if (this.emitter) {
2636
- this.emitter.emit({ type: "operation:created", operation });
2637
- }
2638
- },
2639
- relationEnforcer: this.relationEnforcer,
2640
- causalTracker: this.causalTracker,
2641
- inTransaction: options?.inTransaction ?? false,
2642
- extraCausalDeps: options?.extraCausalDeps,
2643
- secretKeyProvider: this.secretKeyProvider
2644
- };
2645
- }
2646
- /**
2647
- * Load a materialized row by ID, including soft-deleted tombstones.
2648
- */
2649
- async findMaterializedRow(collection, recordId) {
2650
- this.ensureOpen();
2651
- const definition = this.schema.collections[collection];
2652
- if (!definition) {
2653
- return null;
2654
- }
2655
- const rows = await this.adapter.query(
2656
- `SELECT * FROM ${quoteIdent7(collection)} WHERE id = ?`,
2657
- [recordId]
2658
- );
2659
- const row = rows[0];
2660
- if (!row) {
2661
- return null;
2662
- }
2663
- return {
2664
- record: deserializeRecord(row, definition.fields),
2665
- deleted: row._deleted === 1
2666
- };
2667
- }
2668
- /**
2669
- * Raw version state of a materialized row, for optimistic-concurrency guarded
2670
- * applies: capture this snapshot before computing a merge, pass it as
2671
- * `guardRowState`, and the apply refuses to write if the row changed since.
2672
- * Returns null when no row exists (which is itself a valid guard state — the
2673
- * apply then requires the row to still be absent).
2674
- */
2675
- async getRowVersionState(collection, recordId) {
2676
- this.ensureOpen();
2677
- if (!this.schema.collections[collection]) {
2678
- return null;
2679
- }
2680
- const rows = await this.adapter.query(
2681
- `SELECT _version, _field_versions FROM ${quoteIdent7(collection)} WHERE id = ?`,
2682
- [recordId]
2683
- );
2684
- const row = rows[0];
2685
- if (!row) {
2686
- return null;
2687
- }
2688
- return {
2689
- version: typeof row._version === "string" ? row._version : null,
2690
- fieldVersions: typeof row._field_versions === "string" ? row._field_versions : null
2691
- };
2692
- }
2693
- /**
2694
- * Latest operation from this device for a record (used for delete-vs-update merge on sync).
2695
- */
2696
- /**
2697
- * Load the last server version vector acknowledged by this client (persisted in `_kora_meta`).
2698
- */
2699
- async loadLastAckedServerVector() {
2700
- this.ensureOpen();
2701
- return loadLastAckedServerVector(this.adapter);
2702
- }
2703
- /**
2704
- * Persist the last server version vector this client believes the server has applied.
2705
- */
2706
- async saveLastAckedServerVector(vector) {
2707
- this.ensureOpen();
2708
- await saveLastAckedServerVector(this.adapter, vector);
2709
- }
2710
- /**
2711
- * Load persisted delta cursor for resuming paginated initial sync.
2712
- */
2713
- async loadDeltaCursor() {
2714
- this.ensureOpen();
2715
- return loadDeltaCursor(this.adapter);
2716
- }
2717
- /**
2718
- * Persist or clear the delta cursor for paginated initial sync resume.
2719
- */
2720
- async saveDeltaCursor(cursor) {
2721
- this.ensureOpen();
2722
- await saveDeltaCursor(this.adapter, cursor);
2723
- }
2724
- /**
2725
- * Load the persisted delivery watermark for a view signature (0 when none recorded).
2726
- * Defaults to the unfiltered view so existing single-watermark reads keep working.
2727
- */
2728
- async loadDeliveryWatermark(signature = "") {
2729
- this.ensureOpen();
2730
- return loadDeliveryWatermark(this.adapter, signature);
2731
- }
2732
- /**
2733
- * Persist the delivery watermark for a view signature so it survives a client restart.
2734
- */
2735
- async saveDeliveryWatermark(signature, watermark) {
2736
- this.ensureOpen();
2737
- await saveDeliveryWatermark(this.adapter, signature, watermark);
2738
- }
2739
- /**
2740
- * Load every persisted view watermark, keyed by signature.
2741
- */
2742
- async loadAllDeliveryWatermarks() {
2743
- this.ensureOpen();
2744
- return loadAllDeliveryWatermarks(this.adapter);
2745
- }
2746
- /**
2747
- * Delete a persisted view watermark (safe: the view back-fills from 0 when next visited).
2748
- */
2749
- async deleteDeliveryWatermark(signature) {
2750
- this.ensureOpen();
2751
- await deleteDeliveryWatermark(this.adapter, signature);
2752
- }
2753
- /**
2754
- * Local operations not yet reflected on the server version vector.
2755
- */
2756
- async getUnsyncedOperations(serverVector) {
2757
- this.ensureOpen();
2758
- return collectOperationsAheadOfServer(
2759
- this.getVersionVector(),
2760
- serverVector,
2761
- (nodeId, fromSeq, toSeq) => this.getOperationRange(nodeId, fromSeq, toSeq)
2762
- );
2763
- }
2764
- /**
2765
- * Re-stamp never-acknowledged local operations after a fast device clock was
2766
- * corrected, so sync can resume immediately. Safe because unacknowledged
2767
- * operations are private to this device (like unpushed git commits);
2768
- * acknowledged/shared operations are immutable and never touched.
2769
- *
2770
- * After the rewrite the store's HLC is advanced past the highest new
2771
- * timestamp so subsequent local writes keep sorting after the rebased ops.
2772
- * No data-change notifications are emitted: materialized values are
2773
- * unchanged, only version stamps move.
2774
- */
2775
- async rebaseUnsyncedOperations(unsyncedOpIds, correctedNowMs) {
2776
- this.ensureOpen();
2777
- const result = await rebaseUnsyncedOperationsInLog(
2778
- this.adapter,
2779
- this.schema,
2780
- unsyncedOpIds,
2781
- correctedNowMs
2782
- );
2783
- if (result.newMaxTimestamp && this.clock) {
2784
- this.clock.advanceTo(result.newMaxTimestamp);
2785
- }
2786
- return result;
2787
- }
2788
- /**
2789
- * Count of local operations ahead of the server version vector.
2790
- */
2791
- async countUnsyncedOperations(serverVector) {
2792
- const ops = await this.getUnsyncedOperations(serverVector);
2793
- return ops.length;
2794
- }
2795
- /**
2796
- * Compact the local operation log using materialized rows as the baseline.
2797
- * Only removes ops the server has acknowledged (per {@link CompactionStrategy}).
2798
- */
2799
- async compact(strategy) {
2800
- this.ensureOpen();
2801
- if (strategy.mode === "never") {
2802
- return compactOperationLog(this.adapter, this.schema, strategy, createVersionVector3());
2803
- }
2804
- const serverVector = strategy.serverVector ?? await loadLastAckedServerVector(this.adapter);
2805
- return compactOperationLog(this.adapter, this.schema, strategy, serverVector);
2806
- }
2807
- /**
2808
- * Merge session remote vector with persisted last-acked vector (max per node).
2809
- */
2810
- mergeServerVectors(sessionVector, persistedVector) {
2811
- return mergeVersionVectors(persistedVector, sessionVector);
2812
- }
2813
- async getLatestLocalOperationForRecord(collection, recordId) {
2814
- this.ensureOpen();
2815
- const rows = await this.adapter.query(
2816
- `SELECT * FROM ${quoteIdent7(`_kora_ops_${collection}`)} WHERE node_id = ? AND record_id = ? ORDER BY sequence_number DESC LIMIT 1`,
2817
- [this.nodeId, recordId]
2818
- );
2819
- const row = rows[0];
2820
- if (!row) {
2821
- return null;
2822
- }
2823
- return deserializeOperationWithCollection(row, collection);
2824
- }
2825
- /**
2826
- * Latest operation for a record from any node (for 3-way merge when local op log is empty).
2827
- */
2828
- async getLatestOperationForRecord(collection, recordId) {
2829
- this.ensureOpen();
2830
- const rows = await this.adapter.query(
2831
- `SELECT * FROM ${quoteIdent7(`_kora_ops_${collection}`)} WHERE record_id = ?`,
2832
- [recordId]
2833
- );
2834
- let latest = null;
2835
- for (const row of rows) {
2836
- const op = deserializeOperationWithCollection(row, collection);
2837
- if (!latest || HybridLogicalClock3.compare(op.timestamp, latest.timestamp) > 0) {
2838
- latest = op;
2839
- }
2840
- }
2841
- return latest;
2842
- }
2843
- /**
2844
- * All operations for a record, sorted in HLC total order (wallTime, logical,
2845
- * nodeId). Used by the apply pipeline to materialize atomic-op fields by folding
2846
- * the record's log, matching the server's materialization exactly.
2847
- */
2848
- async getOperationsForRecord(collection, recordId) {
2849
- this.ensureOpen();
2850
- const rows = await this.adapter.query(
2851
- `SELECT * FROM ${quoteIdent7(`_kora_ops_${collection}`)} WHERE record_id = ?`,
2852
- [recordId]
2853
- );
2854
- const ops = rows.map((row) => deserializeOperationWithCollection(row, collection));
2855
- ops.sort((a, b) => HybridLogicalClock3.compare(a.timestamp, b.timestamp));
2856
- return ops;
2857
- }
2858
- /** Expose the subscription manager for direct access (e.g., by QueryBuilder) */
2859
- getSubscriptionManager() {
2860
- return this.subscriptionManager;
10
+ Collection,
11
+ QueryBuilder,
12
+ SequenceManager,
13
+ Store,
14
+ SubscriptionBloomFilter,
15
+ SubscriptionManager,
16
+ TransactionContext,
17
+ compactOperationLog,
18
+ computeAckCompactionWatermark,
19
+ normalizeWhere,
20
+ pluralize,
21
+ queryKey,
22
+ singularize
23
+ } from "./chunk-Y5LRH7KI.js";
24
+ import {
25
+ BlobIntegrityError,
26
+ MemoryBlobStore
27
+ } from "./chunk-W4MTTNEZ.js";
28
+ import {
29
+ InvalidStateTransitionError,
30
+ validateStateTransition,
31
+ validateUpdateStateMachine
32
+ } from "./chunk-HZTDBLDB.js";
33
+ import "./chunk-A6GFH5IG.js";
34
+ import {
35
+ BACKUP_VERSION,
36
+ BackupFormatError,
37
+ COMPACTION_BASELINE_META_KEY,
38
+ LOG_QUARANTINE_TABLE,
39
+ convertBackupV1,
40
+ exportBackup,
41
+ readBackupManifest,
42
+ restoreBackup,
43
+ verifyBackupChecksum
44
+ } from "./chunk-P4V375FY.js";
45
+ import {
46
+ mergeYjsUpdates
47
+ } from "./chunk-32EDKWMI.js";
48
+ import {
49
+ LAST_ACKED_SERVER_VECTOR_META_KEY,
50
+ VIRTUAL_TIMESTAMP_FIELDS,
51
+ VIRTUAL_TIMESTAMP_FIELD_NAMES,
52
+ collectOperationsAheadOfServer,
53
+ deserializeVersionVectorFromMeta,
54
+ mergeVersionVectors,
55
+ serializeVersionVectorToMeta
56
+ } from "./chunk-SJP2HTYZ.js";
57
+ import {
58
+ decodeRichtext,
59
+ encodeRichtext,
60
+ richtextStatesEqual,
61
+ richtextToPlainText
62
+ } from "./chunk-J5AGXF3D.js";
63
+ import {
64
+ AdapterError,
65
+ BridgeTerminatedError,
66
+ LeaderUnresponsiveError,
67
+ NoLeaderError,
68
+ OptimisticLockError,
69
+ PersistenceError,
70
+ QueryError,
71
+ RecordNotFoundError,
72
+ RequestAbortedError,
73
+ SchemaVersionAheadError,
74
+ StorageBackendMismatchError,
75
+ StorageDurabilityError,
76
+ StorageInUseError,
77
+ StoreNotOpenError,
78
+ UnsyncedDataError,
79
+ WorkerInitError,
80
+ WorkerTimeoutError
81
+ } from "./chunk-DUKTWVZW.js";
82
+
83
+ // src/persistence/storage-persistence.ts
84
+ function defaultStorageManager() {
85
+ const nav = globalThis.navigator;
86
+ return nav?.storage ?? null;
87
+ }
88
+ function errorMessage(error) {
89
+ return error instanceof Error ? error.message : String(error);
90
+ }
91
+ var StoragePersistence = class {
92
+ current = {
93
+ state: "unknown",
94
+ persisted: false,
95
+ requested: false
96
+ };
97
+ inFlightRequest = null;
98
+ emitter;
99
+ storageOverride;
100
+ constructor(options = {}) {
101
+ this.emitter = options.emitter ?? null;
102
+ this.storageOverride = options.storage;
2861
103
  }
2862
- /**
2863
- * Get the sequence manager for offline-safe sequence generation.
2864
- * @throws {StoreNotOpenError} If the store is not open
2865
- */
2866
- getSequenceManager() {
2867
- this.ensureOpen();
2868
- if (!this.sequenceManager) {
2869
- throw new StoreNotOpenError();
2870
- }
2871
- return this.sequenceManager;
104
+ /** Synchronous snapshot of the last known state. Never prompts. */
105
+ status() {
106
+ return { ...this.current };
2872
107
  }
2873
108
  /**
2874
- * Create a TransactionContext for atomic multi-collection operations.
2875
- * The returned context buffers all mutations and commits them atomically.
2876
- *
2877
- * After commit, the caller is responsible for notifying subscriptions
2878
- * and emitting events for each operation.
109
+ * Read whether storage is already persistent (`navigator.storage.persisted()`).
110
+ * Never prompts and never throws: a failure becomes `state: 'error'`.
2879
111
  */
2880
- createTransaction() {
2881
- this.ensureOpen();
2882
- if (!this.clock) {
2883
- throw new StoreNotOpenError();
112
+ async check() {
113
+ const storage = this.storage();
114
+ if (!storage || typeof storage.persisted !== "function") {
115
+ this.update({ state: "unsupported", persisted: false });
116
+ if (storage) this.emit("unsupported", false);
117
+ return this.status();
2884
118
  }
2885
- return new TransactionContext({
2886
- schema: this.schema,
2887
- adapter: this.adapter,
2888
- clock: this.clock,
2889
- nodeId: this.nodeId,
2890
- sequenceAllocator: new TransactionSequenceAllocator(this.adapter, this.nodeId),
2891
- relationEnforcer: this.relationEnforcer,
2892
- causalTracker: this.causalTracker,
2893
- localMutationHandler: this.localMutationHandler
2894
- });
2895
- }
2896
- /**
2897
- * Execute a function within a transaction. All mutations performed on the
2898
- * TransactionContext are committed atomically. Subscription notifications
2899
- * are batched and fired after the commit.
2900
- *
2901
- * If the function throws, the transaction is rolled back and the error is re-thrown.
2902
- *
2903
- * @param fn - Function receiving a TransactionContext for buffered operations
2904
- * @returns The operations that were committed
2905
- */
2906
- async transaction(fn) {
2907
- const tx = this.createTransaction();
2908
- this.causalTracker?.beginTransaction();
2909
119
  try {
2910
- await fn(tx);
2911
- const { operations } = await tx.commit();
2912
- this.causalTracker?.clearTransaction();
2913
- for (const op of operations) {
2914
- this.recordOperationSequence(op);
2915
- this.subscriptionManager.notify(op.collection, op);
2916
- if (this.emitter) {
2917
- this.emitter.emit({ type: "operation:created", operation: op });
2918
- }
120
+ const persisted = await storage.persisted() === true;
121
+ if (!this.current.persisted) {
122
+ this.update({ state: persisted ? "persisted" : "best-effort", persisted });
2919
123
  }
2920
- return operations;
124
+ this.emit("checked", this.current.persisted);
2921
125
  } catch (error) {
2922
- tx.rollback();
2923
- this.causalTracker?.clearTransaction();
2924
- throw error;
126
+ this.update({ state: "error", persisted: false, lastError: errorMessage(error) });
127
+ this.emit("error", false, errorMessage(error));
2925
128
  }
129
+ return this.status();
2926
130
  }
2927
131
  /**
2928
- * Export all data as a portable backup binary.
2929
- * Includes operations, version vector, metadata, and optionally materialized records.
2930
- *
2931
- * @param options - Backup options (includeRecords, collections, onProgress)
2932
- * @returns Backup as a Uint8Array
2933
- */
2934
- async exportBackup(options) {
2935
- this.ensureOpen();
2936
- const { exportBackup: doExport } = await import("./backup-5S7K2Z35.js");
2937
- return doExport(this.adapter, this.schema, this.nodeId, this.schema.version, options);
2938
- }
2939
- /**
2940
- * Restore data from a backup binary.
132
+ * Ask the browser for persistent storage (`navigator.storage.persist()`).
133
+ * May show a permission prompt (Firefox), so call it from a user gesture or a
134
+ * meaningful moment, and do not block rendering on it. Concurrent calls share
135
+ * one request. Never throws: a failure becomes `state: 'error'`.
2941
136
  *
2942
- * @param data - The backup data
2943
- * @param options - Restore options (merge, collections, onProgress)
2944
- * @returns Result of the restore operation
2945
- */
2946
- async importBackup(data, options) {
2947
- this.ensureOpen();
2948
- const { restoreBackup: doRestore } = await import("./backup-5S7K2Z35.js");
2949
- return doRestore(this.adapter, this.schema, data, options);
137
+ * @returns The state after the browser answered
138
+ */
139
+ request() {
140
+ if (this.inFlightRequest) return this.inFlightRequest;
141
+ const storage = this.storage();
142
+ if (!storage || typeof storage.persist !== "function") {
143
+ this.update({ state: "unsupported", persisted: false });
144
+ if (storage) this.emit("unsupported", false);
145
+ return Promise.resolve(this.status());
146
+ }
147
+ if (this.current.persisted) return Promise.resolve(this.status());
148
+ const persist = storage.persist.bind(storage);
149
+ this.current = { ...this.current, requested: true };
150
+ const pending = (async () => {
151
+ try {
152
+ const persisted = await persist() === true;
153
+ this.update({ state: persisted ? "persisted" : "best-effort", persisted });
154
+ this.emit("requested", persisted);
155
+ } catch (error) {
156
+ this.update({ state: "error", persisted: false, lastError: errorMessage(error) });
157
+ this.emit("error", false, errorMessage(error));
158
+ } finally {
159
+ this.inFlightRequest = null;
160
+ }
161
+ return this.status();
162
+ })();
163
+ this.inFlightRequest = pending;
164
+ return pending;
2950
165
  }
2951
166
  /**
2952
- * Read backup manifest without loading the entire backup.
167
+ * Fire-and-forget `request()` for an automatic trigger. Never awaited by Kora,
168
+ * so a pending prompt cannot block anything; the outcome arrives as a
169
+ * `storage:persistence` event. Skipped when storage is already persistent or a
170
+ * request was already made this session.
2953
171
  *
2954
- * @param data - The raw backup data
2955
- * @returns The backup manifest
172
+ * @param _reason - Which trigger fired (kept for tracing at call sites)
2956
173
  */
2957
- static readBackupManifest(data) {
2958
- return readBackupManifest(data);
2959
- }
2960
- recordOperationSequence(operation) {
2961
- const prev = this.versionVector.get(operation.nodeId) ?? 0;
2962
- if (operation.sequenceNumber > prev) {
2963
- this.versionVector.set(operation.nodeId, operation.sequenceNumber);
2964
- }
2965
- if (operation.nodeId === this.nodeId) {
2966
- this.sequenceNumber = Math.max(this.sequenceNumber, operation.sequenceNumber);
2967
- }
2968
- }
2969
- async allocateSequenceNumber() {
2970
- const seq = await allocateNextSequenceNumber(this.adapter, this.nodeId);
2971
- this.sequenceNumber = seq;
2972
- this.versionVector.set(this.nodeId, seq);
2973
- return seq;
174
+ requestInBackground(_reason) {
175
+ if (this.current.persisted || this.current.requested) return;
176
+ void this.request();
2974
177
  }
2975
- /**
2976
- * Check the stored schema version and run any pending migrations.
2977
- * Migrations are applied in version order within a transaction.
2978
- */
2979
- async runMigrationsIfNeeded() {
2980
- const storedVersion = await this.getStoredSchemaVersion();
2981
- const targetVersion = this.schema.version;
2982
- if (storedVersion >= targetVersion) {
2983
- if (storedVersion === 0) {
2984
- await this.adapter.execute(
2985
- "INSERT OR REPLACE INTO _kora_meta (key, value) VALUES ('schema_version', ?)",
2986
- [String(targetVersion)]
2987
- );
2988
- }
2989
- return;
2990
- }
2991
- const migrations = this.schema.migrations ?? {};
2992
- for (let v = storedVersion + 1; v <= targetVersion; v++) {
2993
- const migration = migrations[v];
2994
- if (!migration) continue;
2995
- const sqlStatements = migrationStepsToSQL(migration.steps);
2996
- for (const sql of sqlStatements) {
2997
- try {
2998
- await this.adapter.execute(sql);
2999
- } catch (e) {
3000
- const msg = e.message || "";
3001
- if (!msg.includes("duplicate column name")) {
3002
- throw e;
3003
- }
3004
- }
3005
- }
3006
- const backfillSteps = migration.steps.filter(
3007
- (s) => s.type === "backfill"
3008
- );
3009
- for (const step of backfillSteps) {
3010
- await this.runBackfill(step.collection, step.transform);
3011
- }
3012
- }
3013
- await this.adapter.execute(
3014
- "INSERT OR REPLACE INTO _kora_meta (key, value) VALUES ('schema_version', ?)",
3015
- [String(targetVersion)]
3016
- );
178
+ storage() {
179
+ return this.storageOverride === void 0 ? defaultStorageManager() : this.storageOverride;
3017
180
  }
3018
- /**
3019
- * Get the stored schema version from _kora_meta. Returns 0 if not set.
3020
- */
3021
- async getStoredSchemaVersion() {
3022
- const rows = await this.adapter.query(
3023
- "SELECT value FROM _kora_meta WHERE key = 'schema_version'"
3024
- );
3025
- return rows[0] ? Number(rows[0].value) : 0;
181
+ update(next) {
182
+ this.current = { ...next, requested: this.current.requested };
3026
183
  }
3027
- /**
3028
- * Run a backfill transform on all records in a collection.
3029
- * Reads all rows, applies the transform, and updates changed fields.
3030
- */
3031
- async runBackfill(collection, transform) {
3032
- const rows = await this.adapter.query(
3033
- `SELECT * FROM ${quoteIdent7(collection)} WHERE _deleted = 0`
3034
- );
3035
- await this.adapter.transaction(async (tx) => {
3036
- for (const row of rows) {
3037
- const updates = transform(row);
3038
- const fields = Object.keys(updates);
3039
- if (fields.length === 0) continue;
3040
- const setClauses = fields.map((f) => `${quoteIdent7(f)} = ?`).join(", ");
3041
- const values = fields.map((f) => {
3042
- const val = updates[f];
3043
- if (typeof val === "boolean") return val ? 1 : 0;
3044
- if (Array.isArray(val) || typeof val === "object" && val !== null) {
3045
- return JSON.stringify(val);
3046
- }
3047
- return val;
3048
- });
3049
- values.push(row.id);
3050
- await tx.execute(`UPDATE ${quoteIdent7(collection)} SET ${setClauses} WHERE id = ?`, values);
3051
- }
184
+ emit(state, persisted, message) {
185
+ this.emitter?.emit({
186
+ type: "storage:persistence",
187
+ state,
188
+ persisted,
189
+ ...message !== void 0 ? { message } : {}
3052
190
  });
3053
191
  }
3054
- async loadOrGenerateNodeId() {
3055
- if (this.configNodeId) {
3056
- if (this.isolation !== "per-tab") {
3057
- await this.adapter.execute(
3058
- "INSERT OR REPLACE INTO _kora_meta (key, value) VALUES ('node_id', ?)",
3059
- [this.configNodeId]
3060
- );
3061
- }
3062
- return this.configNodeId;
3063
- }
3064
- if (this.isolation === "per-tab") {
3065
- return resolvePerTabNodeId(this.dbName);
3066
- }
3067
- const rows = await this.adapter.query(
3068
- "SELECT value FROM _kora_meta WHERE key = 'node_id'"
3069
- );
3070
- if (rows[0]) {
3071
- return rows[0].value;
3072
- }
3073
- const newNodeId = generateUUIDv73();
3074
- await this.adapter.execute("INSERT INTO _kora_meta (key, value) VALUES ('node_id', ?)", [
3075
- newNodeId
3076
- ]);
3077
- return newNodeId;
3078
- }
3079
- async loadSequenceNumber() {
3080
- const rows = await this.adapter.query(
3081
- "SELECT sequence_number FROM _kora_version_vector WHERE node_id = ?",
3082
- [this.nodeId]
3083
- );
3084
- return rows[0]?.sequence_number ?? 0;
3085
- }
3086
- async loadVersionVector() {
3087
- const rows = await this.adapter.query(
3088
- "SELECT node_id, sequence_number FROM _kora_version_vector"
3089
- );
3090
- const vector = createVersionVector3();
3091
- for (const row of rows) {
3092
- vector.set(row.node_id, row.sequence_number);
3093
- }
3094
- return vector;
3095
- }
3096
- ensureOpen() {
3097
- if (!this.opened) {
3098
- throw new StoreNotOpenError();
3099
- }
3100
- }
3101
192
  };
3102
193
 
3103
194
  // src/reactivity/query-store.ts
@@ -3108,6 +199,7 @@ var QueryStore = class {
3108
199
  unsubscribeQuery = null;
3109
200
  active = false;
3110
201
  hasEmittedSnapshot = false;
202
+ error = null;
3111
203
  queryBuilder;
3112
204
  constructor(queryBuilder) {
3113
205
  this.queryBuilder = queryBuilder;
@@ -3140,21 +232,42 @@ var QueryStore = class {
3140
232
  hasSnapshot = () => {
3141
233
  return this.hasEmittedSnapshot;
3142
234
  };
235
+ /**
236
+ * The latest query failure, or null. Set when the query (or a re-run after a
237
+ * write) fails; cleared by the next successful delivery. Listeners are notified
238
+ * on both transitions, so bindings read it next to `getSnapshot()`.
239
+ */
240
+ getError = () => {
241
+ return this.error;
242
+ };
3143
243
  /** Tear down listeners and the underlying query subscription. */
3144
244
  destroy() {
3145
245
  this.stopSubscription();
3146
246
  this.listeners.clear();
3147
247
  this.snapshot = EMPTY_ARRAY;
3148
248
  this.hasEmittedSnapshot = false;
249
+ this.error = null;
3149
250
  }
3150
251
  startSubscription() {
3151
252
  this.active = true;
3152
- this.unsubscribeQuery = this.queryBuilder.subscribe((results) => {
3153
- if (!this.active) return;
3154
- this.hasEmittedSnapshot = true;
3155
- this.snapshot = Object.freeze([...results]);
3156
- this.notifyListeners();
3157
- });
253
+ this.unsubscribeQuery = this.queryBuilder.subscribe(
254
+ (results) => {
255
+ if (!this.active) return;
256
+ this.hasEmittedSnapshot = true;
257
+ this.error = null;
258
+ this.snapshot = Object.freeze([...results]);
259
+ this.notifyListeners();
260
+ },
261
+ {
262
+ // STORE-12: the failure reaches bindings as error state; the last
263
+ // snapshot stays readable and the next success clears the error.
264
+ onError: (failure) => {
265
+ if (!this.active) return;
266
+ this.error = failure.error;
267
+ this.notifyListeners();
268
+ }
269
+ }
270
+ );
3158
271
  }
3159
272
  stopSubscription() {
3160
273
  this.active = false;
@@ -3225,7 +338,7 @@ var QueryStoreCache = class {
3225
338
  return this.entries.size;
3226
339
  }
3227
340
  getKey(queryBuilder) {
3228
- return `${this.scopeKey}:${JSON.stringify(queryBuilder.getDescriptor())}`;
341
+ return `${this.scopeKey}:${queryKey(queryBuilder.getDescriptor())}`;
3229
342
  }
3230
343
  };
3231
344
  var sharedCache = null;
@@ -3645,12 +758,6 @@ function isNotFoundError(error) {
3645
758
  }
3646
759
  async function createOpfsBlobDirectory(rootDirName = "kora-blobs") {
3647
760
  const storage = getStorageManager();
3648
- if (typeof storage.persist === "function") {
3649
- try {
3650
- await storage.persist();
3651
- } catch {
3652
- }
3653
- }
3654
761
  const opfsRoot = await storage.getDirectory();
3655
762
  const root = await opfsRoot.getDirectoryHandle(rootDirName, { create: true });
3656
763
  async function shardHandle(shard, create) {
@@ -4012,10 +1119,19 @@ async function collectBlobGarbage(store, liveRefs, options = {}) {
4012
1119
  }
4013
1120
 
4014
1121
  // src/blob/blob-chunk-transport.ts
4015
- import { generateUUIDv7 as generateUUIDv74 } from "@korajs/core";
1122
+ import { generateUUIDv7 } from "@korajs/core";
4016
1123
  function createRemoteChunkProvider(port, options = {}) {
4017
1124
  const timeoutMs = options.timeoutMs ?? 3e4;
1125
+ const maxThrottleWaitMs = options.maxThrottleWaitMs ?? 10 * 6e4;
1126
+ const minDelayMs = options.minThrottleDelayMs ?? 250;
1127
+ const maxDelayMs = options.maxThrottleDelayMs ?? 3e4;
4018
1128
  const pending = /* @__PURE__ */ new Map();
1129
+ const armTimeout = (requestId, hash) => setTimeout(() => {
1130
+ const entry = pending.get(requestId);
1131
+ if (!entry) return;
1132
+ pending.delete(requestId);
1133
+ entry.reject(new Error(`Blob chunk request for ${hash} timed out after ${timeoutMs}ms`));
1134
+ }, timeoutMs);
4019
1135
  port.onMessage((message) => {
4020
1136
  if (message.type !== "blob-chunk-response") {
4021
1137
  return;
@@ -4024,20 +1140,43 @@ function createRemoteChunkProvider(port, options = {}) {
4024
1140
  if (!entry) {
4025
1141
  return;
4026
1142
  }
4027
- pending.delete(message.requestId);
4028
1143
  clearTimeout(entry.timer);
1144
+ if (message.throttled === true) {
1145
+ entry.throttledCount += 1;
1146
+ const backoff = Math.min(maxDelayMs, minDelayMs * 2 ** (entry.throttledCount - 1));
1147
+ const delay = Math.min(maxDelayMs, Math.max(minDelayMs, message.retryAfterMs ?? backoff));
1148
+ if (Date.now() + delay - entry.startedAtMs > maxThrottleWaitMs) {
1149
+ pending.delete(message.requestId);
1150
+ entry.reject(
1151
+ new Error(
1152
+ `Blob chunk request for ${entry.hash} was throttled for longer than ${maxThrottleWaitMs}ms`
1153
+ )
1154
+ );
1155
+ return;
1156
+ }
1157
+ entry.timer = setTimeout(() => {
1158
+ if (pending.get(message.requestId) !== entry) return;
1159
+ entry.timer = armTimeout(message.requestId, entry.hash);
1160
+ port.send({ type: "blob-chunk-request", requestId: message.requestId, hash: entry.hash });
1161
+ }, delay);
1162
+ return;
1163
+ }
1164
+ pending.delete(message.requestId);
4029
1165
  entry.resolve(message.bytes);
4030
1166
  });
4031
1167
  return {
4032
1168
  pendingCount: () => pending.size,
4033
1169
  getChunk(hash) {
4034
- const requestId = generateUUIDv74();
1170
+ const requestId = generateUUIDv7();
4035
1171
  return new Promise((resolve, reject) => {
4036
- const timer = setTimeout(() => {
4037
- pending.delete(requestId);
4038
- reject(new Error(`Blob chunk request for ${hash} timed out after ${timeoutMs}ms`));
4039
- }, timeoutMs);
4040
- pending.set(requestId, { resolve, reject, timer });
1172
+ pending.set(requestId, {
1173
+ hash,
1174
+ resolve,
1175
+ reject,
1176
+ timer: armTimeout(requestId, hash),
1177
+ startedAtMs: Date.now(),
1178
+ throttledCount: 0
1179
+ });
4041
1180
  port.send({ type: "blob-chunk-request", requestId, hash });
4042
1181
  });
4043
1182
  }
@@ -4089,12 +1228,17 @@ function createChunkPortPair() {
4089
1228
  }
4090
1229
  export {
4091
1230
  AdapterError,
1231
+ BACKUP_VERSION,
1232
+ BackupFormatError,
4092
1233
  BlobIntegrityError,
1234
+ BridgeTerminatedError,
4093
1235
  COMPACTION_BASELINE_META_KEY,
4094
1236
  Collection,
4095
1237
  DEFAULT_CHUNK_SIZE,
4096
1238
  InvalidStateTransitionError,
4097
1239
  LAST_ACKED_SERVER_VECTOR_META_KEY,
1240
+ LOG_QUARANTINE_TABLE,
1241
+ LeaderUnresponsiveError,
4098
1242
  MemoryBlobStore,
4099
1243
  NoLeaderError,
4100
1244
  OpfsBlobStore,
@@ -4105,12 +1249,21 @@ export {
4105
1249
  QueryStore,
4106
1250
  QueryStoreCache,
4107
1251
  RecordNotFoundError,
1252
+ RequestAbortedError,
1253
+ SchemaVersionAheadError,
4108
1254
  SequenceManager,
1255
+ StorageBackendMismatchError,
1256
+ StorageDurabilityError,
1257
+ StorageInUseError,
1258
+ StoragePersistence,
4109
1259
  Store,
4110
1260
  StoreNotOpenError,
4111
1261
  SubscriptionBloomFilter,
4112
1262
  SubscriptionManager,
4113
1263
  TransactionContext,
1264
+ UnsyncedDataError,
1265
+ VIRTUAL_TIMESTAMP_FIELDS,
1266
+ VIRTUAL_TIMESTAMP_FIELD_NAMES,
4114
1267
  WorkerInitError,
4115
1268
  WorkerTimeoutError,
4116
1269
  asRichTextSyncEngine,
@@ -4120,6 +1273,7 @@ export {
4120
1273
  collectOperationsAheadOfServer,
4121
1274
  compactOperationLog,
4122
1275
  computeAckCompactionWatermark,
1276
+ convertBackupV1,
4123
1277
  createChunkPortPair,
4124
1278
  createMemoryServerBlobStore,
4125
1279
  createOpfsBlobDirectory,
@@ -4135,11 +1289,14 @@ export {
4135
1289
  fetchBlobManifest,
4136
1290
  getSharedQueryStoreCache,
4137
1291
  mergeVersionVectors,
1292
+ mergeYjsUpdates,
1293
+ normalizeWhere,
4138
1294
  parseBlobManifest,
4139
1295
  persistedAuditTraceFromEvent,
4140
1296
  pluralize,
4141
1297
  prepareBlobForSend,
4142
1298
  putBlobForTransfer,
1299
+ queryKey,
4143
1300
  readAuditExportManifest,
4144
1301
  readBackupManifest,
4145
1302
  reassembleBlob,