@bitcobblers/wod-wiki-storage 0.35.2117 → 0.35.2120
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/{chunk-Y576CUFQ.mjs → chunk-7ZCBM5NS.mjs} +3 -3
- package/dist/chunk-7ZCBM5NS.mjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/sql/index.cjs +318 -7
- package/dist/sql/index.cjs.map +1 -1
- package/dist/sql/index.d.cts +158 -3
- package/dist/sql/index.d.ts +158 -3
- package/dist/sql/index.mjs +310 -9
- package/dist/sql/index.mjs.map +1 -1
- package/dist/{wire-Cfvz-I0h.d.cts → wire-CiFcjo6_.d.cts} +1 -1
- package/dist/{wire-Cfvz-I0h.d.ts → wire-CiFcjo6_.d.ts} +1 -1
- package/package.json +6 -6
- package/dist/chunk-Y576CUFQ.mjs.map +0 -1
package/dist/sql/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { STORE_DEFS, StorageError, extractKeyPart, validateStorageRow, isRange, DB_VERSION, rowsEqual, validateStoredPins, DOMAIN_PROJECTION_VERSION, bufferToOps, stampUserOwned, bufferedGet, overlayRows, rangeFromIDB, isDirty, keyFromValue, bufferSlot, COLUMN_TYPES, keyInRange, compareKeys } from '../chunk-
|
|
2
|
-
export { StorageError } from '../chunk-
|
|
1
|
+
import { STORE_DEFS, StorageError, extractKeyPart, validateStorageRow, isRange, DB_VERSION, rowsEqual, validateStoredPins, DOMAIN_PROJECTION_VERSION, bufferToOps, stampUserOwned, bufferedGet, overlayRows, rangeFromIDB, isDirty, keyFromValue, bufferSlot, COLUMN_TYPES, keyInRange, compareKeys } from '../chunk-7ZCBM5NS.mjs';
|
|
2
|
+
export { StorageError } from '../chunk-7ZCBM5NS.mjs';
|
|
3
3
|
import { catalogOfItem, sourceMatches } from '@bitcobblers/wod-wiki-wql';
|
|
4
4
|
import { createClient } from '@libsql/client/web';
|
|
5
5
|
|
|
@@ -277,7 +277,7 @@ async function getRow(conn, def, key) {
|
|
|
277
277
|
}
|
|
278
278
|
async function getRows(conn, def, keys) {
|
|
279
279
|
const found = /* @__PURE__ */ new Map();
|
|
280
|
-
for (const part of chunks(keys,
|
|
280
|
+
for (const part of chunks(keys, 50)) {
|
|
281
281
|
const params = [];
|
|
282
282
|
const conditions = part.map((key) => {
|
|
283
283
|
const parts = toParts(key);
|
|
@@ -300,7 +300,7 @@ async function listRowsFromIndexMany(conn, def, indexName, keys) {
|
|
|
300
300
|
return out;
|
|
301
301
|
}
|
|
302
302
|
const found = /* @__PURE__ */ new Map();
|
|
303
|
-
for (const part of chunks(keys,
|
|
303
|
+
for (const part of chunks(keys, 50)) {
|
|
304
304
|
const params = [];
|
|
305
305
|
const conditions = part.map((key) => {
|
|
306
306
|
const parts = toParts(key);
|
|
@@ -594,25 +594,41 @@ async function initializeStore(db) {
|
|
|
594
594
|
await tx.exec(
|
|
595
595
|
`CREATE TABLE IF NOT EXISTS "${SCHEMA_TABLE}" ("key" TEXT PRIMARY KEY, "value" TEXT NOT NULL)`
|
|
596
596
|
);
|
|
597
|
+
const ignored = (name) => name.startsWith("_wodwiki_") || name.startsWith("sqlite_");
|
|
597
598
|
const storeTables = new Set(STORE_DEFS.map((d) => d.name));
|
|
599
|
+
for (const def of STORE_DEFS) {
|
|
600
|
+
const multi = def.indexes.find((i) => i.multiEntry);
|
|
601
|
+
if (multi) storeTables.add(junctionTable(def, multi));
|
|
602
|
+
}
|
|
598
603
|
const tableNames = new Set(
|
|
599
|
-
(await tx.query(tableListSql(db.dialect))).map((r) => String(Object.values(r)[0]))
|
|
604
|
+
(await tx.query(tableListSql(db.dialect))).map((r) => String(Object.values(r)[0])).filter((n) => !ignored(n))
|
|
600
605
|
);
|
|
601
606
|
const guardRows = await tx.query(`SELECT "value" FROM "${SCHEMA_TABLE}" WHERE "key" = ?`, [SCHEMA_KEY]);
|
|
602
607
|
let stored;
|
|
603
608
|
if (guardRows[0]) {
|
|
604
609
|
const guard = JSON.parse(String(guardRows[0].value));
|
|
605
|
-
if (typeof guard.version !== "number") {
|
|
610
|
+
if (typeof guard.version !== "number" || !Number.isSafeInteger(guard.version) || guard.version < 0) {
|
|
606
611
|
throw new StorageError(500, `corrupt ${SCHEMA_TABLE} guard row`, "bad_schema_guard");
|
|
607
612
|
}
|
|
608
613
|
stored = guard.version;
|
|
609
|
-
if (stored
|
|
614
|
+
if (stored > DB_VERSION) {
|
|
610
615
|
throw new StorageError(
|
|
611
616
|
500,
|
|
612
|
-
`database schema v${stored} is
|
|
617
|
+
`database schema v${stored} is newer than this server supports (v${DB_VERSION}); refusing to downgrade`,
|
|
613
618
|
"schema_incompatible"
|
|
614
619
|
);
|
|
615
620
|
}
|
|
621
|
+
if (stored < DB_VERSION) {
|
|
622
|
+
const missing = [...storeTables].filter((name) => !tableNames.has(name));
|
|
623
|
+
const unexpected = [...tableNames].filter((name) => name !== SCHEMA_TABLE && !storeTables.has(name));
|
|
624
|
+
if (missing.length > 0 || unexpected.length > 0) {
|
|
625
|
+
throw new StorageError(
|
|
626
|
+
500,
|
|
627
|
+
`database schema v${stored} is incompatible with v${DB_VERSION} (missing tables: ${missing.join(", ") || "none"}; unrecognized tables: ${unexpected.join(", ") || "none"}); recreation required using an explicitly disposable database`,
|
|
628
|
+
"schema_incompatible"
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
616
632
|
} else if ([...tableNames].some((name) => storeTables.has(name))) {
|
|
617
633
|
throw new StorageError(
|
|
618
634
|
500,
|
|
@@ -1556,6 +1572,291 @@ var BrowserTursoStorage = class extends SqlStorage {
|
|
|
1556
1572
|
}
|
|
1557
1573
|
};
|
|
1558
1574
|
|
|
1559
|
-
|
|
1575
|
+
// src/sql/sync.ts
|
|
1576
|
+
var SYNC_OPS_TABLE = "_wodwiki_sync_ops";
|
|
1577
|
+
var SYNC_STATE_TABLE = "_wodwiki_sync_state";
|
|
1578
|
+
var SYNC_ARCHIVE_TABLE = "_wodwiki_sync_ops_archive";
|
|
1579
|
+
var BINARY_FIELD2 = { attachments: "data" };
|
|
1580
|
+
var STORE_NAMES = Object.fromEntries(STORE_DEFS.map((d) => [d.name, true]));
|
|
1581
|
+
var STATE_KEY = "meta";
|
|
1582
|
+
function isSafeInt(value) {
|
|
1583
|
+
return typeof value === "number" && Number.isSafeInteger(value);
|
|
1584
|
+
}
|
|
1585
|
+
function isScalar(value) {
|
|
1586
|
+
return typeof value === "string" || typeof value === "number" && Number.isFinite(value);
|
|
1587
|
+
}
|
|
1588
|
+
function assertKey(key, what) {
|
|
1589
|
+
if (isScalar(key)) return;
|
|
1590
|
+
if (Array.isArray(key) && key.length > 0 && key.every(isScalar)) return;
|
|
1591
|
+
throw new StorageError(400, `${what} must be a string, finite number, or non-empty scalar tuple`, "bad_sync_key");
|
|
1592
|
+
}
|
|
1593
|
+
function validateMutation(raw) {
|
|
1594
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
1595
|
+
throw new StorageError(400, "sync mutation must be an object", "bad_sync_mutation");
|
|
1596
|
+
}
|
|
1597
|
+
const mutation = raw;
|
|
1598
|
+
if (mutation.op === "wipe") return { op: "wipe" };
|
|
1599
|
+
if (typeof mutation.store !== "string" || !STORE_NAMES[mutation.store]) {
|
|
1600
|
+
throw new StorageError(400, `sync mutation has unknown store ${String(mutation.store)}`, "bad_sync_mutation");
|
|
1601
|
+
}
|
|
1602
|
+
const store = mutation.store;
|
|
1603
|
+
if (mutation.op === "clear") return { op: "clear", store };
|
|
1604
|
+
if (mutation.op === "delete") {
|
|
1605
|
+
assertKey(mutation.key, "sync delete key");
|
|
1606
|
+
return { op: "delete", store, key: mutation.key };
|
|
1607
|
+
}
|
|
1608
|
+
if (mutation.op === "put") {
|
|
1609
|
+
assertKey(mutation.key, "sync put key");
|
|
1610
|
+
if (mutation.value === null || typeof mutation.value !== "object" || Array.isArray(mutation.value)) {
|
|
1611
|
+
throw new StorageError(400, "sync put value must be an object row", "bad_sync_mutation");
|
|
1612
|
+
}
|
|
1613
|
+
return { op: "put", store, key: mutation.key, value: mutation.value };
|
|
1614
|
+
}
|
|
1615
|
+
throw new StorageError(400, `sync mutation has unknown op ${String(mutation.op)}`, "bad_sync_mutation");
|
|
1616
|
+
}
|
|
1617
|
+
function validateEntry(entry) {
|
|
1618
|
+
if (typeof entry.id !== "string" || entry.id.length === 0 || entry.id.length > 200) {
|
|
1619
|
+
throw new StorageError(400, "sync entry id must be a non-empty string", "bad_sync_entry");
|
|
1620
|
+
}
|
|
1621
|
+
if (!isSafeInt(entry.at) || entry.at < 0) {
|
|
1622
|
+
throw new StorageError(400, "sync entry at must be a non-negative safe integer", "bad_sync_entry");
|
|
1623
|
+
}
|
|
1624
|
+
if (!Array.isArray(entry.ops) || entry.ops.length === 0) {
|
|
1625
|
+
throw new StorageError(400, "sync entry ops must be a non-empty array", "bad_sync_entry");
|
|
1626
|
+
}
|
|
1627
|
+
for (const op of entry.ops) validateMutation(op);
|
|
1628
|
+
}
|
|
1629
|
+
async function runStatements(tx, statements) {
|
|
1630
|
+
if (statements.length === 0) return [];
|
|
1631
|
+
if (tx.batch) return tx.batch(statements);
|
|
1632
|
+
const results = [];
|
|
1633
|
+
for (const statement of statements) {
|
|
1634
|
+
results.push(await tx.query(statement.sql, statement.params ?? []));
|
|
1635
|
+
}
|
|
1636
|
+
return results;
|
|
1637
|
+
}
|
|
1638
|
+
async function tableExists(conn, dialect, name) {
|
|
1639
|
+
const sql = dialect === "postgres" ? `SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name = $1` : `SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?`;
|
|
1640
|
+
return (await conn.query(sql, [name])).length > 0;
|
|
1641
|
+
}
|
|
1642
|
+
async function readSyncState(tx) {
|
|
1643
|
+
const rows = await tx.query(`SELECT "value" FROM "${SYNC_STATE_TABLE}" WHERE "key" = ?`, [STATE_KEY]);
|
|
1644
|
+
if (!rows[0]) throw new StorageError(409, "sync target is not initialized", "sync_not_initialized");
|
|
1645
|
+
let parsed;
|
|
1646
|
+
try {
|
|
1647
|
+
parsed = JSON.parse(String(rows[0].value));
|
|
1648
|
+
} catch {
|
|
1649
|
+
throw new StorageError(400, "malformed sync state row", "bad_sync_state");
|
|
1650
|
+
}
|
|
1651
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
1652
|
+
throw new StorageError(400, "malformed sync state row", "bad_sync_state");
|
|
1653
|
+
}
|
|
1654
|
+
const state = parsed;
|
|
1655
|
+
if (!isSafeInt(state.schema) || state.schema < 0 || !isSafeInt(state.rev) || state.rev < 0) {
|
|
1656
|
+
throw new StorageError(400, "sync state schema/rev must be non-negative safe integers", "bad_sync_state");
|
|
1657
|
+
}
|
|
1658
|
+
return { schema: state.schema, rev: state.rev };
|
|
1659
|
+
}
|
|
1660
|
+
async function probeSyncTarget(db) {
|
|
1661
|
+
if (await tableExists(db, db.dialect, SYNC_STATE_TABLE)) {
|
|
1662
|
+
const state = await readSyncState(db);
|
|
1663
|
+
return { kind: "current", schemaVersion: state.schema, rev: state.rev };
|
|
1664
|
+
}
|
|
1665
|
+
if (await tableExists(db, db.dialect, SCHEMA_TABLE)) {
|
|
1666
|
+
const rows = await db.query(`SELECT "value" FROM "${SCHEMA_TABLE}" WHERE "key" = 'schema'`);
|
|
1667
|
+
if (rows[0]) {
|
|
1668
|
+
const guard = JSON.parse(String(rows[0].value));
|
|
1669
|
+
if (typeof guard.version === "number" && Number.isSafeInteger(guard.version) && guard.version >= 0) {
|
|
1670
|
+
return { kind: "legacy", schemaVersion: guard.version };
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
return { kind: "legacy", schemaVersion: 0 };
|
|
1674
|
+
}
|
|
1675
|
+
const others = await db.query(
|
|
1676
|
+
db.dialect === "postgres" ? `SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'` : `SELECT name FROM sqlite_master WHERE type = 'table'`
|
|
1677
|
+
);
|
|
1678
|
+
const internal = (name) => name.startsWith("_wodwiki_") || name.startsWith("sqlite_");
|
|
1679
|
+
return others.some((row) => !internal(String(Object.values(row)[0]))) ? { kind: "unknown" } : "empty";
|
|
1680
|
+
}
|
|
1681
|
+
async function initSyncTarget(db, schemaVersion) {
|
|
1682
|
+
await db.transaction(async (tx) => {
|
|
1683
|
+
await tx.exec(`CREATE TABLE IF NOT EXISTS "${SYNC_STATE_TABLE}" ("key" TEXT PRIMARY KEY, "value" TEXT NOT NULL)`);
|
|
1684
|
+
await tx.exec(
|
|
1685
|
+
`CREATE TABLE IF NOT EXISTS "${SYNC_OPS_TABLE}" ("op_id" TEXT PRIMARY KEY, "rev" INTEGER NOT NULL, "idx" INTEGER NOT NULL, "at" INTEGER NOT NULL, "data" TEXT NOT NULL)`
|
|
1686
|
+
);
|
|
1687
|
+
await tx.exec(
|
|
1688
|
+
`CREATE INDEX IF NOT EXISTS "${SYNC_OPS_TABLE}_rev" ON "${SYNC_OPS_TABLE}" ("rev", "idx")`
|
|
1689
|
+
);
|
|
1690
|
+
await tx.exec(
|
|
1691
|
+
`INSERT INTO "${SYNC_STATE_TABLE}" ("key", "value") VALUES (?, ?) ON CONFLICT ("key") DO NOTHING`,
|
|
1692
|
+
[STATE_KEY, JSON.stringify({ schema: schemaVersion, rev: 0 })]
|
|
1693
|
+
);
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
function encodeMutation(mutation) {
|
|
1697
|
+
if (mutation.op !== "put") return JSON.stringify(mutation);
|
|
1698
|
+
const encoded = encodeRowJson(mutation.value, BINARY_FIELD2[mutation.store]);
|
|
1699
|
+
return `{"key":${JSON.stringify(mutation.key)},"op":"put","store":${JSON.stringify(mutation.store)},"value":${encoded}}`;
|
|
1700
|
+
}
|
|
1701
|
+
function decodeMutation(data) {
|
|
1702
|
+
if (typeof data !== "string") {
|
|
1703
|
+
throw new StorageError(400, "sync op data must be a string", "bad_sync_mutation");
|
|
1704
|
+
}
|
|
1705
|
+
let parsed;
|
|
1706
|
+
try {
|
|
1707
|
+
parsed = JSON.parse(data);
|
|
1708
|
+
} catch {
|
|
1709
|
+
throw new StorageError(400, "sync op data is not valid JSON", "bad_sync_mutation");
|
|
1710
|
+
}
|
|
1711
|
+
const mutation = validateMutation(parsed);
|
|
1712
|
+
if (mutation.op !== "put") return mutation;
|
|
1713
|
+
return {
|
|
1714
|
+
op: "put",
|
|
1715
|
+
store: mutation.store,
|
|
1716
|
+
key: mutation.key,
|
|
1717
|
+
value: decodeRowJson(JSON.stringify(mutation.value), BINARY_FIELD2[mutation.store])
|
|
1718
|
+
};
|
|
1719
|
+
}
|
|
1720
|
+
async function applySyncBatch(db, entries, opts = {}) {
|
|
1721
|
+
if (entries.length === 0) return 0;
|
|
1722
|
+
for (const entry of entries) validateEntry(entry);
|
|
1723
|
+
return db.transaction(async (tx) => {
|
|
1724
|
+
const state = await readSyncState(tx);
|
|
1725
|
+
if (opts.expectSchema !== void 0 && state.schema !== opts.expectSchema) {
|
|
1726
|
+
throw new StorageError(
|
|
1727
|
+
409,
|
|
1728
|
+
`sync target schema changed from v${opts.expectSchema} to v${state.schema} between probe and write \u2014 retry the cycle`,
|
|
1729
|
+
"sync_schema_conflict"
|
|
1730
|
+
);
|
|
1731
|
+
}
|
|
1732
|
+
const firstIds = entries.map((entry) => `${entry.id}:0`);
|
|
1733
|
+
const present = /* @__PURE__ */ new Set();
|
|
1734
|
+
for (let i = 0; i < firstIds.length; i += 100) {
|
|
1735
|
+
const chunk = firstIds.slice(i, i + 100);
|
|
1736
|
+
const rows = await tx.query(
|
|
1737
|
+
`SELECT "op_id" FROM "${SYNC_OPS_TABLE}" WHERE "op_id" IN (${chunk.map(() => "?").join(", ")})`,
|
|
1738
|
+
chunk
|
|
1739
|
+
);
|
|
1740
|
+
for (const row of rows) present.add(String(row.op_id));
|
|
1741
|
+
}
|
|
1742
|
+
const statements = [];
|
|
1743
|
+
let rev = state.rev;
|
|
1744
|
+
let committed = 0;
|
|
1745
|
+
for (const entry of entries) {
|
|
1746
|
+
if (present.has(`${entry.id}:0`)) continue;
|
|
1747
|
+
rev += 1;
|
|
1748
|
+
committed += 1;
|
|
1749
|
+
entry.ops.forEach((mutation, idx) => {
|
|
1750
|
+
statements.push({
|
|
1751
|
+
sql: `INSERT INTO "${SYNC_OPS_TABLE}" ("op_id", "rev", "idx", "at", "data") VALUES (?, ?, ?, ?, ?) ON CONFLICT ("op_id") DO NOTHING`,
|
|
1752
|
+
params: [`${entry.id}:${idx}`, rev, idx, entry.at, encodeMutation(mutation)]
|
|
1753
|
+
});
|
|
1754
|
+
});
|
|
1755
|
+
}
|
|
1756
|
+
const nextSchema = opts.toSchemaVersion ?? state.schema;
|
|
1757
|
+
statements.push({
|
|
1758
|
+
sql: `UPDATE "${SYNC_STATE_TABLE}" SET "value" = ? WHERE "key" = ?`,
|
|
1759
|
+
params: [JSON.stringify({ schema: nextSchema, rev }), STATE_KEY]
|
|
1760
|
+
});
|
|
1761
|
+
await runStatements(tx, statements);
|
|
1762
|
+
return committed;
|
|
1763
|
+
});
|
|
1764
|
+
}
|
|
1765
|
+
async function fetchSyncOps(db, afterRev, limit) {
|
|
1766
|
+
if (!isSafeInt(afterRev) || afterRev < 0) {
|
|
1767
|
+
throw new StorageError(400, "fetchSyncOps cursor must be a non-negative safe integer", "bad_sync_cursor");
|
|
1768
|
+
}
|
|
1769
|
+
const bounded = Math.min(Math.max(1, Math.floor(limit)), 4294967295);
|
|
1770
|
+
const revRows = await db.query(
|
|
1771
|
+
`SELECT DISTINCT "rev" FROM (SELECT "rev" FROM "${SYNC_OPS_TABLE}" WHERE "rev" > ? ORDER BY "rev" ASC LIMIT ?)`,
|
|
1772
|
+
[afterRev, bounded]
|
|
1773
|
+
);
|
|
1774
|
+
const revs = revRows.map((row) => {
|
|
1775
|
+
const rev = Number(row.rev);
|
|
1776
|
+
if (!Number.isSafeInteger(rev) || rev < 0) {
|
|
1777
|
+
throw new StorageError(400, "sync op rev must be a non-negative safe integer", "bad_sync_row");
|
|
1778
|
+
}
|
|
1779
|
+
return rev;
|
|
1780
|
+
});
|
|
1781
|
+
if (revs.length === 0) return [];
|
|
1782
|
+
const rows = await db.query(
|
|
1783
|
+
`SELECT "op_id", "rev", "idx", "at", "data" FROM "${SYNC_OPS_TABLE}" WHERE "rev" IN (${revs.map(() => "?").join(", ")}) ORDER BY "rev", "idx"`,
|
|
1784
|
+
revs
|
|
1785
|
+
);
|
|
1786
|
+
return rows.map((row, at) => {
|
|
1787
|
+
const opId = row.op_id;
|
|
1788
|
+
const rev = Number(row.rev);
|
|
1789
|
+
const idx = Number(row.idx);
|
|
1790
|
+
const atMs = Number(row.at);
|
|
1791
|
+
if (typeof opId !== "string" || opId.length === 0 || !Number.isSafeInteger(rev) || rev < 0 || !Number.isSafeInteger(idx) || idx < 0 || !Number.isSafeInteger(atMs) || atMs < 0) {
|
|
1792
|
+
throw new StorageError(400, `sync op row ${at} has malformed shape`, "bad_sync_row");
|
|
1793
|
+
}
|
|
1794
|
+
return { opId, rev, idx, at: atMs, mutation: decodeMutation(row.data) };
|
|
1795
|
+
});
|
|
1796
|
+
}
|
|
1797
|
+
async function replaceSyncLog(db, entries, toSchemaVersion) {
|
|
1798
|
+
if (entries.length === 0) throw new StorageError(400, "replacement log must contain the converted snapshot", "bad_sync_entry");
|
|
1799
|
+
for (const entry of entries) validateEntry(entry);
|
|
1800
|
+
await db.transaction(async (tx) => {
|
|
1801
|
+
await readSyncState(tx);
|
|
1802
|
+
await tx.exec(
|
|
1803
|
+
`CREATE TABLE IF NOT EXISTS "${SYNC_ARCHIVE_TABLE}" ("op_id" TEXT PRIMARY KEY, "rev" INTEGER NOT NULL, "idx" INTEGER NOT NULL, "at" INTEGER NOT NULL, "data" TEXT NOT NULL)`
|
|
1804
|
+
);
|
|
1805
|
+
await tx.exec(`INSERT INTO "${SYNC_ARCHIVE_TABLE}" ("op_id", "rev", "idx", "at", "data") SELECT "op_id", "rev", "idx", "at", "data" FROM "${SYNC_OPS_TABLE}"`);
|
|
1806
|
+
await tx.exec(`DELETE FROM "${SYNC_OPS_TABLE}"`);
|
|
1807
|
+
const statements = [];
|
|
1808
|
+
let rev = 0;
|
|
1809
|
+
for (const entry of entries) {
|
|
1810
|
+
rev += 1;
|
|
1811
|
+
entry.ops.forEach((mutation, idx) => {
|
|
1812
|
+
statements.push({
|
|
1813
|
+
sql: `INSERT INTO "${SYNC_OPS_TABLE}" ("op_id", "rev", "idx", "at", "data") VALUES (?, ?, ?, ?, ?) ON CONFLICT ("op_id") DO NOTHING`,
|
|
1814
|
+
params: [`${entry.id}:${idx}`, rev, idx, entry.at, encodeMutation(mutation)]
|
|
1815
|
+
});
|
|
1816
|
+
});
|
|
1817
|
+
}
|
|
1818
|
+
statements.push({
|
|
1819
|
+
sql: `UPDATE "${SYNC_STATE_TABLE}" SET "value" = ? WHERE "key" = ?`,
|
|
1820
|
+
params: [JSON.stringify({ schema: toSchemaVersion, rev }), STATE_KEY]
|
|
1821
|
+
});
|
|
1822
|
+
await runStatements(tx, statements);
|
|
1823
|
+
});
|
|
1824
|
+
}
|
|
1825
|
+
async function readRawSyncOps(db) {
|
|
1826
|
+
const rows = await db.query(`SELECT "rev", "idx", "data" FROM "${SYNC_OPS_TABLE}" ORDER BY "rev", "idx"`);
|
|
1827
|
+
return rows.map((row) => {
|
|
1828
|
+
const rev = Number(row.rev);
|
|
1829
|
+
const idx = Number(row.idx);
|
|
1830
|
+
if (!Number.isSafeInteger(rev) || rev < 0 || !Number.isSafeInteger(idx) || idx < 0 || typeof row.data !== "string") {
|
|
1831
|
+
throw new StorageError(400, "sync op row has malformed shape", "bad_sync_row");
|
|
1832
|
+
}
|
|
1833
|
+
let parsed;
|
|
1834
|
+
try {
|
|
1835
|
+
parsed = JSON.parse(row.data);
|
|
1836
|
+
} catch {
|
|
1837
|
+
throw new StorageError(400, "sync op data is not valid JSON", "bad_sync_row");
|
|
1838
|
+
}
|
|
1839
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
1840
|
+
throw new StorageError(400, "sync op data must be an object", "bad_sync_row");
|
|
1841
|
+
}
|
|
1842
|
+
const mutation = parsed;
|
|
1843
|
+
if (typeof mutation.op !== "string" || typeof mutation.store !== "string") {
|
|
1844
|
+
throw new StorageError(400, "sync op must carry string op/store", "bad_sync_row");
|
|
1845
|
+
}
|
|
1846
|
+
return {
|
|
1847
|
+
op: mutation.op,
|
|
1848
|
+
store: mutation.store,
|
|
1849
|
+
key: mutation.key,
|
|
1850
|
+
value: mutation.value === void 0 || mutation.value === null ? void 0 : decodeRowJson(JSON.stringify(mutation.value), BINARY_FIELD2[mutation.store])
|
|
1851
|
+
};
|
|
1852
|
+
});
|
|
1853
|
+
}
|
|
1854
|
+
async function readLegacyRows(db, table) {
|
|
1855
|
+
if (!await tableExists(db, db.dialect, table)) return [];
|
|
1856
|
+
const rows = await db.query(`SELECT "data" FROM "${table}"`);
|
|
1857
|
+
return rows.map((row) => decodeRowJson(String(row.data), BINARY_FIELD2[table]));
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
export { BrowserTursoStorage, MAX_BODY, SCHEMA_TABLE, SYNC_ARCHIVE_TABLE, SYNC_OPS_TABLE, SYNC_STATE_TABLE, SqlStorage, applySyncBatch, clearRows, countRows, decodeRowJson, deleteRows, encodeRowJson, fetchSyncOps, getRow, getRows, handleDomainQuery, initSyncTarget, initializeDomain, initializeStore, libsqlDatabase, listRows, listRowsFromIndexMany, probeSyncTarget, putRow, putRows, readLegacyRows, readRawSyncOps, rebuildProjections, refreshDomain, replaceSyncLog, storeDefinition, validateTursoUrl, wipeDomain, wipeRows };
|
|
1560
1861
|
//# sourceMappingURL=index.mjs.map
|
|
1561
1862
|
//# sourceMappingURL=index.mjs.map
|