@korajs/store 1.0.0-beta.1 → 1.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/better-sqlite3.cjs.map +1 -1
- package/dist/adapters/better-sqlite3.js +1 -1
- package/dist/adapters/indexeddb.cjs +196 -39
- package/dist/adapters/indexeddb.cjs.map +1 -1
- package/dist/adapters/indexeddb.d.cts +1 -1
- package/dist/adapters/indexeddb.d.ts +1 -1
- package/dist/adapters/indexeddb.js +15 -8
- package/dist/adapters/indexeddb.js.map +1 -1
- package/dist/adapters/sqlite-wasm-shared-host.cjs +80 -53
- package/dist/adapters/sqlite-wasm-shared-host.cjs.map +1 -1
- package/dist/adapters/sqlite-wasm-shared-host.d.cts +63 -1
- package/dist/adapters/sqlite-wasm-shared-host.d.ts +63 -1
- package/dist/adapters/sqlite-wasm-shared-host.js +56 -52
- package/dist/adapters/sqlite-wasm-shared-host.js.map +1 -1
- package/dist/adapters/sqlite-wasm-worker.cjs +25 -3
- package/dist/adapters/sqlite-wasm-worker.cjs.map +1 -1
- package/dist/adapters/sqlite-wasm-worker.js +25 -3
- package/dist/adapters/sqlite-wasm-worker.js.map +1 -1
- package/dist/adapters/sqlite-wasm.cjs +186 -34
- package/dist/adapters/sqlite-wasm.cjs.map +1 -1
- package/dist/adapters/sqlite-wasm.d.cts +29 -3
- package/dist/adapters/sqlite-wasm.d.ts +29 -3
- package/dist/adapters/sqlite-wasm.js +3 -3
- package/dist/{backup-ZV4Y424B.js → backup-IQE4QC7Q.js} +2 -2
- package/dist/{chunk-RBPVEAVM.js → chunk-2ZRQGFEF.js} +179 -36
- package/dist/chunk-2ZRQGFEF.js.map +1 -0
- package/dist/{chunk-IOEFNHHP.js → chunk-COJSQBXW.js} +2 -2
- package/dist/{chunk-X57MWW35.js → chunk-OCOGQRHW.js} +12 -9
- package/dist/chunk-OCOGQRHW.js.map +1 -0
- package/dist/{chunk-FFYA7J5H.js → chunk-RCTG3IJG.js} +3 -2
- package/dist/chunk-RCTG3IJG.js.map +1 -0
- package/dist/{chunk-QL2XVZRN.js → chunk-TUR7RGEP.js} +12 -1
- package/dist/{chunk-QL2XVZRN.js.map → chunk-TUR7RGEP.js.map} +1 -1
- package/dist/{chunk-ZLWAYQEQ.js → chunk-UXRWVXBP.js} +32 -28
- package/dist/chunk-UXRWVXBP.js.map +1 -0
- package/dist/{export-audit-AJ7RORNK.js → export-audit-MT2LTMIN.js} +2 -2
- package/dist/index.cjs +167 -141
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -1
- package/dist/index.d.ts +10 -1
- package/dist/index.js +51 -41
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs +45 -40
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.js +2 -2
- package/dist/{sqlite-wasm-channel-DUDPclTE.d.cts → sqlite-wasm-channel-CNHRf-m2.d.cts} +1 -1
- package/dist/{sqlite-wasm-channel-DUDPclTE.d.ts → sqlite-wasm-channel-CNHRf-m2.d.ts} +1 -1
- package/dist/sqlite-wasm-channel-NGSBJR44.js +12 -0
- package/package.json +2 -2
- package/dist/chunk-FFYA7J5H.js.map +0 -1
- package/dist/chunk-RBPVEAVM.js.map +0 -1
- package/dist/chunk-X57MWW35.js.map +0 -1
- package/dist/chunk-ZLWAYQEQ.js.map +0 -1
- package/dist/sqlite-wasm-channel-H43KEZLM.js +0 -12
- /package/dist/{backup-ZV4Y424B.js.map → backup-IQE4QC7Q.js.map} +0 -0
- /package/dist/{chunk-IOEFNHHP.js.map → chunk-COJSQBXW.js.map} +0 -0
- /package/dist/{export-audit-AJ7RORNK.js.map → export-audit-MT2LTMIN.js.map} +0 -0
- /package/dist/{sqlite-wasm-channel-H43KEZLM.js.map → sqlite-wasm-channel-NGSBJR44.js.map} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -528,7 +528,7 @@ async function restoreBackup(adapter, schema, data, options) {
|
|
|
528
528
|
schema_version: op.schemaVersion
|
|
529
529
|
};
|
|
530
530
|
await tx.execute(
|
|
531
|
-
`INSERT OR IGNORE INTO _kora_ops_${collection} (id, node_id, type, record_id, data, previous_data, timestamp, sequence_number, causal_deps, schema_version) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
531
|
+
`INSERT OR IGNORE INTO ${(0, import_core6.quoteIdent)(`_kora_ops_${collection}`)} (id, node_id, type, record_id, data, previous_data, timestamp, sequence_number, causal_deps, schema_version) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
532
532
|
[
|
|
533
533
|
opRow.id,
|
|
534
534
|
opRow.node_id,
|
|
@@ -547,8 +547,8 @@ async function restoreBackup(adapter, schema, data, options) {
|
|
|
547
547
|
} else {
|
|
548
548
|
await adapter.transaction(async (tx) => {
|
|
549
549
|
for (const collection of Object.keys(schema.collections)) {
|
|
550
|
-
await tx.execute(`DELETE FROM _kora_ops_${collection}`);
|
|
551
|
-
await tx.execute(`DELETE FROM ${collection}`);
|
|
550
|
+
await tx.execute(`DELETE FROM ${(0, import_core6.quoteIdent)(`_kora_ops_${collection}`)}`);
|
|
551
|
+
await tx.execute(`DELETE FROM ${(0, import_core6.quoteIdent)(collection)}`);
|
|
552
552
|
}
|
|
553
553
|
await tx.execute("DELETE FROM _kora_version_vector");
|
|
554
554
|
await tx.execute("DELETE FROM _kora_meta");
|
|
@@ -568,7 +568,7 @@ async function restoreBackup(adapter, schema, data, options) {
|
|
|
568
568
|
for (const op of operations) {
|
|
569
569
|
const collection = op.collection;
|
|
570
570
|
await tx.execute(
|
|
571
|
-
`INSERT INTO _kora_ops_${collection} (id, node_id, type, record_id, data, previous_data, timestamp, sequence_number, causal_deps, schema_version) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
571
|
+
`INSERT INTO ${(0, import_core6.quoteIdent)(`_kora_ops_${collection}`)} (id, node_id, type, record_id, data, previous_data, timestamp, sequence_number, causal_deps, schema_version) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
572
572
|
[
|
|
573
573
|
op.id,
|
|
574
574
|
op.nodeId,
|
|
@@ -600,6 +600,7 @@ async function restoreBackup(adapter, schema, data, options) {
|
|
|
600
600
|
for (const record of records) {
|
|
601
601
|
const keys = Object.keys(record);
|
|
602
602
|
const placeholders = keys.map(() => "?").join(", ");
|
|
603
|
+
const quotedKeys = keys.map((k) => (0, import_core6.quoteIdent)(k)).join(", ");
|
|
603
604
|
const values = keys.map((k) => {
|
|
604
605
|
const v = record[k];
|
|
605
606
|
if (typeof v === "boolean") return v ? 1 : 0;
|
|
@@ -607,7 +608,7 @@ async function restoreBackup(adapter, schema, data, options) {
|
|
|
607
608
|
return v;
|
|
608
609
|
});
|
|
609
610
|
await tx.execute(
|
|
610
|
-
`INSERT OR REPLACE INTO ${col} (${
|
|
611
|
+
`INSERT OR REPLACE INTO ${(0, import_core6.quoteIdent)(col)} (${quotedKeys}) VALUES (${placeholders})`,
|
|
611
612
|
values
|
|
612
613
|
);
|
|
613
614
|
}
|
|
@@ -640,7 +641,7 @@ async function readAllOperations(adapter, schema) {
|
|
|
640
641
|
const allOps = [];
|
|
641
642
|
for (const collectionName of Object.keys(schema.collections)) {
|
|
642
643
|
const rows = await adapter.query(
|
|
643
|
-
`SELECT * FROM _kora_ops_${collectionName} ORDER BY sequence_number ASC`
|
|
644
|
+
`SELECT * FROM ${(0, import_core6.quoteIdent)(`_kora_ops_${collectionName}`)} ORDER BY sequence_number ASC`
|
|
644
645
|
);
|
|
645
646
|
for (const row of rows) {
|
|
646
647
|
allOps.push(deserializeOperationWithCollection(row, collectionName));
|
|
@@ -649,7 +650,9 @@ async function readAllOperations(adapter, schema) {
|
|
|
649
650
|
return allOps;
|
|
650
651
|
}
|
|
651
652
|
async function readCollectionRecords(adapter, collection) {
|
|
652
|
-
return adapter.query(
|
|
653
|
+
return adapter.query(
|
|
654
|
+
`SELECT * FROM ${(0, import_core6.quoteIdent)(collection)} WHERE _deleted = 0`
|
|
655
|
+
);
|
|
653
656
|
}
|
|
654
657
|
async function computeSha256(data) {
|
|
655
658
|
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
@@ -717,7 +720,7 @@ __export(audit_trace_store_exports, {
|
|
|
717
720
|
});
|
|
718
721
|
function persistedAuditTraceFromEvent(event) {
|
|
719
722
|
return {
|
|
720
|
-
id: (0,
|
|
723
|
+
id: (0, import_core21.generateUUIDv7)(),
|
|
721
724
|
recordedAt: Date.now(),
|
|
722
725
|
eventType: event.type,
|
|
723
726
|
constraint: event.type === "constraint:violated" ? event.constraint : void 0,
|
|
@@ -783,11 +786,11 @@ function auditTraceIndexFields(trace) {
|
|
|
783
786
|
recordId: trace.operationA.recordId
|
|
784
787
|
};
|
|
785
788
|
}
|
|
786
|
-
var
|
|
789
|
+
var import_core21;
|
|
787
790
|
var init_audit_trace_store = __esm({
|
|
788
791
|
"src/audit/audit-trace-store.ts"() {
|
|
789
792
|
"use strict";
|
|
790
|
-
|
|
793
|
+
import_core21 = require("@korajs/core");
|
|
791
794
|
init_audit_json();
|
|
792
795
|
}
|
|
793
796
|
});
|
|
@@ -859,7 +862,7 @@ async function readAllOperations2(adapter, schema, collectionFilter) {
|
|
|
859
862
|
const allOps = [];
|
|
860
863
|
for (const collectionName of collections) {
|
|
861
864
|
const rows = await adapter.query(
|
|
862
|
-
`SELECT * FROM _kora_ops_${collectionName} ORDER BY sequence_number ASC`
|
|
865
|
+
`SELECT * FROM ${(0, import_core22.quoteIdent)(`_kora_ops_${collectionName}`)} ORDER BY sequence_number ASC`
|
|
863
866
|
);
|
|
864
867
|
for (const row of rows) {
|
|
865
868
|
allOps.push(deserializeOperationWithCollection(row, collectionName));
|
|
@@ -970,9 +973,11 @@ function parseNdjsonSection(content) {
|
|
|
970
973
|
}
|
|
971
974
|
return text.split("\n").map((line) => deserializeAuditJson(line));
|
|
972
975
|
}
|
|
976
|
+
var import_core22;
|
|
973
977
|
var init_export_audit = __esm({
|
|
974
978
|
"src/audit/export-audit.ts"() {
|
|
975
979
|
"use strict";
|
|
980
|
+
import_core22 = require("@korajs/core");
|
|
976
981
|
init_serializer();
|
|
977
982
|
init_audit_json();
|
|
978
983
|
init_audit_trace_store();
|
|
@@ -990,6 +995,7 @@ __export(index_exports, {
|
|
|
990
995
|
InvalidStateTransitionError: () => InvalidStateTransitionError,
|
|
991
996
|
LAST_ACKED_SERVER_VECTOR_META_KEY: () => LAST_ACKED_SERVER_VECTOR_META_KEY,
|
|
992
997
|
MemoryBlobStore: () => MemoryBlobStore,
|
|
998
|
+
NoLeaderError: () => NoLeaderError,
|
|
993
999
|
OpfsBlobStore: () => OpfsBlobStore,
|
|
994
1000
|
OptimisticLockError: () => OptimisticLockError,
|
|
995
1001
|
PersistenceError: () => PersistenceError,
|
|
@@ -1108,6 +1114,16 @@ var WorkerTimeoutError = class extends import_core.KoraError {
|
|
|
1108
1114
|
this.name = "WorkerTimeoutError";
|
|
1109
1115
|
}
|
|
1110
1116
|
};
|
|
1117
|
+
var NoLeaderError = class extends import_core.KoraError {
|
|
1118
|
+
constructor(operation) {
|
|
1119
|
+
super(
|
|
1120
|
+
`No live leader tab is answering multi-tab storage RPC for operation "${operation}"`,
|
|
1121
|
+
"NO_LEADER",
|
|
1122
|
+
{ operation }
|
|
1123
|
+
);
|
|
1124
|
+
this.name = "NoLeaderError";
|
|
1125
|
+
}
|
|
1126
|
+
};
|
|
1111
1127
|
var PersistenceError = class extends import_core.KoraError {
|
|
1112
1128
|
constructor(message, context) {
|
|
1113
1129
|
super(`Persistence error: ${message}`, "PERSISTENCE_ERROR", context);
|
|
@@ -1147,7 +1163,7 @@ async function compactOperationLog(adapter, schema, strategy, serverVector) {
|
|
|
1147
1163
|
const collectionNames = Object.keys(schema.collections);
|
|
1148
1164
|
await adapter.transaction(async (tx) => {
|
|
1149
1165
|
for (const collectionName of collectionNames) {
|
|
1150
|
-
const table = `_kora_ops_${collectionName}
|
|
1166
|
+
const table = (0, import_core2.quoteIdent)(`_kora_ops_${collectionName}`);
|
|
1151
1167
|
for (const [nodeId, maxSeq] of watermark) {
|
|
1152
1168
|
if (maxSeq <= 0) continue;
|
|
1153
1169
|
const countSql = buildCountSql(table, ageCutoffSerialized !== null);
|
|
@@ -1256,11 +1272,14 @@ async function saveDeltaCursor(adapter, cursor) {
|
|
|
1256
1272
|
}
|
|
1257
1273
|
|
|
1258
1274
|
// src/store/store.ts
|
|
1259
|
-
var
|
|
1275
|
+
var import_core23 = require("@korajs/core");
|
|
1260
1276
|
init_backup();
|
|
1261
1277
|
|
|
1278
|
+
// src/collection/collection.ts
|
|
1279
|
+
var import_core13 = require("@korajs/core");
|
|
1280
|
+
|
|
1262
1281
|
// src/mutations/execute-delete.ts
|
|
1263
|
-
var
|
|
1282
|
+
var import_core9 = require("@korajs/core");
|
|
1264
1283
|
|
|
1265
1284
|
// src/lww/row-version.ts
|
|
1266
1285
|
var import_core7 = require("@korajs/core");
|
|
@@ -1286,9 +1305,10 @@ function lwwVersionWhereClause(remoteVersion) {
|
|
|
1286
1305
|
}
|
|
1287
1306
|
|
|
1288
1307
|
// src/query/sql-builder.ts
|
|
1308
|
+
var import_core8 = require("@korajs/core");
|
|
1289
1309
|
function buildSelectQuery(descriptor, fields) {
|
|
1290
1310
|
const params = [];
|
|
1291
|
-
const parts = [`SELECT * FROM ${descriptor.collection}`];
|
|
1311
|
+
const parts = [`SELECT * FROM ${(0, import_core8.quoteIdent)(descriptor.collection)}`];
|
|
1292
1312
|
const whereClause = buildWhereClauseParts(descriptor.where, fields, params);
|
|
1293
1313
|
const deletedFilter = "_deleted = 0";
|
|
1294
1314
|
if (whereClause) {
|
|
@@ -1299,7 +1319,7 @@ function buildSelectQuery(descriptor, fields) {
|
|
|
1299
1319
|
if (descriptor.orderBy.length > 0) {
|
|
1300
1320
|
const orderParts = descriptor.orderBy.map((o) => {
|
|
1301
1321
|
validateFieldName(o.field, fields);
|
|
1302
|
-
return `${o.field} ${o.direction.toUpperCase()}`;
|
|
1322
|
+
return `${(0, import_core8.quoteIdent)(o.field)} ${o.direction.toUpperCase()}`;
|
|
1303
1323
|
});
|
|
1304
1324
|
parts.push(`ORDER BY ${orderParts.join(", ")}`);
|
|
1305
1325
|
}
|
|
@@ -1313,7 +1333,7 @@ function buildSelectQuery(descriptor, fields) {
|
|
|
1313
1333
|
}
|
|
1314
1334
|
function buildCountQuery(descriptor, fields) {
|
|
1315
1335
|
const params = [];
|
|
1316
|
-
const parts = [`SELECT COUNT(*) as count FROM ${descriptor.collection}`];
|
|
1336
|
+
const parts = [`SELECT COUNT(*) as count FROM ${(0, import_core8.quoteIdent)(descriptor.collection)}`];
|
|
1317
1337
|
const whereClause = buildWhereClauseParts(descriptor.where, fields, params);
|
|
1318
1338
|
const deletedFilter = "_deleted = 0";
|
|
1319
1339
|
if (whereClause) {
|
|
@@ -1327,17 +1347,18 @@ function buildInsertQuery(collection, record) {
|
|
|
1327
1347
|
const columns = Object.keys(record);
|
|
1328
1348
|
const placeholders = columns.map(() => "?");
|
|
1329
1349
|
const params = Object.values(record);
|
|
1330
|
-
const
|
|
1350
|
+
const quotedColumns = columns.map((col) => (0, import_core8.quoteIdent)(col));
|
|
1351
|
+
const sql = `INSERT INTO ${(0, import_core8.quoteIdent)(collection)} (${quotedColumns.join(", ")}) VALUES (${placeholders.join(", ")})`;
|
|
1331
1352
|
return { sql, params };
|
|
1332
1353
|
}
|
|
1333
1354
|
function buildUpdateQuery(collection, id, changes) {
|
|
1334
|
-
const setClauses = Object.keys(changes).map((col) => `${col} = ?`);
|
|
1355
|
+
const setClauses = Object.keys(changes).map((col) => `${(0, import_core8.quoteIdent)(col)} = ?`);
|
|
1335
1356
|
const params = [...Object.values(changes), id];
|
|
1336
|
-
const sql = `UPDATE ${collection} SET ${setClauses.join(", ")} WHERE id = ?`;
|
|
1357
|
+
const sql = `UPDATE ${(0, import_core8.quoteIdent)(collection)} SET ${setClauses.join(", ")} WHERE id = ?`;
|
|
1337
1358
|
return { sql, params };
|
|
1338
1359
|
}
|
|
1339
1360
|
function buildFieldFastForwardUpdateQuery(collection, id, fieldChanges, remoteVersion, wallTime, options) {
|
|
1340
|
-
const fieldClauses = Object.keys(fieldChanges).map((col) => `${col} = ?`);
|
|
1361
|
+
const fieldClauses = Object.keys(fieldChanges).map((col) => `${(0, import_core8.quoteIdent)(col)} = ?`);
|
|
1341
1362
|
const setClauses = [
|
|
1342
1363
|
...fieldClauses,
|
|
1343
1364
|
"_version = CASE WHEN _version >= ? THEN _version ELSE ? END",
|
|
@@ -1355,25 +1376,25 @@ function buildFieldFastForwardUpdateQuery(collection, id, fieldChanges, remoteVe
|
|
|
1355
1376
|
params.push(options.maxCreatedAt, options.maxCreatedAt);
|
|
1356
1377
|
}
|
|
1357
1378
|
params.push(id);
|
|
1358
|
-
const sql = `UPDATE ${collection} SET ${setClauses.join(", ")} WHERE id = ?`;
|
|
1379
|
+
const sql = `UPDATE ${(0, import_core8.quoteIdent)(collection)} SET ${setClauses.join(", ")} WHERE id = ?`;
|
|
1359
1380
|
return { sql, params };
|
|
1360
1381
|
}
|
|
1361
1382
|
function buildSoftDeleteQuery(collection, id, updatedAt, version) {
|
|
1362
1383
|
if (version !== void 0) {
|
|
1363
1384
|
return {
|
|
1364
|
-
sql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ?`,
|
|
1385
|
+
sql: `UPDATE ${(0, import_core8.quoteIdent)(collection)} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ?`,
|
|
1365
1386
|
params: [updatedAt, version, id]
|
|
1366
1387
|
};
|
|
1367
1388
|
}
|
|
1368
1389
|
return {
|
|
1369
|
-
sql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ? WHERE id = ?`,
|
|
1390
|
+
sql: `UPDATE ${(0, import_core8.quoteIdent)(collection)} SET _deleted = 1, _updated_at = ? WHERE id = ?`,
|
|
1370
1391
|
params: [updatedAt, id]
|
|
1371
1392
|
};
|
|
1372
1393
|
}
|
|
1373
1394
|
function buildLwwSoftDeleteQuery(collection, id, updatedAt, version) {
|
|
1374
1395
|
const lww = lwwVersionWhereClause(version);
|
|
1375
1396
|
return {
|
|
1376
|
-
sql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ? AND ${lww.sql}`,
|
|
1397
|
+
sql: `UPDATE ${(0, import_core8.quoteIdent)(collection)} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ? AND ${lww.sql}`,
|
|
1377
1398
|
params: [updatedAt, version, id, ...lww.params]
|
|
1378
1399
|
};
|
|
1379
1400
|
}
|
|
@@ -1404,31 +1425,32 @@ function buildWhereClauseParts(where, fields, params) {
|
|
|
1404
1425
|
}
|
|
1405
1426
|
function buildOperatorCondition(fieldName, operator, value, descriptor, params) {
|
|
1406
1427
|
const sqlValue = descriptor?.kind === "boolean" && typeof value === "boolean" ? value ? 1 : 0 : value;
|
|
1428
|
+
const column = (0, import_core8.quoteIdent)(fieldName);
|
|
1407
1429
|
switch (operator) {
|
|
1408
1430
|
case "$eq":
|
|
1409
1431
|
if (sqlValue === null) {
|
|
1410
|
-
return `${
|
|
1432
|
+
return `${column} IS NULL`;
|
|
1411
1433
|
}
|
|
1412
1434
|
params.push(sqlValue);
|
|
1413
|
-
return `${
|
|
1435
|
+
return `${column} = ?`;
|
|
1414
1436
|
case "$ne":
|
|
1415
1437
|
if (sqlValue === null) {
|
|
1416
|
-
return `${
|
|
1438
|
+
return `${column} IS NOT NULL`;
|
|
1417
1439
|
}
|
|
1418
1440
|
params.push(sqlValue);
|
|
1419
|
-
return `${
|
|
1441
|
+
return `${column} != ?`;
|
|
1420
1442
|
case "$gt":
|
|
1421
1443
|
params.push(sqlValue);
|
|
1422
|
-
return `${
|
|
1444
|
+
return `${column} > ?`;
|
|
1423
1445
|
case "$gte":
|
|
1424
1446
|
params.push(sqlValue);
|
|
1425
|
-
return `${
|
|
1447
|
+
return `${column} >= ?`;
|
|
1426
1448
|
case "$lt":
|
|
1427
1449
|
params.push(sqlValue);
|
|
1428
|
-
return `${
|
|
1450
|
+
return `${column} < ?`;
|
|
1429
1451
|
case "$lte":
|
|
1430
1452
|
params.push(sqlValue);
|
|
1431
|
-
return `${
|
|
1453
|
+
return `${column} <= ?`;
|
|
1432
1454
|
case "$in": {
|
|
1433
1455
|
if (!Array.isArray(sqlValue)) {
|
|
1434
1456
|
throw new QueryError(`$in operator requires an array value for field "${fieldName}"`, {
|
|
@@ -1442,7 +1464,7 @@ function buildOperatorCondition(fieldName, operator, value, descriptor, params)
|
|
|
1442
1464
|
descriptor?.kind === "boolean" && typeof item === "boolean" ? item ? 1 : 0 : item
|
|
1443
1465
|
);
|
|
1444
1466
|
}
|
|
1445
|
-
return `${
|
|
1467
|
+
return `${column} IN (${placeholders.join(", ")})`;
|
|
1446
1468
|
}
|
|
1447
1469
|
default:
|
|
1448
1470
|
throw new QueryError(`Unknown operator "${operator}"`, { operator });
|
|
@@ -1522,7 +1544,7 @@ function resolveCausalDeps(ctx) {
|
|
|
1522
1544
|
// src/mutations/execute-delete.ts
|
|
1523
1545
|
async function executeDelete(ctx, id, options) {
|
|
1524
1546
|
const currentRows = await ctx.adapter.query(
|
|
1525
|
-
`SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,
|
|
1547
|
+
`SELECT * FROM ${(0, import_core9.quoteIdent)(ctx.collection)} WHERE id = ? AND _deleted = 0`,
|
|
1526
1548
|
[id]
|
|
1527
1549
|
);
|
|
1528
1550
|
if (!currentRows[0]) {
|
|
@@ -1534,7 +1556,7 @@ async function executeDelete(ctx, id, options) {
|
|
|
1534
1556
|
await ctx.adapter.transaction(async (tx) => {
|
|
1535
1557
|
if (!operation) {
|
|
1536
1558
|
const sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId);
|
|
1537
|
-
operation = await (0,
|
|
1559
|
+
operation = await (0, import_core9.createOperation)(
|
|
1538
1560
|
{
|
|
1539
1561
|
nodeId: ctx.nodeId,
|
|
1540
1562
|
type: "delete",
|
|
@@ -1581,7 +1603,7 @@ async function executeDelete(ctx, id, options) {
|
|
|
1581
1603
|
}
|
|
1582
1604
|
|
|
1583
1605
|
// src/mutations/execute-insert.ts
|
|
1584
|
-
var
|
|
1606
|
+
var import_core10 = require("@korajs/core");
|
|
1585
1607
|
|
|
1586
1608
|
// src/lww/field-versions.ts
|
|
1587
1609
|
function parseFieldVersions(raw) {
|
|
@@ -1638,14 +1660,14 @@ function resolvePerFieldLww(current, incomingFields, incomingVersion, rowVersion
|
|
|
1638
1660
|
init_op_data_encoding();
|
|
1639
1661
|
init_serializer();
|
|
1640
1662
|
async function executeInsert(ctx, data) {
|
|
1641
|
-
const validated = (0,
|
|
1642
|
-
const recordId = (0,
|
|
1663
|
+
const validated = (0, import_core10.validateRecord)(ctx.collection, ctx.definition, data, "insert");
|
|
1664
|
+
const recordId = (0, import_core10.generateUUIDv7)();
|
|
1643
1665
|
for (const [fieldName, descriptor] of Object.entries(ctx.definition.fields)) {
|
|
1644
1666
|
if (descriptor.auto && descriptor.kind === "timestamp") {
|
|
1645
1667
|
validated[fieldName] = Date.now();
|
|
1646
1668
|
}
|
|
1647
1669
|
}
|
|
1648
|
-
const writeData = await (0,
|
|
1670
|
+
const writeData = await (0, import_core10.transformSecretFieldsForWrite)(
|
|
1649
1671
|
validated,
|
|
1650
1672
|
ctx.definition,
|
|
1651
1673
|
ctx.secretKeyProvider
|
|
@@ -1655,7 +1677,7 @@ async function executeInsert(ctx, data) {
|
|
|
1655
1677
|
let record;
|
|
1656
1678
|
await ctx.adapter.transaction(async (tx) => {
|
|
1657
1679
|
const sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId);
|
|
1658
|
-
operation = await (0,
|
|
1680
|
+
operation = await (0, import_core10.createOperation)(
|
|
1659
1681
|
{
|
|
1660
1682
|
nodeId: ctx.nodeId,
|
|
1661
1683
|
type: "insert",
|
|
@@ -1705,13 +1727,13 @@ async function executeInsert(ctx, data) {
|
|
|
1705
1727
|
}
|
|
1706
1728
|
|
|
1707
1729
|
// src/mutations/execute-update.ts
|
|
1708
|
-
var
|
|
1730
|
+
var import_core12 = require("@korajs/core");
|
|
1709
1731
|
init_op_data_encoding();
|
|
1710
1732
|
init_serializer();
|
|
1711
1733
|
|
|
1712
1734
|
// src/state-machine/state-validator.ts
|
|
1713
|
-
var
|
|
1714
|
-
var InvalidStateTransitionError = class extends
|
|
1735
|
+
var import_core11 = require("@korajs/core");
|
|
1736
|
+
var InvalidStateTransitionError = class extends import_core11.KoraError {
|
|
1715
1737
|
constructor(collection, recordId, field, fromState, toState, allowedStates) {
|
|
1716
1738
|
super(
|
|
1717
1739
|
`Invalid state transition in collection "${collection}": cannot transition field "${field}" from "${fromState}" to "${toState}". Allowed transitions from "${fromState}": ${allowedStates.length > 0 ? allowedStates.join(", ") : "(none -- terminal state)"}`,
|
|
@@ -1745,7 +1767,7 @@ function validateStateTransition(collectionName, recordId, stateMachine, current
|
|
|
1745
1767
|
collection: collectionName,
|
|
1746
1768
|
transitions: stateMachine.transitions
|
|
1747
1769
|
};
|
|
1748
|
-
const transitionResult = (0,
|
|
1770
|
+
const transitionResult = (0, import_core11.validateTransition)(constraint, currentState, newState);
|
|
1749
1771
|
if (transitionResult.valid) {
|
|
1750
1772
|
return { valid: true, allowedStates: transitionResult.allowedTargets };
|
|
1751
1773
|
}
|
|
@@ -1794,14 +1816,14 @@ function validateUpdateStateMachine(collectionName, recordId, collectionDef, cur
|
|
|
1794
1816
|
// src/mutations/execute-update.ts
|
|
1795
1817
|
async function executeUpdate(ctx, id, data) {
|
|
1796
1818
|
const currentRows = await ctx.adapter.query(
|
|
1797
|
-
`SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,
|
|
1819
|
+
`SELECT * FROM ${(0, import_core12.quoteIdent)(ctx.collection)} WHERE id = ? AND _deleted = 0`,
|
|
1798
1820
|
[id]
|
|
1799
1821
|
);
|
|
1800
1822
|
const currentRow = currentRows[0];
|
|
1801
1823
|
if (!currentRow) {
|
|
1802
1824
|
throw new RecordNotFoundError(ctx.collection, id);
|
|
1803
1825
|
}
|
|
1804
|
-
let validated = (0,
|
|
1826
|
+
let validated = (0, import_core12.validateRecord)(ctx.collection, ctx.definition, data, "update");
|
|
1805
1827
|
const currentRecord = deserializeRecord(currentRow, ctx.definition.fields);
|
|
1806
1828
|
validated = validateUpdateStateMachine(
|
|
1807
1829
|
ctx.collection,
|
|
@@ -1819,14 +1841,14 @@ async function executeUpdate(ctx, id, data) {
|
|
|
1819
1841
|
for (const key of Object.keys(validated)) {
|
|
1820
1842
|
const value = validated[key];
|
|
1821
1843
|
previousData[key] = currentRecord[key];
|
|
1822
|
-
if ((0,
|
|
1823
|
-
resolvedData[key] = (0,
|
|
1824
|
-
atomicOps[key] = (0,
|
|
1844
|
+
if ((0, import_core12.isAtomicOp)(value)) {
|
|
1845
|
+
resolvedData[key] = (0, import_core12.resolveAtomicOp)(currentRecord[key], value);
|
|
1846
|
+
atomicOps[key] = (0, import_core12.toAtomicOp)(value);
|
|
1825
1847
|
} else {
|
|
1826
1848
|
resolvedData[key] = value;
|
|
1827
1849
|
}
|
|
1828
1850
|
}
|
|
1829
|
-
const writeData = await (0,
|
|
1851
|
+
const writeData = await (0, import_core12.transformSecretFieldsForWrite)(
|
|
1830
1852
|
resolvedData,
|
|
1831
1853
|
ctx.definition,
|
|
1832
1854
|
ctx.secretKeyProvider
|
|
@@ -1836,7 +1858,7 @@ async function executeUpdate(ctx, id, data) {
|
|
|
1836
1858
|
let operation;
|
|
1837
1859
|
await ctx.adapter.transaction(async (tx) => {
|
|
1838
1860
|
const sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId);
|
|
1839
|
-
operation = await (0,
|
|
1861
|
+
operation = await (0, import_core12.createOperation)(
|
|
1840
1862
|
{
|
|
1841
1863
|
nodeId: ctx.nodeId,
|
|
1842
1864
|
type: "update",
|
|
@@ -1876,7 +1898,7 @@ async function executeUpdate(ctx, id, data) {
|
|
|
1876
1898
|
});
|
|
1877
1899
|
ctx.onMutation(ctx.collection, operation);
|
|
1878
1900
|
const rows = await ctx.adapter.query(
|
|
1879
|
-
`SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,
|
|
1901
|
+
`SELECT * FROM ${(0, import_core12.quoteIdent)(ctx.collection)} WHERE id = ? AND _deleted = 0`,
|
|
1880
1902
|
[id]
|
|
1881
1903
|
);
|
|
1882
1904
|
const row = rows[0];
|
|
@@ -1939,7 +1961,7 @@ var Collection = class {
|
|
|
1939
1961
|
}
|
|
1940
1962
|
async findById(id) {
|
|
1941
1963
|
const rows = await this.adapter.query(
|
|
1942
|
-
`SELECT * FROM ${this.name} WHERE id = ? AND _deleted = 0`,
|
|
1964
|
+
`SELECT * FROM ${(0, import_core13.quoteIdent)(this.name)} WHERE id = ? AND _deleted = 0`,
|
|
1943
1965
|
[id]
|
|
1944
1966
|
);
|
|
1945
1967
|
const row = rows[0];
|
|
@@ -1972,6 +1994,7 @@ var Collection = class {
|
|
|
1972
1994
|
};
|
|
1973
1995
|
|
|
1974
1996
|
// src/query/query-builder.ts
|
|
1997
|
+
var import_core14 = require("@korajs/core");
|
|
1975
1998
|
init_serializer();
|
|
1976
1999
|
|
|
1977
2000
|
// src/query/pluralize.ts
|
|
@@ -2203,7 +2226,7 @@ var QueryBuilder = class _QueryBuilder {
|
|
|
2203
2226
|
const relatedDef = this.schema?.collections[relatedCollection];
|
|
2204
2227
|
if (!relatedDef) return;
|
|
2205
2228
|
const placeholders = uniqueFks.map(() => "?").join(", ");
|
|
2206
|
-
const sql = `SELECT * FROM ${relatedCollection} WHERE id IN (${placeholders}) AND _deleted = 0`;
|
|
2229
|
+
const sql = `SELECT * FROM ${(0, import_core14.quoteIdent)(relatedCollection)} WHERE id IN (${placeholders}) AND _deleted = 0`;
|
|
2207
2230
|
const rows = await this.adapter.query(sql, uniqueFks);
|
|
2208
2231
|
const relatedRecords = rows.map((row) => deserializeRecord(row, relatedDef.fields));
|
|
2209
2232
|
const lookup = /* @__PURE__ */ new Map();
|
|
@@ -2226,7 +2249,7 @@ var QueryBuilder = class _QueryBuilder {
|
|
|
2226
2249
|
if (!relatedDef) return;
|
|
2227
2250
|
const fkField = relation.field;
|
|
2228
2251
|
const placeholders = primaryIds.map(() => "?").join(", ");
|
|
2229
|
-
const sql = `SELECT * FROM ${relatedCollection} WHERE ${fkField} IN (${placeholders}) AND _deleted = 0`;
|
|
2252
|
+
const sql = `SELECT * FROM ${(0, import_core14.quoteIdent)(relatedCollection)} WHERE ${(0, import_core14.quoteIdent)(fkField)} IN (${placeholders}) AND _deleted = 0`;
|
|
2230
2253
|
const rows = await this.adapter.query(sql, primaryIds);
|
|
2231
2254
|
const relatedRecords = rows.map((row) => deserializeRecord(row, relatedDef.fields));
|
|
2232
2255
|
const grouped = /* @__PURE__ */ new Map();
|
|
@@ -2268,7 +2291,7 @@ var QueryError2 = class extends Error {
|
|
|
2268
2291
|
};
|
|
2269
2292
|
|
|
2270
2293
|
// src/relations/relation-enforcer.ts
|
|
2271
|
-
var
|
|
2294
|
+
var import_core15 = require("@korajs/core");
|
|
2272
2295
|
init_serializer();
|
|
2273
2296
|
|
|
2274
2297
|
// src/relations/relation-lookup.ts
|
|
@@ -2293,7 +2316,7 @@ function getIncomingRelations(lookup, collection) {
|
|
|
2293
2316
|
}
|
|
2294
2317
|
|
|
2295
2318
|
// src/relations/relation-enforcer.ts
|
|
2296
|
-
var ReferentialIntegrityError = class extends
|
|
2319
|
+
var ReferentialIntegrityError = class extends import_core15.KoraError {
|
|
2297
2320
|
constructor(collection, recordId, referencingCollection, relationName, referencingCount) {
|
|
2298
2321
|
super(
|
|
2299
2322
|
`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.`,
|
|
@@ -2372,7 +2395,7 @@ var RelationEnforcer = class {
|
|
|
2372
2395
|
async enforceCascade(incoming, deletedRecordId, tx, causalDeps) {
|
|
2373
2396
|
const { sourceCollection, foreignKeyField } = incoming;
|
|
2374
2397
|
const referencingRows = await tx.query(
|
|
2375
|
-
`SELECT id FROM ${sourceCollection} WHERE ${foreignKeyField} = ? AND _deleted = 0`,
|
|
2398
|
+
`SELECT id FROM ${(0, import_core15.quoteIdent)(sourceCollection)} WHERE ${(0, import_core15.quoteIdent)(foreignKeyField)} = ? AND _deleted = 0`,
|
|
2376
2399
|
[deletedRecordId]
|
|
2377
2400
|
);
|
|
2378
2401
|
if (referencingRows.length === 0) {
|
|
@@ -2382,7 +2405,7 @@ var RelationEnforcer = class {
|
|
|
2382
2405
|
for (const row of referencingRows) {
|
|
2383
2406
|
const now = Date.now();
|
|
2384
2407
|
const sequenceNumber = await allocateNextSequenceInTransaction(tx, this.nodeId);
|
|
2385
|
-
const operation = await (0,
|
|
2408
|
+
const operation = await (0, import_core15.createOperation)(
|
|
2386
2409
|
{
|
|
2387
2410
|
nodeId: this.nodeId,
|
|
2388
2411
|
type: "delete",
|
|
@@ -2419,7 +2442,7 @@ var RelationEnforcer = class {
|
|
|
2419
2442
|
return [];
|
|
2420
2443
|
}
|
|
2421
2444
|
const referencingRows = await tx.query(
|
|
2422
|
-
`SELECT id FROM ${sourceCollection} WHERE ${foreignKeyField} = ? AND _deleted = 0`,
|
|
2445
|
+
`SELECT id FROM ${(0, import_core15.quoteIdent)(sourceCollection)} WHERE ${(0, import_core15.quoteIdent)(foreignKeyField)} = ? AND _deleted = 0`,
|
|
2423
2446
|
[deletedRecordId]
|
|
2424
2447
|
);
|
|
2425
2448
|
if (referencingRows.length === 0) {
|
|
@@ -2431,7 +2454,7 @@ var RelationEnforcer = class {
|
|
|
2431
2454
|
const sequenceNumber = await allocateNextSequenceInTransaction(tx, this.nodeId);
|
|
2432
2455
|
const updateData = { [foreignKeyField]: null };
|
|
2433
2456
|
const previousData = { [foreignKeyField]: deletedRecordId };
|
|
2434
|
-
const operation = await (0,
|
|
2457
|
+
const operation = await (0, import_core15.createOperation)(
|
|
2435
2458
|
{
|
|
2436
2459
|
nodeId: this.nodeId,
|
|
2437
2460
|
type: "update",
|
|
@@ -2467,7 +2490,7 @@ var RelationEnforcer = class {
|
|
|
2467
2490
|
async enforceRestrict(incoming, deletedRecordId, tx) {
|
|
2468
2491
|
const { sourceCollection, foreignKeyField, relationName } = incoming;
|
|
2469
2492
|
const countRows = await tx.query(
|
|
2470
|
-
`SELECT COUNT(*) as cnt FROM ${sourceCollection} WHERE ${foreignKeyField} = ? AND _deleted = 0`,
|
|
2493
|
+
`SELECT COUNT(*) as cnt FROM ${(0, import_core15.quoteIdent)(sourceCollection)} WHERE ${(0, import_core15.quoteIdent)(foreignKeyField)} = ? AND _deleted = 0`,
|
|
2471
2494
|
[deletedRecordId]
|
|
2472
2495
|
);
|
|
2473
2496
|
const count = countRows[0]?.cnt ?? 0;
|
|
@@ -2492,7 +2515,7 @@ var RelationEnforcer = class {
|
|
|
2492
2515
|
};
|
|
2493
2516
|
|
|
2494
2517
|
// src/replay/replay-to.ts
|
|
2495
|
-
var
|
|
2518
|
+
var import_core16 = require("@korajs/core");
|
|
2496
2519
|
var import_internal = require("@korajs/core/internal");
|
|
2497
2520
|
init_op_data_encoding();
|
|
2498
2521
|
function collectCausalClosure(allOps, targetOperationId) {
|
|
@@ -2502,7 +2525,7 @@ function collectCausalClosure(allOps, targetOperationId) {
|
|
|
2502
2525
|
}
|
|
2503
2526
|
const target = opMap.get(targetOperationId);
|
|
2504
2527
|
if (!target) {
|
|
2505
|
-
throw new
|
|
2528
|
+
throw new import_core16.OperationError(
|
|
2506
2529
|
`Operation "${targetOperationId}" not found in the local operation log`,
|
|
2507
2530
|
{
|
|
2508
2531
|
operationId: targetOperationId
|
|
@@ -2534,7 +2557,7 @@ function buildReplaySnapshot(schema, allOps, targetOperationId) {
|
|
|
2534
2557
|
const operationsApplied = collectCausalClosure(allOps, targetOperationId);
|
|
2535
2558
|
const targetOperation = operationsApplied.find((op) => op.id === targetOperationId);
|
|
2536
2559
|
if (!targetOperation) {
|
|
2537
|
-
throw new
|
|
2560
|
+
throw new import_core16.OperationError(`Operation "${targetOperationId}" not found after causal sort`, {
|
|
2538
2561
|
operationId: targetOperationId
|
|
2539
2562
|
});
|
|
2540
2563
|
}
|
|
@@ -2660,7 +2683,7 @@ function toCollectionRecord(record, definition) {
|
|
|
2660
2683
|
}
|
|
2661
2684
|
|
|
2662
2685
|
// src/sequences/sequence-manager.ts
|
|
2663
|
-
var
|
|
2686
|
+
var import_core17 = require("@korajs/core");
|
|
2664
2687
|
var SequenceManager = class {
|
|
2665
2688
|
adapter;
|
|
2666
2689
|
nodeId;
|
|
@@ -2678,7 +2701,7 @@ var SequenceManager = class {
|
|
|
2678
2701
|
async next(name, config) {
|
|
2679
2702
|
const scope = config?.scope ?? "";
|
|
2680
2703
|
const startAt = config?.startAt ?? 1;
|
|
2681
|
-
const format = config?.format ?? (0,
|
|
2704
|
+
const format = config?.format ?? (0, import_core17.defaultSequenceFormat)(name);
|
|
2682
2705
|
let counter = 0;
|
|
2683
2706
|
await this.adapter.transaction(async (tx) => {
|
|
2684
2707
|
const rows = await tx.query(
|
|
@@ -2700,7 +2723,7 @@ var SequenceManager = class {
|
|
|
2700
2723
|
);
|
|
2701
2724
|
}
|
|
2702
2725
|
});
|
|
2703
|
-
return (0,
|
|
2726
|
+
return (0, import_core17.formatSequenceValue)(format, counter, this.nodeId);
|
|
2704
2727
|
}
|
|
2705
2728
|
/**
|
|
2706
2729
|
* Get the current counter value without incrementing.
|
|
@@ -3159,10 +3182,10 @@ var SubscriptionManager = class {
|
|
|
3159
3182
|
};
|
|
3160
3183
|
|
|
3161
3184
|
// src/sync/rebase-unsynced-operations.ts
|
|
3162
|
-
var
|
|
3185
|
+
var import_core18 = require("@korajs/core");
|
|
3163
3186
|
var import_internal2 = require("@korajs/core/internal");
|
|
3164
3187
|
init_serializer();
|
|
3165
|
-
var LOGICAL_COUNTER_LIMIT =
|
|
3188
|
+
var LOGICAL_COUNTER_LIMIT = import_core18.MAX_LOGICAL + 1;
|
|
3166
3189
|
function emptyResult() {
|
|
3167
3190
|
return { operations: [], idMapping: {}, rebasedCount: 0, newMaxTimestamp: null };
|
|
3168
3191
|
}
|
|
@@ -3174,7 +3197,9 @@ async function rebaseUnsyncedOperationsInLog(adapter, schema, unsyncedOpIds, cor
|
|
|
3174
3197
|
const rebased = [];
|
|
3175
3198
|
let maxOtherSerializedTs = null;
|
|
3176
3199
|
for (const collectionName of Object.keys(schema.collections)) {
|
|
3177
|
-
const rows = await adapter.query(
|
|
3200
|
+
const rows = await adapter.query(
|
|
3201
|
+
`SELECT * FROM ${(0, import_core18.quoteIdent)(`_kora_ops_${collectionName}`)}`
|
|
3202
|
+
);
|
|
3178
3203
|
for (const row of rows) {
|
|
3179
3204
|
if (rebaseIds.has(row.id)) {
|
|
3180
3205
|
rebased.push(deserializeOperationWithCollection(row, collectionName));
|
|
@@ -3186,9 +3211,9 @@ async function rebaseUnsyncedOperationsInLog(adapter, schema, unsyncedOpIds, cor
|
|
|
3186
3211
|
if (rebased.length === 0) {
|
|
3187
3212
|
return emptyResult();
|
|
3188
3213
|
}
|
|
3189
|
-
const maxOtherWallTime = maxOtherSerializedTs === null ? Number.NEGATIVE_INFINITY :
|
|
3214
|
+
const maxOtherWallTime = maxOtherSerializedTs === null ? Number.NEGATIVE_INFINITY : import_core18.HybridLogicalClock.deserialize(maxOtherSerializedTs).wallTime;
|
|
3190
3215
|
const baseWallTime = Math.max(correctedNowMs, maxOtherWallTime + 1);
|
|
3191
|
-
rebased.sort((a, b) =>
|
|
3216
|
+
rebased.sort((a, b) => import_core18.HybridLogicalClock.compare(a.timestamp, b.timestamp));
|
|
3192
3217
|
const newTimestamps = rebased.map((op, i) => ({
|
|
3193
3218
|
// Spill into wallTime past the 5-digit logical serialization limit so the
|
|
3194
3219
|
// serialized order stays correct even for enormous offline queues.
|
|
@@ -3216,7 +3241,7 @@ async function rebaseUnsyncedOperationsInLog(adapter, schema, unsyncedOpIds, cor
|
|
|
3216
3241
|
schemaVersion: op.schemaVersion,
|
|
3217
3242
|
...op.atomicOps !== void 0 ? { atomicOps: op.atomicOps } : {}
|
|
3218
3243
|
};
|
|
3219
|
-
const newId = await (0, import_internal2.computeOperationId)(input,
|
|
3244
|
+
const newId = await (0, import_internal2.computeOperationId)(input, import_core18.HybridLogicalClock.serialize(ts));
|
|
3220
3245
|
idMapping[op.id] = newId;
|
|
3221
3246
|
newIds.push(newId);
|
|
3222
3247
|
}
|
|
@@ -3243,7 +3268,7 @@ async function rebaseUnsyncedOperationsInLog(adapter, schema, unsyncedOpIds, cor
|
|
|
3243
3268
|
continue;
|
|
3244
3269
|
}
|
|
3245
3270
|
const opsTable = `_kora_ops_${oldOp.collection}`;
|
|
3246
|
-
await tx.execute(`DELETE FROM ${opsTable} WHERE id = ?`, [oldOp.id]);
|
|
3271
|
+
await tx.execute(`DELETE FROM ${(0, import_core18.quoteIdent)(opsTable)} WHERE id = ?`, [oldOp.id]);
|
|
3247
3272
|
const opInsert = buildInsertQuery(
|
|
3248
3273
|
opsTable,
|
|
3249
3274
|
serializeOperation(newOp)
|
|
@@ -3252,11 +3277,11 @@ async function rebaseUnsyncedOperationsInLog(adapter, schema, unsyncedOpIds, cor
|
|
|
3252
3277
|
const oldVersion = serializeRowVersion(oldOp.timestamp);
|
|
3253
3278
|
const newVersion = serializeRowVersion(newOp.timestamp);
|
|
3254
3279
|
await tx.execute(
|
|
3255
|
-
`UPDATE ${oldOp.collection} SET _version = ?, _updated_at = ? WHERE id = ? AND _version = ?`,
|
|
3280
|
+
`UPDATE ${(0, import_core18.quoteIdent)(oldOp.collection)} SET _version = ?, _updated_at = ? WHERE id = ? AND _version = ?`,
|
|
3256
3281
|
[newVersion, newOp.timestamp.wallTime, oldOp.recordId, oldVersion]
|
|
3257
3282
|
);
|
|
3258
3283
|
const rows = await tx.query(
|
|
3259
|
-
`SELECT _field_versions FROM ${oldOp.collection} WHERE id = ?`,
|
|
3284
|
+
`SELECT _field_versions FROM ${(0, import_core18.quoteIdent)(oldOp.collection)} WHERE id = ?`,
|
|
3260
3285
|
[oldOp.recordId]
|
|
3261
3286
|
);
|
|
3262
3287
|
const fieldVersions = parseFieldVersions(rows[0]?._field_versions);
|
|
@@ -3268,14 +3293,14 @@ async function rebaseUnsyncedOperationsInLog(adapter, schema, unsyncedOpIds, cor
|
|
|
3268
3293
|
}
|
|
3269
3294
|
}
|
|
3270
3295
|
if (changed) {
|
|
3271
|
-
await tx.execute(
|
|
3272
|
-
|
|
3273
|
-
oldOp.recordId
|
|
3274
|
-
|
|
3296
|
+
await tx.execute(
|
|
3297
|
+
`UPDATE ${(0, import_core18.quoteIdent)(oldOp.collection)} SET _field_versions = ? WHERE id = ?`,
|
|
3298
|
+
[serializeFieldVersions(fieldVersions), oldOp.recordId]
|
|
3299
|
+
);
|
|
3275
3300
|
}
|
|
3276
3301
|
if (oldOp.type === "insert") {
|
|
3277
3302
|
await tx.execute(
|
|
3278
|
-
`UPDATE ${oldOp.collection} SET _created_at = ? WHERE id = ? AND _created_at = ?`,
|
|
3303
|
+
`UPDATE ${(0, import_core18.quoteIdent)(oldOp.collection)} SET _created_at = ? WHERE id = ? AND _created_at = ?`,
|
|
3279
3304
|
[newOp.timestamp.wallTime, oldOp.recordId, oldOp.timestamp.wallTime]
|
|
3280
3305
|
);
|
|
3281
3306
|
}
|
|
@@ -3291,7 +3316,7 @@ async function rebaseUnsyncedOperationsInLog(adapter, schema, unsyncedOpIds, cor
|
|
|
3291
3316
|
}
|
|
3292
3317
|
|
|
3293
3318
|
// src/transaction/transaction-context.ts
|
|
3294
|
-
var
|
|
3319
|
+
var import_core19 = require("@korajs/core");
|
|
3295
3320
|
init_op_data_encoding();
|
|
3296
3321
|
init_serializer();
|
|
3297
3322
|
var TransactionContext = class {
|
|
@@ -3303,7 +3328,7 @@ var TransactionContext = class {
|
|
|
3303
3328
|
config;
|
|
3304
3329
|
constructor(config) {
|
|
3305
3330
|
this.config = config;
|
|
3306
|
-
this.transactionId = (0,
|
|
3331
|
+
this.transactionId = (0, import_core19.generateUUIDv7)();
|
|
3307
3332
|
}
|
|
3308
3333
|
/**
|
|
3309
3334
|
* Set a human-readable mutation name for this transaction.
|
|
@@ -3418,8 +3443,8 @@ var TransactionContext = class {
|
|
|
3418
3443
|
}
|
|
3419
3444
|
async insert(collectionName, definition, data) {
|
|
3420
3445
|
this.ensureActive();
|
|
3421
|
-
const validated = (0,
|
|
3422
|
-
const recordId = (0,
|
|
3446
|
+
const validated = (0, import_core19.validateRecord)(collectionName, definition, data, "insert");
|
|
3447
|
+
const recordId = (0, import_core19.generateUUIDv7)();
|
|
3423
3448
|
const now = Date.now();
|
|
3424
3449
|
for (const [fieldName, descriptor] of Object.entries(definition.fields)) {
|
|
3425
3450
|
if (descriptor.auto && descriptor.kind === "timestamp") {
|
|
@@ -3428,7 +3453,7 @@ var TransactionContext = class {
|
|
|
3428
3453
|
}
|
|
3429
3454
|
const sequenceNumber = await this.config.sequenceAllocator.allocate();
|
|
3430
3455
|
const causalDeps = this.config.causalTracker?.nextCausalDeps(collectionName, true) ?? [];
|
|
3431
|
-
const operation = await (0,
|
|
3456
|
+
const operation = await (0, import_core19.createOperation)(
|
|
3432
3457
|
{
|
|
3433
3458
|
nodeId: this.config.nodeId,
|
|
3434
3459
|
type: "insert",
|
|
@@ -3483,7 +3508,7 @@ var TransactionContext = class {
|
|
|
3483
3508
|
if (!currentRecord) {
|
|
3484
3509
|
throw new RecordNotFoundError(collectionName, id);
|
|
3485
3510
|
}
|
|
3486
|
-
const validated = (0,
|
|
3511
|
+
const validated = (0, import_core19.validateRecord)(collectionName, definition, data, "update");
|
|
3487
3512
|
const now = Date.now();
|
|
3488
3513
|
const previousData = {};
|
|
3489
3514
|
const resolvedData = {};
|
|
@@ -3491,9 +3516,9 @@ var TransactionContext = class {
|
|
|
3491
3516
|
for (const key of Object.keys(validated)) {
|
|
3492
3517
|
const value = validated[key];
|
|
3493
3518
|
previousData[key] = currentRecord[key];
|
|
3494
|
-
if ((0,
|
|
3495
|
-
resolvedData[key] = (0,
|
|
3496
|
-
atomicOps[key] = (0,
|
|
3519
|
+
if ((0, import_core19.isAtomicOp)(value)) {
|
|
3520
|
+
resolvedData[key] = (0, import_core19.resolveAtomicOp)(currentRecord[key], value);
|
|
3521
|
+
atomicOps[key] = (0, import_core19.toAtomicOp)(value);
|
|
3497
3522
|
} else {
|
|
3498
3523
|
resolvedData[key] = value;
|
|
3499
3524
|
}
|
|
@@ -3501,7 +3526,7 @@ var TransactionContext = class {
|
|
|
3501
3526
|
const hasAtomicOps = Object.keys(atomicOps).length > 0;
|
|
3502
3527
|
const sequenceNumber = await this.config.sequenceAllocator.allocate();
|
|
3503
3528
|
const causalDeps = this.config.causalTracker?.nextCausalDeps(collectionName, true) ?? [];
|
|
3504
|
-
const operation = await (0,
|
|
3529
|
+
const operation = await (0, import_core19.createOperation)(
|
|
3505
3530
|
{
|
|
3506
3531
|
nodeId: this.config.nodeId,
|
|
3507
3532
|
type: "update",
|
|
@@ -3556,7 +3581,7 @@ var TransactionContext = class {
|
|
|
3556
3581
|
const now = Date.now();
|
|
3557
3582
|
const sequenceNumber = await this.config.sequenceAllocator.allocate();
|
|
3558
3583
|
const causalDeps = this.config.causalTracker?.nextCausalDeps(collectionName, true) ?? [];
|
|
3559
|
-
const operation = await (0,
|
|
3584
|
+
const operation = await (0, import_core19.createOperation)(
|
|
3560
3585
|
{
|
|
3561
3586
|
nodeId: this.config.nodeId,
|
|
3562
3587
|
type: "delete",
|
|
@@ -3606,7 +3631,7 @@ var TransactionContext = class {
|
|
|
3606
3631
|
}
|
|
3607
3632
|
}
|
|
3608
3633
|
const rows = await this.config.adapter.query(
|
|
3609
|
-
`SELECT * FROM ${collectionName} WHERE id = ? AND _deleted = 0`,
|
|
3634
|
+
`SELECT * FROM ${(0, import_core19.quoteIdent)(collectionName)} WHERE id = ? AND _deleted = 0`,
|
|
3610
3635
|
[id]
|
|
3611
3636
|
);
|
|
3612
3637
|
let record = rows[0] ? deserializeRecord(rows[0], definition.fields) : null;
|
|
@@ -3661,7 +3686,7 @@ var TransactionSequenceAllocator = class {
|
|
|
3661
3686
|
};
|
|
3662
3687
|
|
|
3663
3688
|
// src/store/tab-node-id.ts
|
|
3664
|
-
var
|
|
3689
|
+
var import_core20 = require("@korajs/core");
|
|
3665
3690
|
function perTabStorageKey(dbName) {
|
|
3666
3691
|
return `kora-tab-node-${dbName}`;
|
|
3667
3692
|
}
|
|
@@ -3682,7 +3707,7 @@ function resolvePerTabNodeId(dbName) {
|
|
|
3682
3707
|
if (existing) {
|
|
3683
3708
|
return existing;
|
|
3684
3709
|
}
|
|
3685
|
-
const nodeId = (0,
|
|
3710
|
+
const nodeId = (0, import_core20.generateUUIDv7)();
|
|
3686
3711
|
savePerTabNodeId(dbName, nodeId);
|
|
3687
3712
|
return nodeId;
|
|
3688
3713
|
}
|
|
@@ -3692,7 +3717,7 @@ var Store = class {
|
|
|
3692
3717
|
opened = false;
|
|
3693
3718
|
nodeId = "";
|
|
3694
3719
|
sequenceNumber = 0;
|
|
3695
|
-
versionVector = (0,
|
|
3720
|
+
versionVector = (0, import_core23.createVersionVector)();
|
|
3696
3721
|
clock = null;
|
|
3697
3722
|
collections = /* @__PURE__ */ new Map();
|
|
3698
3723
|
subscriptionManager;
|
|
@@ -3728,8 +3753,8 @@ var Store = class {
|
|
|
3728
3753
|
await this.adapter.open(this.schema);
|
|
3729
3754
|
await this.runMigrationsIfNeeded();
|
|
3730
3755
|
this.nodeId = await this.loadOrGenerateNodeId();
|
|
3731
|
-
this.clock = new
|
|
3732
|
-
this.causalTracker = new
|
|
3756
|
+
this.clock = new import_core23.HybridLogicalClock(this.nodeId);
|
|
3757
|
+
this.causalTracker = new import_core23.CausalTracker();
|
|
3733
3758
|
this.sequenceManager = new SequenceManager(this.adapter, this.nodeId);
|
|
3734
3759
|
this.sequenceNumber = await this.loadSequenceNumber();
|
|
3735
3760
|
this.versionVector = await this.loadVersionVector();
|
|
@@ -3841,7 +3866,7 @@ var Store = class {
|
|
|
3841
3866
|
return "skipped";
|
|
3842
3867
|
}
|
|
3843
3868
|
const existing = await this.adapter.query(
|
|
3844
|
-
`SELECT id FROM _kora_ops_${collection} WHERE id = ?`,
|
|
3869
|
+
`SELECT id FROM ${(0, import_core23.quoteIdent)(`_kora_ops_${collection}`)} WHERE id = ?`,
|
|
3845
3870
|
[op.id]
|
|
3846
3871
|
);
|
|
3847
3872
|
if (existing.length > 0) {
|
|
@@ -3946,7 +3971,7 @@ var Store = class {
|
|
|
3946
3971
|
const serializedChanges = serializeRecord(materializeSource, definition.fields);
|
|
3947
3972
|
const changedFields = Object.keys(serializedChanges);
|
|
3948
3973
|
const currentRows = await tx.query(
|
|
3949
|
-
`SELECT _version, _field_versions FROM ${collection} WHERE id = ?`,
|
|
3974
|
+
`SELECT _version, _field_versions FROM ${(0, import_core23.quoteIdent)(collection)} WHERE id = ?`,
|
|
3950
3975
|
[op.recordId]
|
|
3951
3976
|
);
|
|
3952
3977
|
const currentRow = currentRows[0];
|
|
@@ -4042,20 +4067,20 @@ var Store = class {
|
|
|
4042
4067
|
*/
|
|
4043
4068
|
async foldOrphanedOperations(tx, collection, definition, recordId) {
|
|
4044
4069
|
const orphanRows = await tx.query(
|
|
4045
|
-
`SELECT * FROM _kora_ops_${collection} WHERE record_id = ? AND type IN ('update', 'delete')`,
|
|
4070
|
+
`SELECT * FROM ${(0, import_core23.quoteIdent)(`_kora_ops_${collection}`)} WHERE record_id = ? AND type IN ('update', 'delete')`,
|
|
4046
4071
|
[recordId]
|
|
4047
4072
|
);
|
|
4048
4073
|
if (orphanRows.length === 0) {
|
|
4049
4074
|
return;
|
|
4050
4075
|
}
|
|
4051
|
-
const orphans = orphanRows.map((r) => deserializeOperationWithCollection(r, collection)).sort((a, b) =>
|
|
4076
|
+
const orphans = orphanRows.map((r) => deserializeOperationWithCollection(r, collection)).sort((a, b) => import_core23.HybridLogicalClock.compare(a.timestamp, b.timestamp));
|
|
4052
4077
|
for (const orphan of orphans) {
|
|
4053
4078
|
const orphanVersion = serializeRowVersion(orphan.timestamp);
|
|
4054
4079
|
const orphanWall = orphan.timestamp.wallTime;
|
|
4055
4080
|
if (orphan.type === "update" && orphan.data) {
|
|
4056
4081
|
const orphanChanges = serializeRecord(orphan.data, definition.fields);
|
|
4057
4082
|
const rows = await tx.query(
|
|
4058
|
-
`SELECT _version, _field_versions FROM ${collection} WHERE id = ?`,
|
|
4083
|
+
`SELECT _version, _field_versions FROM ${(0, import_core23.quoteIdent)(collection)} WHERE id = ?`,
|
|
4059
4084
|
[recordId]
|
|
4060
4085
|
);
|
|
4061
4086
|
const current = rows[0];
|
|
@@ -4105,7 +4130,7 @@ var Store = class {
|
|
|
4105
4130
|
const allOps = [];
|
|
4106
4131
|
for (const collectionName of Object.keys(this.schema.collections)) {
|
|
4107
4132
|
const rows = await this.adapter.query(
|
|
4108
|
-
`SELECT * FROM _kora_ops_${collectionName} WHERE node_id = ? AND sequence_number >= ? AND sequence_number <= ? ORDER BY sequence_number ASC`,
|
|
4133
|
+
`SELECT * FROM ${(0, import_core23.quoteIdent)(`_kora_ops_${collectionName}`)} WHERE node_id = ? AND sequence_number >= ? AND sequence_number <= ? ORDER BY sequence_number ASC`,
|
|
4109
4134
|
[nodeId, fromSeq, toSeq]
|
|
4110
4135
|
);
|
|
4111
4136
|
for (const row of rows) {
|
|
@@ -4124,7 +4149,7 @@ var Store = class {
|
|
|
4124
4149
|
const allOps = [];
|
|
4125
4150
|
for (const collectionName of Object.keys(this.schema.collections)) {
|
|
4126
4151
|
const rows = await this.adapter.query(
|
|
4127
|
-
`SELECT * FROM _kora_ops_${collectionName} ORDER BY sequence_number ASC`
|
|
4152
|
+
`SELECT * FROM ${(0, import_core23.quoteIdent)(`_kora_ops_${collectionName}`)} ORDER BY sequence_number ASC`
|
|
4128
4153
|
);
|
|
4129
4154
|
for (const row of rows) {
|
|
4130
4155
|
allOps.push(deserializeOperationWithCollection(row, collectionName));
|
|
@@ -4234,7 +4259,7 @@ var Store = class {
|
|
|
4234
4259
|
return null;
|
|
4235
4260
|
}
|
|
4236
4261
|
const rows = await this.adapter.query(
|
|
4237
|
-
`SELECT * FROM ${collection} WHERE id = ?`,
|
|
4262
|
+
`SELECT * FROM ${(0, import_core23.quoteIdent)(collection)} WHERE id = ?`,
|
|
4238
4263
|
[recordId]
|
|
4239
4264
|
);
|
|
4240
4265
|
const row = rows[0];
|
|
@@ -4259,7 +4284,7 @@ var Store = class {
|
|
|
4259
4284
|
return null;
|
|
4260
4285
|
}
|
|
4261
4286
|
const rows = await this.adapter.query(
|
|
4262
|
-
`SELECT _version, _field_versions FROM ${collection} WHERE id = ?`,
|
|
4287
|
+
`SELECT _version, _field_versions FROM ${(0, import_core23.quoteIdent)(collection)} WHERE id = ?`,
|
|
4263
4288
|
[recordId]
|
|
4264
4289
|
);
|
|
4265
4290
|
const row = rows[0];
|
|
@@ -4351,7 +4376,7 @@ var Store = class {
|
|
|
4351
4376
|
async compact(strategy) {
|
|
4352
4377
|
this.ensureOpen();
|
|
4353
4378
|
if (strategy.mode === "never") {
|
|
4354
|
-
return compactOperationLog(this.adapter, this.schema, strategy, (0,
|
|
4379
|
+
return compactOperationLog(this.adapter, this.schema, strategy, (0, import_core23.createVersionVector)());
|
|
4355
4380
|
}
|
|
4356
4381
|
const serverVector = strategy.serverVector ?? await loadLastAckedServerVector(this.adapter);
|
|
4357
4382
|
return compactOperationLog(this.adapter, this.schema, strategy, serverVector);
|
|
@@ -4365,7 +4390,7 @@ var Store = class {
|
|
|
4365
4390
|
async getLatestLocalOperationForRecord(collection, recordId) {
|
|
4366
4391
|
this.ensureOpen();
|
|
4367
4392
|
const rows = await this.adapter.query(
|
|
4368
|
-
`SELECT * FROM _kora_ops_${collection} WHERE node_id = ? AND record_id = ? ORDER BY sequence_number DESC LIMIT 1`,
|
|
4393
|
+
`SELECT * FROM ${(0, import_core23.quoteIdent)(`_kora_ops_${collection}`)} WHERE node_id = ? AND record_id = ? ORDER BY sequence_number DESC LIMIT 1`,
|
|
4369
4394
|
[this.nodeId, recordId]
|
|
4370
4395
|
);
|
|
4371
4396
|
const row = rows[0];
|
|
@@ -4380,13 +4405,13 @@ var Store = class {
|
|
|
4380
4405
|
async getLatestOperationForRecord(collection, recordId) {
|
|
4381
4406
|
this.ensureOpen();
|
|
4382
4407
|
const rows = await this.adapter.query(
|
|
4383
|
-
`SELECT * FROM _kora_ops_${collection} WHERE record_id = ?`,
|
|
4408
|
+
`SELECT * FROM ${(0, import_core23.quoteIdent)(`_kora_ops_${collection}`)} WHERE record_id = ?`,
|
|
4384
4409
|
[recordId]
|
|
4385
4410
|
);
|
|
4386
4411
|
let latest = null;
|
|
4387
4412
|
for (const row of rows) {
|
|
4388
4413
|
const op = deserializeOperationWithCollection(row, collection);
|
|
4389
|
-
if (!latest ||
|
|
4414
|
+
if (!latest || import_core23.HybridLogicalClock.compare(op.timestamp, latest.timestamp) > 0) {
|
|
4390
4415
|
latest = op;
|
|
4391
4416
|
}
|
|
4392
4417
|
}
|
|
@@ -4529,7 +4554,7 @@ var Store = class {
|
|
|
4529
4554
|
for (let v = storedVersion + 1; v <= targetVersion; v++) {
|
|
4530
4555
|
const migration = migrations[v];
|
|
4531
4556
|
if (!migration) continue;
|
|
4532
|
-
const sqlStatements = (0,
|
|
4557
|
+
const sqlStatements = (0, import_core23.migrationStepsToSQL)(migration.steps);
|
|
4533
4558
|
for (const sql of sqlStatements) {
|
|
4534
4559
|
try {
|
|
4535
4560
|
await this.adapter.execute(sql);
|
|
@@ -4567,14 +4592,14 @@ var Store = class {
|
|
|
4567
4592
|
*/
|
|
4568
4593
|
async runBackfill(collection, transform) {
|
|
4569
4594
|
const rows = await this.adapter.query(
|
|
4570
|
-
`SELECT * FROM ${collection} WHERE _deleted = 0`
|
|
4595
|
+
`SELECT * FROM ${(0, import_core23.quoteIdent)(collection)} WHERE _deleted = 0`
|
|
4571
4596
|
);
|
|
4572
4597
|
await this.adapter.transaction(async (tx) => {
|
|
4573
4598
|
for (const row of rows) {
|
|
4574
4599
|
const updates = transform(row);
|
|
4575
4600
|
const fields = Object.keys(updates);
|
|
4576
4601
|
if (fields.length === 0) continue;
|
|
4577
|
-
const setClauses = fields.map((f) => `${f} = ?`).join(", ");
|
|
4602
|
+
const setClauses = fields.map((f) => `${(0, import_core23.quoteIdent)(f)} = ?`).join(", ");
|
|
4578
4603
|
const values = fields.map((f) => {
|
|
4579
4604
|
const val = updates[f];
|
|
4580
4605
|
if (typeof val === "boolean") return val ? 1 : 0;
|
|
@@ -4584,7 +4609,7 @@ var Store = class {
|
|
|
4584
4609
|
return val;
|
|
4585
4610
|
});
|
|
4586
4611
|
values.push(row.id);
|
|
4587
|
-
await tx.execute(`UPDATE ${collection} SET ${setClauses} WHERE id = ?`, values);
|
|
4612
|
+
await tx.execute(`UPDATE ${(0, import_core23.quoteIdent)(collection)} SET ${setClauses} WHERE id = ?`, values);
|
|
4588
4613
|
}
|
|
4589
4614
|
});
|
|
4590
4615
|
}
|
|
@@ -4607,7 +4632,7 @@ var Store = class {
|
|
|
4607
4632
|
if (rows[0]) {
|
|
4608
4633
|
return rows[0].value;
|
|
4609
4634
|
}
|
|
4610
|
-
const newNodeId = (0,
|
|
4635
|
+
const newNodeId = (0, import_core23.generateUUIDv7)();
|
|
4611
4636
|
await this.adapter.execute("INSERT INTO _kora_meta (key, value) VALUES ('node_id', ?)", [
|
|
4612
4637
|
newNodeId
|
|
4613
4638
|
]);
|
|
@@ -4624,7 +4649,7 @@ var Store = class {
|
|
|
4624
4649
|
const rows = await this.adapter.query(
|
|
4625
4650
|
"SELECT node_id, sequence_number FROM _kora_version_vector"
|
|
4626
4651
|
);
|
|
4627
|
-
const vector = (0,
|
|
4652
|
+
const vector = (0, import_core23.createVersionVector)();
|
|
4628
4653
|
for (const row of rows) {
|
|
4629
4654
|
vector.set(row.node_id, row.sequence_number);
|
|
4630
4655
|
}
|
|
@@ -4698,12 +4723,12 @@ var QueryStore = class {
|
|
|
4698
4723
|
};
|
|
4699
4724
|
|
|
4700
4725
|
// src/reactivity/assert-query-ready.ts
|
|
4701
|
-
var
|
|
4726
|
+
var import_core24 = require("@korajs/core");
|
|
4702
4727
|
var LEGACY_PENDING_COLLECTION = "__pending__";
|
|
4703
4728
|
function assertQueryReady(query) {
|
|
4704
4729
|
const descriptor = query.getDescriptor();
|
|
4705
4730
|
if (descriptor.collection === LEGACY_PENDING_COLLECTION) {
|
|
4706
|
-
throw new
|
|
4731
|
+
throw new import_core24.AppNotReadyError(
|
|
4707
4732
|
"Cannot use useQuery() before app.ready. Await app.ready or wrap your UI in <KoraProvider app={app}>."
|
|
4708
4733
|
);
|
|
4709
4734
|
}
|
|
@@ -5126,7 +5151,7 @@ init_audit_json();
|
|
|
5126
5151
|
init_export_audit();
|
|
5127
5152
|
|
|
5128
5153
|
// src/blob/content-addressed-blob-store.ts
|
|
5129
|
-
var
|
|
5154
|
+
var import_core25 = require("@korajs/core");
|
|
5130
5155
|
var BlobIntegrityError = class extends Error {
|
|
5131
5156
|
constructor(expectedHash, actualHash) {
|
|
5132
5157
|
super(`Blob integrity check failed: content under hash ${expectedHash} hashes to ${actualHash}`);
|
|
@@ -5140,7 +5165,7 @@ var BlobIntegrityError = class extends Error {
|
|
|
5140
5165
|
var MemoryBlobStore = class {
|
|
5141
5166
|
blobs = /* @__PURE__ */ new Map();
|
|
5142
5167
|
async put(bytes, metadata = {}) {
|
|
5143
|
-
const ref = await (0,
|
|
5168
|
+
const ref = await (0, import_core25.createBlobRef)(bytes, metadata);
|
|
5144
5169
|
if (!this.blobs.has(ref.hash)) {
|
|
5145
5170
|
const stored = new Uint8Array(bytes.byteLength);
|
|
5146
5171
|
stored.set(bytes);
|
|
@@ -5153,7 +5178,7 @@ var MemoryBlobStore = class {
|
|
|
5153
5178
|
if (stored === void 0) {
|
|
5154
5179
|
return null;
|
|
5155
5180
|
}
|
|
5156
|
-
const actualHash = await (0,
|
|
5181
|
+
const actualHash = await (0, import_core25.hashBlob)(stored);
|
|
5157
5182
|
if (actualHash !== hash) {
|
|
5158
5183
|
throw new BlobIntegrityError(hash, actualHash);
|
|
5159
5184
|
}
|
|
@@ -5176,7 +5201,7 @@ var MemoryBlobStore = class {
|
|
|
5176
5201
|
};
|
|
5177
5202
|
|
|
5178
5203
|
// src/blob/opfs-blob-store.ts
|
|
5179
|
-
var
|
|
5204
|
+
var import_core26 = require("@korajs/core");
|
|
5180
5205
|
var OpfsBlobStore = class {
|
|
5181
5206
|
constructor(dir) {
|
|
5182
5207
|
this.dir = dir;
|
|
@@ -5186,7 +5211,7 @@ var OpfsBlobStore = class {
|
|
|
5186
5211
|
return hash.slice(0, 2);
|
|
5187
5212
|
}
|
|
5188
5213
|
async put(bytes, metadata = {}) {
|
|
5189
|
-
const ref = await (0,
|
|
5214
|
+
const ref = await (0, import_core26.createBlobRef)(bytes, metadata);
|
|
5190
5215
|
const shard = this.shardFor(ref.hash);
|
|
5191
5216
|
if (await this.dir.has(shard, ref.hash)) {
|
|
5192
5217
|
return ref;
|
|
@@ -5201,7 +5226,7 @@ var OpfsBlobStore = class {
|
|
|
5201
5226
|
if (bytes === null) {
|
|
5202
5227
|
return null;
|
|
5203
5228
|
}
|
|
5204
|
-
const actual = await (0,
|
|
5229
|
+
const actual = await (0, import_core26.hashBlob)(bytes);
|
|
5205
5230
|
if (actual !== hash) {
|
|
5206
5231
|
throw new BlobIntegrityError(hash, actual);
|
|
5207
5232
|
}
|
|
@@ -5347,7 +5372,7 @@ async function createOpfsBlobStore(rootDirName = "kora-blobs") {
|
|
|
5347
5372
|
}
|
|
5348
5373
|
|
|
5349
5374
|
// src/blob/blob-chunking.ts
|
|
5350
|
-
var
|
|
5375
|
+
var import_core27 = require("@korajs/core");
|
|
5351
5376
|
var DEFAULT_CHUNK_SIZE = 256 * 1024;
|
|
5352
5377
|
async function chunkBlob(bytes, chunkSize = DEFAULT_CHUNK_SIZE, metadata = {}) {
|
|
5353
5378
|
if (chunkSize <= 0) {
|
|
@@ -5359,13 +5384,13 @@ async function chunkBlob(bytes, chunkSize = DEFAULT_CHUNK_SIZE, metadata = {}) {
|
|
|
5359
5384
|
const end = Math.min(offset + chunkSize, bytes.byteLength);
|
|
5360
5385
|
const chunk = new Uint8Array(end - offset);
|
|
5361
5386
|
chunk.set(bytes.subarray(offset, end));
|
|
5362
|
-
const hash = await (0,
|
|
5387
|
+
const hash = await (0, import_core27.hashBlob)(chunk);
|
|
5363
5388
|
chunkHashes.push(hash);
|
|
5364
5389
|
if (!chunks.has(hash)) {
|
|
5365
5390
|
chunks.set(hash, chunk);
|
|
5366
5391
|
}
|
|
5367
5392
|
}
|
|
5368
|
-
const blobHash = await (0,
|
|
5393
|
+
const blobHash = await (0, import_core27.hashBlob)(bytes);
|
|
5369
5394
|
const manifest = {
|
|
5370
5395
|
blobHash,
|
|
5371
5396
|
size: bytes.byteLength,
|
|
@@ -5397,7 +5422,7 @@ async function reassembleBlob(manifest, chunkStore) {
|
|
|
5397
5422
|
out.set(part, offset);
|
|
5398
5423
|
offset += part.byteLength;
|
|
5399
5424
|
}
|
|
5400
|
-
const actual = await (0,
|
|
5425
|
+
const actual = await (0, import_core27.hashBlob)(out);
|
|
5401
5426
|
if (actual !== manifest.blobHash) {
|
|
5402
5427
|
throw new BlobIntegrityError(manifest.blobHash, actual);
|
|
5403
5428
|
}
|
|
@@ -5405,7 +5430,7 @@ async function reassembleBlob(manifest, chunkStore) {
|
|
|
5405
5430
|
}
|
|
5406
5431
|
|
|
5407
5432
|
// src/blob/blob-transfer.ts
|
|
5408
|
-
var
|
|
5433
|
+
var import_core28 = require("@korajs/core");
|
|
5409
5434
|
async function receiveBlob(manifest, provider, stores) {
|
|
5410
5435
|
let chunksFetched = 0;
|
|
5411
5436
|
let chunksSkipped = 0;
|
|
@@ -5423,7 +5448,7 @@ async function receiveBlob(manifest, provider, stores) {
|
|
|
5423
5448
|
if (bytes === null) {
|
|
5424
5449
|
throw new Error(`Blob transfer failed: provider could not supply chunk ${hash}`);
|
|
5425
5450
|
}
|
|
5426
|
-
const actual = await (0,
|
|
5451
|
+
const actual = await (0, import_core28.hashBlob)(bytes);
|
|
5427
5452
|
if (actual !== hash) {
|
|
5428
5453
|
throw new BlobIntegrityError(hash, actual);
|
|
5429
5454
|
}
|
|
@@ -5452,7 +5477,7 @@ async function prepareBlobForSend(bytes, chunkStore, options = {}) {
|
|
|
5452
5477
|
}
|
|
5453
5478
|
|
|
5454
5479
|
// src/blob/blob-manifest-transfer.ts
|
|
5455
|
-
var
|
|
5480
|
+
var import_core29 = require("@korajs/core");
|
|
5456
5481
|
function serializeBlobManifest(manifest) {
|
|
5457
5482
|
const canonical = {
|
|
5458
5483
|
blobHash: manifest.blobHash,
|
|
@@ -5517,7 +5542,7 @@ async function fetchBlobManifest(provider, manifestHash) {
|
|
|
5517
5542
|
if (bytes === null) {
|
|
5518
5543
|
throw new Error(`Blob manifest ${manifestHash} could not be fetched from the provider`);
|
|
5519
5544
|
}
|
|
5520
|
-
const actual = await (0,
|
|
5545
|
+
const actual = await (0, import_core29.hashBlob)(bytes);
|
|
5521
5546
|
if (actual !== manifestHash) {
|
|
5522
5547
|
throw new BlobIntegrityError(manifestHash, actual);
|
|
5523
5548
|
}
|
|
@@ -5533,12 +5558,12 @@ async function resolveBlobManifest(provider, ref) {
|
|
|
5533
5558
|
}
|
|
5534
5559
|
|
|
5535
5560
|
// src/blob/server-blob-callbacks.ts
|
|
5536
|
-
var
|
|
5561
|
+
var import_core30 = require("@korajs/core");
|
|
5537
5562
|
function toServerBlobCallbacks(store) {
|
|
5538
5563
|
return {
|
|
5539
5564
|
resolveBlobChunk: (hash) => store.get(hash),
|
|
5540
5565
|
async persistBlobChunk(hash, bytes) {
|
|
5541
|
-
const actual = await (0,
|
|
5566
|
+
const actual = await (0, import_core30.hashBlob)(bytes);
|
|
5542
5567
|
if (actual !== hash) {
|
|
5543
5568
|
return;
|
|
5544
5569
|
}
|
|
@@ -5552,11 +5577,11 @@ function createMemoryServerBlobStore() {
|
|
|
5552
5577
|
}
|
|
5553
5578
|
|
|
5554
5579
|
// src/blob/blob-gc.ts
|
|
5555
|
-
var
|
|
5580
|
+
var import_core31 = require("@korajs/core");
|
|
5556
5581
|
function extractBlobRefs(record) {
|
|
5557
5582
|
const refs = [];
|
|
5558
5583
|
for (const value of Object.values(record)) {
|
|
5559
|
-
if ((0,
|
|
5584
|
+
if ((0, import_core31.isBlobRef)(value)) {
|
|
5560
5585
|
refs.push(value);
|
|
5561
5586
|
}
|
|
5562
5587
|
}
|
|
@@ -5601,7 +5626,7 @@ async function collectBlobGarbage(store, liveRefs, options = {}) {
|
|
|
5601
5626
|
}
|
|
5602
5627
|
|
|
5603
5628
|
// src/blob/blob-chunk-transport.ts
|
|
5604
|
-
var
|
|
5629
|
+
var import_core32 = require("@korajs/core");
|
|
5605
5630
|
function createRemoteChunkProvider(port, options = {}) {
|
|
5606
5631
|
const timeoutMs = options.timeoutMs ?? 3e4;
|
|
5607
5632
|
const pending = /* @__PURE__ */ new Map();
|
|
@@ -5620,7 +5645,7 @@ function createRemoteChunkProvider(port, options = {}) {
|
|
|
5620
5645
|
return {
|
|
5621
5646
|
pendingCount: () => pending.size,
|
|
5622
5647
|
getChunk(hash) {
|
|
5623
|
-
const requestId = (0,
|
|
5648
|
+
const requestId = (0, import_core32.generateUUIDv7)();
|
|
5624
5649
|
return new Promise((resolve, reject) => {
|
|
5625
5650
|
const timer = setTimeout(() => {
|
|
5626
5651
|
pending.delete(requestId);
|
|
@@ -5686,6 +5711,7 @@ function createChunkPortPair() {
|
|
|
5686
5711
|
InvalidStateTransitionError,
|
|
5687
5712
|
LAST_ACKED_SERVER_VECTOR_META_KEY,
|
|
5688
5713
|
MemoryBlobStore,
|
|
5714
|
+
NoLeaderError,
|
|
5689
5715
|
OpfsBlobStore,
|
|
5690
5716
|
OptimisticLockError,
|
|
5691
5717
|
PersistenceError,
|