@starkeep/storage-sqlite 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +281 -201
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -22
- package/dist/index.d.ts +24 -22
- package/dist/index.js +295 -199
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -28,6 +28,7 @@ __export(index_exports, {
|
|
|
28
28
|
getAppSyncableNamespace: () => getAppSyncableNamespace,
|
|
29
29
|
initializeLocalSchema: () => initializeLocalSchema,
|
|
30
30
|
listAppSyncableNamespaces: () => listAppSyncableNamespaces,
|
|
31
|
+
sqliteCompiler: () => compiler,
|
|
31
32
|
upsertAppSyncableNamespace: () => upsertAppSyncableNamespace
|
|
32
33
|
});
|
|
33
34
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -37,10 +38,12 @@ var import_node_sqlite = require("sqlite");
|
|
|
37
38
|
var import_node_fs = require("fs");
|
|
38
39
|
var import_node_path = require("path");
|
|
39
40
|
var import_protocol_primitives3 = require("@starkeep/protocol-primitives");
|
|
40
|
-
var
|
|
41
|
+
var import_storage_adapter2 = require("@starkeep/storage-adapter");
|
|
42
|
+
var import_kysely3 = require("kysely");
|
|
41
43
|
|
|
42
44
|
// src/serialization.ts
|
|
43
45
|
var import_protocol_primitives = require("@starkeep/protocol-primitives");
|
|
46
|
+
var import_storage_adapter = require("@starkeep/storage-adapter");
|
|
44
47
|
function recordToRow(record) {
|
|
45
48
|
return {
|
|
46
49
|
id: record.id,
|
|
@@ -56,8 +59,7 @@ function recordToRow(record) {
|
|
|
56
59
|
size_bytes: record.sizeBytes,
|
|
57
60
|
original_filename: record.originalFilename,
|
|
58
61
|
origin_app_id: record.originAppId,
|
|
59
|
-
parent_id: record.parentId
|
|
60
|
-
label: record.label
|
|
62
|
+
parent_id: record.parentId
|
|
61
63
|
};
|
|
62
64
|
}
|
|
63
65
|
function rowToRecord(row) {
|
|
@@ -75,8 +77,7 @@ function rowToRecord(row) {
|
|
|
75
77
|
sizeBytes: row.size_bytes,
|
|
76
78
|
originalFilename: row.original_filename,
|
|
77
79
|
originAppId: row.origin_app_id,
|
|
78
|
-
parentId: row.parent_id ? (0, import_protocol_primitives.createStarkeepId)(row.parent_id) : null
|
|
79
|
-
label: row.label
|
|
80
|
+
parentId: row.parent_id ? (0, import_protocol_primitives.createStarkeepId)(row.parent_id) : null
|
|
80
81
|
};
|
|
81
82
|
}
|
|
82
83
|
|
|
@@ -95,8 +96,7 @@ var FIELD_MAP = {
|
|
|
95
96
|
sizeBytes: "size_bytes",
|
|
96
97
|
originAppId: "origin_app_id",
|
|
97
98
|
parentId: "parent_id",
|
|
98
|
-
originalFilename: "original_filename"
|
|
99
|
-
label: "label"
|
|
99
|
+
originalFilename: "original_filename"
|
|
100
100
|
};
|
|
101
101
|
function mapField(field) {
|
|
102
102
|
return FIELD_MAP[field] ?? field;
|
|
@@ -164,93 +164,99 @@ function applyFilter(qb, filter) {
|
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
// src/schema/bootstrap.ts
|
|
167
|
+
var import_kysely2 = require("kysely");
|
|
167
168
|
var import_protocol_primitives2 = require("@starkeep/protocol-primitives");
|
|
168
|
-
function
|
|
169
|
+
function applyLocalSchemaDdl(db) {
|
|
169
170
|
db.exec("PRAGMA journal_mode = WAL");
|
|
170
171
|
db.exec("PRAGMA foreign_keys = ON");
|
|
171
|
-
db.exec(`
|
|
172
|
-
CREATE TABLE IF NOT EXISTS shared_records (
|
|
173
|
-
id TEXT PRIMARY KEY,
|
|
174
|
-
type TEXT NOT NULL,
|
|
175
|
-
created_at TEXT NOT NULL,
|
|
176
|
-
updated_at TEXT NOT NULL,
|
|
177
|
-
-- Denormalized from updated_at (its nodeId component) on every write.
|
|
178
|
-
-- Feeds the sync responder's per-node coverage watermark via the
|
|
179
|
-
-- (node_id, updated_at) index without scanning the table.
|
|
180
|
-
node_id TEXT NOT NULL,
|
|
181
|
-
deleted_at TEXT,
|
|
182
|
-
version INTEGER NOT NULL DEFAULT 1,
|
|
183
|
-
content_hash TEXT NOT NULL,
|
|
184
|
-
object_storage_key TEXT NOT NULL,
|
|
185
|
-
mime_type TEXT,
|
|
186
|
-
size_bytes INTEGER NOT NULL,
|
|
187
|
-
original_filename TEXT,
|
|
188
|
-
origin_app_id TEXT NOT NULL,
|
|
189
|
-
parent_id TEXT,
|
|
190
|
-
-- Advisory appId/purpose interest-filter marker (e.g. photos/thumbnail);
|
|
191
|
-
-- NULL = general interest. See DataRecord.label.
|
|
192
|
-
label TEXT
|
|
193
|
-
)
|
|
194
|
-
`);
|
|
195
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_type ON shared_records(type)");
|
|
196
172
|
db.exec(
|
|
197
|
-
"
|
|
173
|
+
compiler.schema.createTable("shared_records").ifNotExists().addColumn("id", "text", (c) => c.primaryKey()).addColumn("type", "text", (c) => c.notNull()).addColumn("created_at", "text", (c) => c.notNull()).addColumn("updated_at", "text", (c) => c.notNull()).addColumn("node_id", "text", (c) => c.notNull()).addColumn("deleted_at", "text").addColumn("version", "integer", (c) => c.notNull().defaultTo(1)).addColumn("content_hash", "text", (c) => c.notNull()).addColumn("object_storage_key", "text", (c) => c.notNull()).addColumn("mime_type", "text").addColumn("size_bytes", "integer", (c) => c.notNull()).addColumn("original_filename", "text").addColumn("origin_app_id", "text", (c) => c.notNull()).addColumn("parent_id", "text").compile().sql
|
|
174
|
+
);
|
|
175
|
+
const sharedRecordsIndexes = [
|
|
176
|
+
compiler.schema.createIndex("idx_shared_records_type").ifNotExists().on("shared_records").column("type"),
|
|
177
|
+
compiler.schema.createIndex("idx_shared_records_node_watermark").ifNotExists().on("shared_records").columns(["node_id", "updated_at"]),
|
|
178
|
+
compiler.schema.createIndex("idx_shared_records_origin_app").ifNotExists().on("shared_records").column("origin_app_id"),
|
|
179
|
+
compiler.schema.createIndex("idx_shared_records_parent_id").ifNotExists().on("shared_records").column("parent_id"),
|
|
180
|
+
// Duplicate-file prevention: (filename + bytes) is unique among live
|
|
181
|
+
// records. Tombstoned rows (deleted_at IS NOT NULL) are excluded so a
|
|
182
|
+
// re-upload after delete is allowed. Records with NULL filename are not
|
|
183
|
+
// constrained — the rule requires both filename and content to match.
|
|
184
|
+
compiler.schema.createIndex("uq_shared_records_filename_hash").ifNotExists().unique().on("shared_records").columns(["original_filename", "content_hash"]).where(import_kysely2.sql.ref("deleted_at"), "is", null).where(import_kysely2.sql.ref("original_filename"), "is not", null)
|
|
185
|
+
];
|
|
186
|
+
for (const index of sharedRecordsIndexes) {
|
|
187
|
+
db.exec(index.compile().sql);
|
|
188
|
+
}
|
|
189
|
+
db.exec(
|
|
190
|
+
compiler.schema.createTable("shared_access_grants").ifNotExists().addColumn("app_id", "text", (c) => c.notNull()).addColumn("type_id", "text", (c) => c.notNull()).addColumn("access", "text", (c) => c.notNull().check(import_kysely2.sql`access IN ('read', 'readwrite')`)).addColumn("metadata_write", "integer", (c) => c.notNull().defaultTo(0)).addColumn("created_at", "text", (c) => c.notNull().defaultTo(import_kysely2.sql`(datetime('now'))`)).addPrimaryKeyConstraint("pk_shared_access_grants", ["app_id", "type_id"]).compile().sql
|
|
191
|
+
);
|
|
192
|
+
db.exec(
|
|
193
|
+
compiler.schema.createIndex("idx_shared_access_grants_app").ifNotExists().on("shared_access_grants").column("app_id").compile().sql
|
|
194
|
+
);
|
|
195
|
+
db.exec(
|
|
196
|
+
compiler.schema.createTable("shared_record_labels").ifNotExists().addColumn("record_id", "text", (c) => c.notNull()).addColumn("app_id", "text", (c) => c.notNull()).addColumn("key", "text", (c) => c.notNull()).addColumn("value", "text", (c) => c.notNull()).addColumn("record_type", "text", (c) => c.notNull()).addColumn("created_at", "text", (c) => c.notNull()).addColumn("updated_at", "text", (c) => c.notNull()).addColumn("node_id", "text", (c) => c.notNull()).addColumn("deleted_at", "text").addPrimaryKeyConstraint("pk_shared_record_labels", [
|
|
197
|
+
"record_id",
|
|
198
|
+
"app_id",
|
|
199
|
+
"key",
|
|
200
|
+
"value"
|
|
201
|
+
]).compile().sql
|
|
202
|
+
);
|
|
203
|
+
const sharedRecordLabelsIndexes = [
|
|
204
|
+
// The reverse path. Column order mirrors DSQL's idx_record_labels_reverse
|
|
205
|
+
// exactly — see the long comment there for why each column is where it is.
|
|
206
|
+
// SQLite *does* support partial indexes, so `deleted_at` could have been a
|
|
207
|
+
// WHERE clause here; it is a key column instead so the two backends have
|
|
208
|
+
// one index shape between them and a query planned on one is planned the
|
|
209
|
+
// same on the other.
|
|
210
|
+
compiler.schema.createIndex("idx_shared_record_labels_reverse").ifNotExists().on("shared_record_labels").columns(["app_id", "key", "deleted_at", "value", "record_id"]),
|
|
211
|
+
compiler.schema.createIndex("idx_shared_record_labels_node_watermark").ifNotExists().on("shared_record_labels").columns(["node_id", "updated_at"])
|
|
212
|
+
];
|
|
213
|
+
for (const index of sharedRecordLabelsIndexes) {
|
|
214
|
+
db.exec(index.compile().sql);
|
|
215
|
+
}
|
|
216
|
+
db.exec(
|
|
217
|
+
compiler.schema.createTable("shared_app_label_keys").ifNotExists().addColumn("app_id", "text", (c) => c.notNull()).addColumn("key", "text", (c) => c.notNull()).addColumn("description", "text").addColumn("created_at", "text", (c) => c.notNull().defaultTo(import_kysely2.sql`(datetime('now'))`)).addPrimaryKeyConstraint("pk_shared_app_label_keys", ["app_id", "key"]).compile().sql
|
|
218
|
+
);
|
|
219
|
+
db.exec(
|
|
220
|
+
compiler.schema.createTable("shared_app_registry").ifNotExists().addColumn("app_id", "text", (c) => c.primaryKey()).addColumn("name", "text", (c) => c.notNull()).addColumn("version", "text", (c) => c.notNull()).addColumn("tier", "text", (c) => c.notNull().defaultTo("app")).addColumn("manifest", "text", (c) => c.notNull()).addColumn("status", "text", (c) => c.notNull().defaultTo("installing")).addColumn("hmac_secret", "text", (c) => c.notNull()).addColumn("installed_at", "text", (c) => c.notNull().defaultTo(import_kysely2.sql`(datetime('now'))`)).addColumn("updated_at", "text", (c) => c.notNull().defaultTo(import_kysely2.sql`(datetime('now'))`)).compile().sql
|
|
221
|
+
);
|
|
222
|
+
db.exec(
|
|
223
|
+
compiler.schema.createTable("shared_app_install_steps").ifNotExists().addColumn("app_id", "text", (c) => c.notNull()).addColumn(
|
|
224
|
+
"operation",
|
|
225
|
+
"text",
|
|
226
|
+
(c) => c.notNull().check(import_kysely2.sql`operation IN ('install', 'uninstall')`)
|
|
227
|
+
).addColumn("step", "text", (c) => c.notNull()).addColumn(
|
|
228
|
+
"status",
|
|
229
|
+
"text",
|
|
230
|
+
(c) => c.notNull().check(import_kysely2.sql`status IN ('pending', 'done', 'failed')`)
|
|
231
|
+
).addColumn("error", "text").addColumn("updated_at", "text", (c) => c.notNull().defaultTo(import_kysely2.sql`(datetime('now'))`)).addPrimaryKeyConstraint("pk_shared_app_install_steps", [
|
|
232
|
+
"app_id",
|
|
233
|
+
"operation",
|
|
234
|
+
"step"
|
|
235
|
+
]).compile().sql
|
|
198
236
|
);
|
|
199
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_origin_app ON shared_records(origin_app_id)");
|
|
200
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_parent_id ON shared_records(parent_id)");
|
|
201
237
|
db.exec(
|
|
202
|
-
"
|
|
238
|
+
compiler.schema.createTable("app_syncable_namespaces").ifNotExists().addColumn("app_id", "text", (c) => c.primaryKey()).addColumn("tables_json", "text", (c) => c.notNull()).addColumn("files_enabled", "integer", (c) => c.notNull().defaultTo(0)).addColumn("created_at", "text", (c) => c.notNull().defaultTo(import_kysely2.sql`(datetime('now'))`)).compile().sql
|
|
203
239
|
);
|
|
204
|
-
db.exec(`
|
|
205
|
-
CREATE TABLE IF NOT EXISTS shared_access_grants (
|
|
206
|
-
app_id TEXT NOT NULL,
|
|
207
|
-
type_id TEXT NOT NULL,
|
|
208
|
-
access TEXT NOT NULL CHECK (access IN ('read', 'readwrite')),
|
|
209
|
-
metadata_write INTEGER NOT NULL DEFAULT 0,
|
|
210
|
-
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
211
|
-
PRIMARY KEY (app_id, type_id)
|
|
212
|
-
)
|
|
213
|
-
`);
|
|
214
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_access_grants_app ON shared_access_grants(app_id)");
|
|
215
|
-
db.exec(`
|
|
216
|
-
CREATE TABLE IF NOT EXISTS shared_app_registry (
|
|
217
|
-
app_id TEXT PRIMARY KEY,
|
|
218
|
-
name TEXT NOT NULL,
|
|
219
|
-
version TEXT NOT NULL,
|
|
220
|
-
tier TEXT NOT NULL DEFAULT 'app',
|
|
221
|
-
manifest TEXT NOT NULL,
|
|
222
|
-
status TEXT NOT NULL DEFAULT 'installing',
|
|
223
|
-
hmac_secret TEXT NOT NULL,
|
|
224
|
-
installed_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
225
|
-
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
226
|
-
)
|
|
227
|
-
`);
|
|
228
|
-
db.exec(`
|
|
229
|
-
CREATE TABLE IF NOT EXISTS shared_app_install_steps (
|
|
230
|
-
app_id TEXT NOT NULL,
|
|
231
|
-
operation TEXT NOT NULL CHECK (operation IN ('install', 'uninstall')),
|
|
232
|
-
step TEXT NOT NULL,
|
|
233
|
-
status TEXT NOT NULL CHECK (status IN ('pending', 'done', 'failed')),
|
|
234
|
-
error TEXT,
|
|
235
|
-
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
236
|
-
PRIMARY KEY (app_id, operation, step)
|
|
237
|
-
)
|
|
238
|
-
`);
|
|
239
|
-
db.exec(`
|
|
240
|
-
CREATE TABLE IF NOT EXISTS app_syncable_namespaces (
|
|
241
|
-
app_id TEXT PRIMARY KEY,
|
|
242
|
-
tables_json TEXT NOT NULL,
|
|
243
|
-
files_enabled INTEGER NOT NULL DEFAULT 0,
|
|
244
|
-
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
245
|
-
)
|
|
246
|
-
`);
|
|
247
240
|
for (const c of import_protocol_primitives2.CATEGORIES) {
|
|
248
241
|
if (c.id === "other") continue;
|
|
249
242
|
db.exec((0, import_protocol_primitives2.sqliteMetadataDdl)(c));
|
|
250
243
|
}
|
|
251
244
|
}
|
|
245
|
+
function initializeLocalSchema(db) {
|
|
246
|
+
try {
|
|
247
|
+
applyLocalSchemaDdl(db);
|
|
248
|
+
} catch (err) {
|
|
249
|
+
throw new Error(
|
|
250
|
+
`Local schema bootstrap failed: ${err.message}. This usually means the SQLite DB predates the current schema. There is no migration system by design \u2014 delete the DB ($STARKEEP_DIR/data.db, default ~/.starkeep/data.db) and it will be rebuilt on next start.`,
|
|
251
|
+
{ cause: err }
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
252
255
|
|
|
253
256
|
// src/adapter.ts
|
|
257
|
+
var LABELS = {
|
|
258
|
+
table: "shared_record_labels"
|
|
259
|
+
};
|
|
254
260
|
var SqliteDatabaseAdapter = class {
|
|
255
261
|
database = null;
|
|
256
262
|
options;
|
|
@@ -275,14 +281,14 @@ var SqliteDatabaseAdapter = class {
|
|
|
275
281
|
async healthCheck() {
|
|
276
282
|
if (!this.database) return false;
|
|
277
283
|
try {
|
|
278
|
-
this.database.prepare(
|
|
284
|
+
this.database.prepare(import_kysely3.sql`SELECT 1`.compile(compiler).sql).get();
|
|
279
285
|
return true;
|
|
280
286
|
} catch {
|
|
281
287
|
return false;
|
|
282
288
|
}
|
|
283
289
|
}
|
|
284
290
|
getDatabase() {
|
|
285
|
-
if (!this.database) throw new
|
|
291
|
+
if (!this.database) throw new import_storage_adapter2.StorageError("Database not initialized. Call init() first.");
|
|
286
292
|
return this.database;
|
|
287
293
|
}
|
|
288
294
|
/**
|
|
@@ -293,45 +299,44 @@ var SqliteDatabaseAdapter = class {
|
|
|
293
299
|
getRawDatabase() {
|
|
294
300
|
return this.getDatabase();
|
|
295
301
|
}
|
|
296
|
-
runStmt(
|
|
297
|
-
this.getDatabase().prepare(
|
|
302
|
+
runStmt(sql3, ...params) {
|
|
303
|
+
this.getDatabase().prepare(sql3).run(...params);
|
|
298
304
|
}
|
|
299
|
-
getRow(
|
|
300
|
-
return this.getDatabase().prepare(
|
|
305
|
+
getRow(sql3, ...params) {
|
|
306
|
+
return this.getDatabase().prepare(sql3).get(
|
|
301
307
|
...params
|
|
302
308
|
);
|
|
303
309
|
}
|
|
304
|
-
allRows(
|
|
305
|
-
return this.getDatabase().prepare(
|
|
310
|
+
allRows(sql3, ...params) {
|
|
311
|
+
return this.getDatabase().prepare(sql3).all(
|
|
306
312
|
...params
|
|
307
313
|
);
|
|
308
314
|
}
|
|
309
315
|
async put(record) {
|
|
310
316
|
const row = recordToRow(record);
|
|
311
|
-
const
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
317
|
+
const updateColumns = Object.keys(row).filter((column) => column !== "id");
|
|
318
|
+
const query = compiler.insertInto("shared_records").values({ ...row }).onConflict(
|
|
319
|
+
(oc) => oc.column("id").doUpdateSet(
|
|
320
|
+
(eb) => Object.fromEntries(
|
|
321
|
+
updateColumns.map((column) => [column, eb.ref(`excluded.${column}`)])
|
|
322
|
+
)
|
|
323
|
+
)
|
|
324
|
+
).compile();
|
|
325
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
316
326
|
}
|
|
317
327
|
async get(id) {
|
|
318
|
-
const
|
|
328
|
+
const query = compiler.selectFrom("shared_records").selectAll().where("id", "=", id).compile();
|
|
329
|
+
const row = this.getRow(query.sql, ...query.parameters);
|
|
319
330
|
return row ? rowToRecord(row) : null;
|
|
320
331
|
}
|
|
321
332
|
async delete(id, hlc) {
|
|
322
333
|
const ts = (0, import_protocol_primitives3.serializeHLC)(hlc);
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
ts,
|
|
326
|
-
ts,
|
|
327
|
-
hlc.nodeId,
|
|
328
|
-
id
|
|
329
|
-
);
|
|
334
|
+
const query = compiler.updateTable("shared_records").set({ deleted_at: ts, updated_at: ts, node_id: hlc.nodeId }).where("id", "=", id).compile();
|
|
335
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
330
336
|
}
|
|
331
337
|
async getNodeWatermarks() {
|
|
332
|
-
const
|
|
333
|
-
|
|
334
|
-
);
|
|
338
|
+
const watermarksQuery = compiler.selectFrom("shared_records").select(({ fn }) => ["node_id", fn.max("updated_at").as("max_updated_at")]).groupBy("node_id").compile();
|
|
339
|
+
const rows = this.allRows(watermarksQuery.sql);
|
|
335
340
|
const out = {};
|
|
336
341
|
for (const row of rows) {
|
|
337
342
|
out[row.node_id] = (0, import_protocol_primitives3.deserializeHLC)(row.max_updated_at);
|
|
@@ -339,8 +344,8 @@ var SqliteDatabaseAdapter = class {
|
|
|
339
344
|
return out;
|
|
340
345
|
}
|
|
341
346
|
async query(query) {
|
|
342
|
-
const { sql, params } = buildSelectQuery(query);
|
|
343
|
-
const rows = this.allRows(
|
|
347
|
+
const { sql: sql3, params } = buildSelectQuery(query);
|
|
348
|
+
const rows = this.allRows(sql3, ...params);
|
|
344
349
|
const limit = query.limit;
|
|
345
350
|
const hasMore = limit ? rows.length > limit : false;
|
|
346
351
|
const resultRows = hasMore ? rows.slice(0, limit) : rows;
|
|
@@ -381,29 +386,28 @@ var SqliteDatabaseAdapter = class {
|
|
|
381
386
|
} catch (error) {
|
|
382
387
|
this.getDatabase().exec("ROLLBACK TO SAVEPOINT starkeep_tx");
|
|
383
388
|
this.getDatabase().exec("RELEASE SAVEPOINT starkeep_tx");
|
|
384
|
-
throw new
|
|
389
|
+
throw new import_storage_adapter2.TransactionError("Transaction failed", error);
|
|
385
390
|
}
|
|
386
391
|
}
|
|
387
392
|
async putMetadata(typeId, row) {
|
|
388
393
|
const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
|
|
389
|
-
const
|
|
390
|
-
const values = [row.recordId];
|
|
394
|
+
const values = { record_id: row.recordId };
|
|
391
395
|
for (const [key, value] of Object.entries(row)) {
|
|
392
396
|
if (key === "recordId") continue;
|
|
393
|
-
|
|
394
|
-
values.push(value);
|
|
397
|
+
values[key] = value;
|
|
395
398
|
}
|
|
396
|
-
const
|
|
397
|
-
const
|
|
398
|
-
|
|
399
|
-
|
|
399
|
+
const updateColumns = Object.keys(values).filter((c) => c !== "record_id");
|
|
400
|
+
const query = compiler.insertInto(table).values(values).onConflict(
|
|
401
|
+
(oc) => updateColumns.length > 0 ? oc.column("record_id").doUpdateSet(
|
|
402
|
+
(eb) => Object.fromEntries(updateColumns.map((c) => [c, eb.ref(`excluded.${c}`)]))
|
|
403
|
+
) : oc.column("record_id").doNothing()
|
|
404
|
+
).compile();
|
|
405
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
400
406
|
}
|
|
401
407
|
async getMetadata(typeId, recordId) {
|
|
402
408
|
const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
recordId
|
|
406
|
-
);
|
|
409
|
+
const query = compiler.selectFrom(table).selectAll().where("record_id", "=", recordId).compile();
|
|
410
|
+
const row = this.getRow(query.sql, ...query.parameters);
|
|
407
411
|
if (!row) return null;
|
|
408
412
|
return columnsToMetadataRow(recordId, row);
|
|
409
413
|
}
|
|
@@ -411,11 +415,8 @@ var SqliteDatabaseAdapter = class {
|
|
|
411
415
|
const result = /* @__PURE__ */ new Map();
|
|
412
416
|
if (recordIds.length === 0) return result;
|
|
413
417
|
const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
|
|
414
|
-
const
|
|
415
|
-
const rows = this.allRows(
|
|
416
|
-
`SELECT * FROM ${table} WHERE record_id IN (${placeholders})`,
|
|
417
|
-
...recordIds
|
|
418
|
-
);
|
|
418
|
+
const query = compiler.selectFrom(table).selectAll().where("record_id", "in", recordIds).compile();
|
|
419
|
+
const rows = this.allRows(query.sql, ...query.parameters);
|
|
419
420
|
for (const row of rows) {
|
|
420
421
|
const recordId = row["record_id"];
|
|
421
422
|
result.set(recordId, columnsToMetadataRow(recordId, row));
|
|
@@ -424,7 +425,93 @@ var SqliteDatabaseAdapter = class {
|
|
|
424
425
|
}
|
|
425
426
|
async deleteMetadata(typeId, recordId) {
|
|
426
427
|
const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
|
|
427
|
-
|
|
428
|
+
const query = compiler.deleteFrom(table).where("record_id", "=", recordId).compile();
|
|
429
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
430
|
+
}
|
|
431
|
+
// ---- Cross-app record labels -------------------------------------------
|
|
432
|
+
//
|
|
433
|
+
// The SQL for all of these is built in `@starkeep/storage-adapter`, shared
|
|
434
|
+
// with the DSQL adapter: same columns, same ON CONFLICT set, same cursor
|
|
435
|
+
// predicate, same paging. Only the table name is this backend's business,
|
|
436
|
+
// and it lives in LABELS above.
|
|
437
|
+
async upsertLabels(labels) {
|
|
438
|
+
if (labels.length === 0) return;
|
|
439
|
+
const query = (0, import_storage_adapter2.buildLabelUpsert)(compiler, LABELS, labels);
|
|
440
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
441
|
+
}
|
|
442
|
+
async retractLabels(retractions) {
|
|
443
|
+
for (const r of retractions) {
|
|
444
|
+
const query = (0, import_storage_adapter2.buildLabelRetraction)(compiler, LABELS, r);
|
|
445
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
async replaceLabelValues(replacements) {
|
|
449
|
+
if (replacements.length === 0) return;
|
|
450
|
+
this.getDatabase().exec("BEGIN");
|
|
451
|
+
try {
|
|
452
|
+
for (const r of replacements) {
|
|
453
|
+
const tombstone = (0, import_storage_adapter2.buildLabelValueReplacementTombstone)(compiler, LABELS, r);
|
|
454
|
+
this.runStmt(tombstone.sql, ...tombstone.parameters);
|
|
455
|
+
if (r.values.length > 0) {
|
|
456
|
+
const upsert = (0, import_storage_adapter2.buildLabelUpsert)(
|
|
457
|
+
compiler,
|
|
458
|
+
LABELS,
|
|
459
|
+
r.values.map((value) => ({
|
|
460
|
+
recordId: r.recordId,
|
|
461
|
+
appId: r.appId,
|
|
462
|
+
key: r.key,
|
|
463
|
+
value,
|
|
464
|
+
recordType: r.recordType,
|
|
465
|
+
hlc: r.hlc
|
|
466
|
+
}))
|
|
467
|
+
);
|
|
468
|
+
this.runStmt(upsert.sql, ...upsert.parameters);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
this.getDatabase().exec("COMMIT");
|
|
472
|
+
} catch (error) {
|
|
473
|
+
this.getDatabase().exec("ROLLBACK");
|
|
474
|
+
throw error;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
async getLabelsByRecordIds(recordIds) {
|
|
478
|
+
if (recordIds.length === 0) return /* @__PURE__ */ new Map();
|
|
479
|
+
const query = (0, import_storage_adapter2.buildLabelsByRecordIds)(compiler, LABELS, recordIds);
|
|
480
|
+
return (0, import_storage_adapter2.groupLabelsByRecordId)(
|
|
481
|
+
this.allRows(query.sql, ...query.parameters)
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
async findByLabel(query) {
|
|
485
|
+
const compiled = (0, import_storage_adapter2.buildFindByLabel)(compiler, LABELS, query);
|
|
486
|
+
if (!compiled) return { labels: [], nextCursor: null, hasMore: false };
|
|
487
|
+
const rows = this.allRows(compiled.sql, ...compiled.parameters);
|
|
488
|
+
return (0, import_storage_adapter2.paginateFindByLabel)(rows, query.limit);
|
|
489
|
+
}
|
|
490
|
+
// ---- Label sync ---------------------------------------------------------
|
|
491
|
+
async putLabel(label) {
|
|
492
|
+
const query = (0, import_storage_adapter2.buildLabelSnapshotUpsert)(compiler, LABELS, label);
|
|
493
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
494
|
+
}
|
|
495
|
+
async getLabel(recordId, appId, key, value) {
|
|
496
|
+
const query = (0, import_storage_adapter2.buildGetLabel)(compiler, LABELS, recordId, appId, key, value);
|
|
497
|
+
const row = this.getRow(query.sql, ...query.parameters);
|
|
498
|
+
return row ? (0, import_storage_adapter2.rowToLabel)(row) : null;
|
|
499
|
+
}
|
|
500
|
+
async queryLabels(query) {
|
|
501
|
+
const compiled = (0, import_storage_adapter2.buildQueryLabels)(compiler, LABELS, query);
|
|
502
|
+
const rows = this.allRows(compiled.sql, ...compiled.parameters);
|
|
503
|
+
return (0, import_storage_adapter2.paginateLabelScan)(rows, query.limit);
|
|
504
|
+
}
|
|
505
|
+
async getLabelNodeWatermarks() {
|
|
506
|
+
const query = (0, import_storage_adapter2.buildLabelNodeWatermarks)(compiler, LABELS);
|
|
507
|
+
const rows = this.allRows(query.sql);
|
|
508
|
+
const out = {};
|
|
509
|
+
for (const row of rows) out[row.node_id] = (0, import_protocol_primitives3.deserializeHLC)(row.max_updated_at);
|
|
510
|
+
return out;
|
|
511
|
+
}
|
|
512
|
+
async tombstoneLabelsForRecord(recordId, hlc) {
|
|
513
|
+
const query = (0, import_storage_adapter2.buildTombstoneLabelsForRecord)(compiler, LABELS, recordId, hlc);
|
|
514
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
428
515
|
}
|
|
429
516
|
};
|
|
430
517
|
function columnsToMetadataRow(recordId, columns) {
|
|
@@ -453,34 +540,38 @@ function rowToNamespace(r) {
|
|
|
453
540
|
};
|
|
454
541
|
}
|
|
455
542
|
function upsertAppSyncableNamespace(db, appId, tables, filesEnabled) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
543
|
+
const query = compiler.insertInto("app_syncable_namespaces").values({
|
|
544
|
+
app_id: appId,
|
|
545
|
+
tables_json: JSON.stringify(tables),
|
|
546
|
+
files_enabled: filesEnabled ? 1 : 0
|
|
547
|
+
}).onConflict(
|
|
548
|
+
(oc) => oc.column("app_id").doUpdateSet((eb) => ({
|
|
549
|
+
tables_json: eb.ref("excluded.tables_json"),
|
|
550
|
+
files_enabled: eb.ref("excluded.files_enabled")
|
|
551
|
+
}))
|
|
552
|
+
).compile();
|
|
553
|
+
db.prepare(query.sql).run(...query.parameters);
|
|
463
554
|
}
|
|
464
555
|
function deleteAppSyncableNamespace(db, appId) {
|
|
465
|
-
|
|
556
|
+
const query = compiler.deleteFrom("app_syncable_namespaces").where("app_id", "=", appId).compile();
|
|
557
|
+
db.prepare(query.sql).run(...query.parameters);
|
|
466
558
|
}
|
|
467
559
|
function getAppSyncableNamespace(db, appId) {
|
|
468
|
-
const
|
|
469
|
-
|
|
470
|
-
).get(appId);
|
|
560
|
+
const query = compiler.selectFrom("app_syncable_namespaces").select(["app_id", "tables_json", "files_enabled"]).where("app_id", "=", appId).compile();
|
|
561
|
+
const row = db.prepare(query.sql).get(...query.parameters);
|
|
471
562
|
if (!row) return null;
|
|
472
563
|
return rowToNamespace(row);
|
|
473
564
|
}
|
|
474
565
|
function listAppSyncableNamespaces(db) {
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
).all();
|
|
566
|
+
const query = compiler.selectFrom("app_syncable_namespaces").select(["app_id", "tables_json", "files_enabled"]).compile();
|
|
567
|
+
const rows = db.prepare(query.sql).all();
|
|
478
568
|
return rows.map(rowToNamespace);
|
|
479
569
|
}
|
|
480
570
|
var SqliteAppSyncableNamespaceStore = class {
|
|
481
571
|
constructor(db) {
|
|
482
572
|
this.db = db;
|
|
483
573
|
}
|
|
574
|
+
db;
|
|
484
575
|
get(appId) {
|
|
485
576
|
return getAppSyncableNamespace(this.db, appId);
|
|
486
577
|
}
|
|
@@ -490,12 +581,21 @@ var SqliteAppSyncableNamespaceStore = class {
|
|
|
490
581
|
};
|
|
491
582
|
|
|
492
583
|
// src/app-syncable/apply.ts
|
|
584
|
+
var import_kysely4 = require("kysely");
|
|
493
585
|
var import_protocol_primitives4 = require("@starkeep/protocol-primitives");
|
|
586
|
+
function runCompiled(db, compiled) {
|
|
587
|
+
db.prepare(compiled.sql).run(...compiled.parameters);
|
|
588
|
+
}
|
|
589
|
+
function allCompiled(db, compiled) {
|
|
590
|
+
return db.prepare(compiled.sql).all(...compiled.parameters);
|
|
591
|
+
}
|
|
494
592
|
var SqliteAppSyncableApplier = class {
|
|
495
593
|
constructor(db, namespace) {
|
|
496
594
|
this.db = db;
|
|
497
595
|
this.namespace = namespace;
|
|
498
596
|
}
|
|
597
|
+
db;
|
|
598
|
+
namespace;
|
|
499
599
|
apply(entry) {
|
|
500
600
|
const ns = this.namespace.get(entry.appId);
|
|
501
601
|
if (!ns) {
|
|
@@ -523,27 +623,22 @@ var SqliteAppSyncableApplier = class {
|
|
|
523
623
|
const row = withNodeId(entry.row ?? {}, entry);
|
|
524
624
|
const cols = Object.keys(row);
|
|
525
625
|
if (cols.length === 0) return;
|
|
526
|
-
const colList = cols.map(q).join(", ");
|
|
527
|
-
const placeholders = cols.map(() => "?").join(", ");
|
|
528
|
-
const values = cols.map((c) => row[c]);
|
|
529
|
-
if (pkColumns.length === 0) {
|
|
530
|
-
this.db.prepare(`INSERT OR IGNORE INTO ${q(fullName)} (${colList}) VALUES (${placeholders})`).run(...values);
|
|
531
|
-
return;
|
|
532
|
-
}
|
|
533
|
-
const conflictTarget = pkColumns.map(q).join(", ");
|
|
534
626
|
const updateCols = cols.filter((c) => !pkColumns.includes(c));
|
|
535
|
-
if (updateCols.length === 0) {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
627
|
+
if (pkColumns.length === 0 || updateCols.length === 0) {
|
|
628
|
+
runCompiled(
|
|
629
|
+
this.db,
|
|
630
|
+
compiler.insertInto(fullName).orIgnore().values({ ...row }).compile()
|
|
631
|
+
);
|
|
539
632
|
return;
|
|
540
633
|
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
634
|
+
runCompiled(
|
|
635
|
+
this.db,
|
|
636
|
+
compiler.insertInto(fullName).values({ ...row }).onConflict(
|
|
637
|
+
(oc) => oc.columns(pkColumns).doUpdateSet(
|
|
638
|
+
(eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
|
|
639
|
+
).where(import_kysely4.sql.ref("excluded.updated_at"), ">", import_kysely4.sql.ref(`${fullName}.updated_at`))
|
|
640
|
+
).compile()
|
|
641
|
+
);
|
|
547
642
|
}
|
|
548
643
|
applyUpdate(fullName, entry) {
|
|
549
644
|
const rawPatch = entry.row ?? {};
|
|
@@ -552,48 +647,39 @@ var SqliteAppSyncableApplier = class {
|
|
|
552
647
|
const patchCols = Object.keys(patch);
|
|
553
648
|
const whereCols = Object.keys(where);
|
|
554
649
|
if (patchCols.length === 0) return;
|
|
555
|
-
const setClause = patchCols.map((c) => `${q(c)} = ?`).join(", ");
|
|
556
650
|
const incomingUpdatedAt = patch["updated_at"];
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
this.db.prepare(`UPDATE ${q(fullName)} SET ${setClause}${whereClause}`).run(...params);
|
|
651
|
+
let query = compiler.updateTable(fullName).set({ ...patch });
|
|
652
|
+
for (const c of whereCols) {
|
|
653
|
+
query = query.where(c, "=", where[c]);
|
|
654
|
+
}
|
|
655
|
+
if (incomingUpdatedAt) {
|
|
656
|
+
query = query.where("updated_at", "<", incomingUpdatedAt);
|
|
657
|
+
}
|
|
658
|
+
runCompiled(this.db, query.compile());
|
|
566
659
|
}
|
|
567
660
|
applyDelete(fullName, entry) {
|
|
568
661
|
const where = entry.where ?? {};
|
|
569
662
|
const whereCols = Object.keys(where);
|
|
570
663
|
const incomingUpdatedAt = entry.row?.["updated_at"];
|
|
571
664
|
const ts = incomingUpdatedAt ?? (0, import_protocol_primitives4.serializeHLC)(entry.timestamp);
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
nodeIdOf(ts, entry),
|
|
581
|
-
...whereCols.map((c) => where[c]),
|
|
582
|
-
ts
|
|
583
|
-
// for the LWW updated_at < ? condition
|
|
584
|
-
];
|
|
585
|
-
this.db.prepare(
|
|
586
|
-
`UPDATE ${q(fullName)} SET deleted_at = ?, updated_at = ?, node_id = ?${whereClause}`
|
|
587
|
-
).run(...params);
|
|
665
|
+
let query = compiler.updateTable(fullName).set({ deleted_at: ts, updated_at: ts, node_id: nodeIdOf(ts, entry) });
|
|
666
|
+
for (const c of whereCols) {
|
|
667
|
+
query = query.where(c, "=", where[c]);
|
|
668
|
+
}
|
|
669
|
+
query = query.where(
|
|
670
|
+
(eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)])
|
|
671
|
+
);
|
|
672
|
+
runCompiled(this.db, query.compile());
|
|
588
673
|
}
|
|
589
674
|
/** See `ScanCapableApplier.getNodeWatermarks`. Missing table → `{}`. */
|
|
590
675
|
async getNodeWatermarks(appId, table) {
|
|
591
676
|
const fullName = appSyncableTableName(appId, table);
|
|
592
677
|
let rows;
|
|
593
678
|
try {
|
|
594
|
-
rows =
|
|
595
|
-
|
|
596
|
-
|
|
679
|
+
rows = allCompiled(
|
|
680
|
+
this.db,
|
|
681
|
+
compiler.selectFrom(fullName).select(({ fn }) => ["node_id", fn.max("updated_at").as("max_updated_at")]).groupBy("node_id").compile()
|
|
682
|
+
);
|
|
597
683
|
} catch {
|
|
598
684
|
return {};
|
|
599
685
|
}
|
|
@@ -616,15 +702,11 @@ var SqliteAppSyncableApplier = class {
|
|
|
616
702
|
const limit = options?.limit;
|
|
617
703
|
let rows;
|
|
618
704
|
try {
|
|
705
|
+
let query = compiler.selectFrom(fullName).selectAll().where("updated_at", ">", floor).orderBy("updated_at", "asc");
|
|
619
706
|
if (limit !== void 0) {
|
|
620
|
-
|
|
621
|
-
`SELECT * FROM ${q(fullName)} WHERE updated_at > ? ORDER BY updated_at ASC LIMIT ?`
|
|
622
|
-
).all(floor, limit + 1);
|
|
623
|
-
} else {
|
|
624
|
-
rows = this.db.prepare(
|
|
625
|
-
`SELECT * FROM ${q(fullName)} WHERE updated_at > ? ORDER BY updated_at ASC`
|
|
626
|
-
).all(floor);
|
|
707
|
+
query = query.limit(limit + 1);
|
|
627
708
|
}
|
|
709
|
+
rows = allCompiled(this.db, query.compile());
|
|
628
710
|
} catch {
|
|
629
711
|
return { rows: [], nextCursor: null, hasMore: false };
|
|
630
712
|
}
|
|
@@ -638,16 +720,13 @@ var SqliteAppSyncableApplier = class {
|
|
|
638
720
|
queryRows(appId, table, where) {
|
|
639
721
|
const fullName = appSyncableTableName(appId, table);
|
|
640
722
|
const whereCols = where ? Object.keys(where) : [];
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
return this.db
|
|
723
|
+
let query = compiler.selectFrom(fullName).selectAll().where("deleted_at", "is", null);
|
|
724
|
+
for (const c of whereCols) {
|
|
725
|
+
query = query.where(c, "=", where[c]);
|
|
726
|
+
}
|
|
727
|
+
return allCompiled(this.db, query.compile());
|
|
646
728
|
}
|
|
647
729
|
};
|
|
648
|
-
function q(name) {
|
|
649
|
-
return `"${name}"`;
|
|
650
|
-
}
|
|
651
730
|
function withNodeId(row, entry) {
|
|
652
731
|
return { ...row, node_id: nodeIdOf(row["updated_at"], entry) };
|
|
653
732
|
}
|
|
@@ -679,6 +758,7 @@ function rowToEntry(appId, table, row) {
|
|
|
679
758
|
getAppSyncableNamespace,
|
|
680
759
|
initializeLocalSchema,
|
|
681
760
|
listAppSyncableNamespaces,
|
|
761
|
+
sqliteCompiler,
|
|
682
762
|
upsertAppSyncableNamespace
|
|
683
763
|
});
|
|
684
764
|
//# sourceMappingURL=index.cjs.map
|