@starkeep/storage-sqlite 0.2.0 → 0.3.0

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 CHANGED
@@ -24,11 +24,11 @@ __export(index_exports, {
24
24
  SqliteAppSyncableNamespaceStore: () => SqliteAppSyncableNamespaceStore,
25
25
  SqliteDatabaseAdapter: () => SqliteDatabaseAdapter,
26
26
  appSyncableTableName: () => appSyncableTableName,
27
- createSqliteAccessPolicyStore: () => createSqliteAccessPolicyStore,
28
27
  deleteAppSyncableNamespace: () => deleteAppSyncableNamespace,
29
28
  getAppSyncableNamespace: () => getAppSyncableNamespace,
30
29
  initializeLocalSchema: () => initializeLocalSchema,
31
30
  listAppSyncableNamespaces: () => listAppSyncableNamespaces,
31
+ sqliteCompiler: () => compiler,
32
32
  upsertAppSyncableNamespace: () => upsertAppSyncableNamespace
33
33
  });
34
34
  module.exports = __toCommonJS(index_exports);
@@ -38,16 +38,19 @@ var import_node_sqlite = require("sqlite");
38
38
  var import_node_fs = require("fs");
39
39
  var import_node_path = require("path");
40
40
  var import_protocol_primitives3 = require("@starkeep/protocol-primitives");
41
- var import_storage_adapter = require("@starkeep/storage-adapter");
41
+ var import_storage_adapter2 = require("@starkeep/storage-adapter");
42
+ var import_kysely3 = require("kysely");
42
43
 
43
44
  // src/serialization.ts
44
45
  var import_protocol_primitives = require("@starkeep/protocol-primitives");
46
+ var import_storage_adapter = require("@starkeep/storage-adapter");
45
47
  function recordToRow(record) {
46
48
  return {
47
49
  id: record.id,
48
50
  type: record.type,
49
51
  created_at: (0, import_protocol_primitives.serializeHLC)(record.createdAt),
50
52
  updated_at: (0, import_protocol_primitives.serializeHLC)(record.updatedAt),
53
+ node_id: record.updatedAt.nodeId,
51
54
  deleted_at: record.deletedAt ? (0, import_protocol_primitives.serializeHLC)(record.deletedAt) : null,
52
55
  version: record.version,
53
56
  content_hash: record.contentHash,
@@ -161,96 +164,99 @@ function applyFilter(qb, filter) {
161
164
  }
162
165
 
163
166
  // src/schema/bootstrap.ts
167
+ var import_kysely2 = require("kysely");
164
168
  var import_protocol_primitives2 = require("@starkeep/protocol-primitives");
165
- function initializeLocalSchema(db) {
169
+ function applyLocalSchemaDdl(db) {
166
170
  db.exec("PRAGMA journal_mode = WAL");
167
171
  db.exec("PRAGMA foreign_keys = ON");
168
- db.exec(`
169
- CREATE TABLE IF NOT EXISTS shared_records (
170
- id TEXT PRIMARY KEY,
171
- type TEXT NOT NULL,
172
- created_at TEXT NOT NULL,
173
- updated_at TEXT NOT NULL,
174
- deleted_at TEXT,
175
- version INTEGER NOT NULL DEFAULT 1,
176
- content_hash TEXT NOT NULL,
177
- object_storage_key TEXT NOT NULL,
178
- mime_type TEXT,
179
- size_bytes INTEGER NOT NULL,
180
- original_filename TEXT,
181
- origin_app_id TEXT NOT NULL,
182
- parent_id TEXT
183
- )
184
- `);
185
- db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_type ON shared_records(type)");
186
- db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_origin_app ON shared_records(origin_app_id)");
187
- db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_parent_id ON shared_records(parent_id)");
188
172
  db.exec(
189
- "CREATE UNIQUE INDEX IF NOT EXISTS uq_shared_records_filename_hash ON shared_records(original_filename, content_hash) WHERE deleted_at IS NULL AND original_filename IS NOT NULL"
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
236
+ );
237
+ db.exec(
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
190
239
  );
191
- db.exec(`
192
- CREATE TABLE IF NOT EXISTS shared_access_grants (
193
- app_id TEXT NOT NULL,
194
- type_id TEXT NOT NULL,
195
- access TEXT NOT NULL CHECK (access IN ('read', 'readwrite')),
196
- metadata_write INTEGER NOT NULL DEFAULT 0,
197
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
198
- PRIMARY KEY (app_id, type_id)
199
- )
200
- `);
201
- db.exec("CREATE INDEX IF NOT EXISTS idx_shared_access_grants_app ON shared_access_grants(app_id)");
202
- db.exec(`
203
- CREATE TABLE IF NOT EXISTS shared_app_registry (
204
- app_id TEXT PRIMARY KEY,
205
- name TEXT NOT NULL,
206
- version TEXT NOT NULL,
207
- tier TEXT NOT NULL DEFAULT 'app',
208
- manifest TEXT NOT NULL,
209
- status TEXT NOT NULL DEFAULT 'installing',
210
- hmac_secret TEXT NOT NULL,
211
- installed_at TEXT NOT NULL DEFAULT (datetime('now')),
212
- updated_at TEXT NOT NULL DEFAULT (datetime('now'))
213
- )
214
- `);
215
- db.exec(`
216
- CREATE TABLE IF NOT EXISTS shared_app_install_steps (
217
- app_id TEXT NOT NULL,
218
- operation TEXT NOT NULL CHECK (operation IN ('install', 'uninstall')),
219
- step TEXT NOT NULL,
220
- status TEXT NOT NULL CHECK (status IN ('pending', 'done', 'failed')),
221
- error TEXT,
222
- updated_at TEXT NOT NULL DEFAULT (datetime('now')),
223
- PRIMARY KEY (app_id, operation, step)
224
- )
225
- `);
226
- db.exec(`
227
- CREATE TABLE IF NOT EXISTS app_syncable_namespaces (
228
- app_id TEXT PRIMARY KEY,
229
- tables_json TEXT NOT NULL,
230
- files_enabled INTEGER NOT NULL DEFAULT 0,
231
- created_at TEXT NOT NULL DEFAULT (datetime('now'))
232
- )
233
- `);
234
- db.exec(`
235
- CREATE TABLE IF NOT EXISTS access_policies (
236
- policy_id TEXT PRIMARY KEY,
237
- subject_type TEXT NOT NULL,
238
- subject_id TEXT NOT NULL,
239
- resource_type TEXT NOT NULL,
240
- resource_id TEXT NOT NULL,
241
- permissions TEXT NOT NULL,
242
- granted_at TEXT NOT NULL,
243
- expires_at TEXT
244
- )
245
- `);
246
- db.exec("CREATE INDEX IF NOT EXISTS idx_access_policies_subject ON access_policies(subject_type, subject_id)");
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("SELECT 1").get();
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 import_storage_adapter.StorageError("Database not initialized. Call init() first.");
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,43 +299,53 @@ var SqliteDatabaseAdapter = class {
293
299
  getRawDatabase() {
294
300
  return this.getDatabase();
295
301
  }
296
- runStmt(sql, ...params) {
297
- this.getDatabase().prepare(sql).run(...params);
302
+ runStmt(sql3, ...params) {
303
+ this.getDatabase().prepare(sql3).run(...params);
298
304
  }
299
- getRow(sql, ...params) {
300
- return this.getDatabase().prepare(sql).get(
305
+ getRow(sql3, ...params) {
306
+ return this.getDatabase().prepare(sql3).get(
301
307
  ...params
302
308
  );
303
309
  }
304
- allRows(sql, ...params) {
305
- return this.getDatabase().prepare(sql).all(
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 columns = Object.keys(row);
312
- const placeholders = columns.map(() => "?").join(", ");
313
- const updates = columns.filter((column) => column !== "id").map((column) => `${column} = excluded.${column}`).join(", ");
314
- const sql = `INSERT INTO shared_records (${columns.join(", ")}) VALUES (${placeholders}) ON CONFLICT(id) DO UPDATE SET ${updates}`;
315
- this.runStmt(sql, ...Object.values(row));
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 row = this.getRow("SELECT * FROM shared_records WHERE id = ?", id);
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
- this.runStmt(
324
- "UPDATE shared_records SET deleted_at = ?, updated_at = ? WHERE id = ?",
325
- ts,
326
- ts,
327
- id
328
- );
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);
336
+ }
337
+ async getNodeWatermarks() {
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);
340
+ const out = {};
341
+ for (const row of rows) {
342
+ out[row.node_id] = (0, import_protocol_primitives3.deserializeHLC)(row.max_updated_at);
343
+ }
344
+ return out;
329
345
  }
330
346
  async query(query) {
331
- const { sql, params } = buildSelectQuery(query);
332
- const rows = this.allRows(sql, ...params);
347
+ const { sql: sql3, params } = buildSelectQuery(query);
348
+ const rows = this.allRows(sql3, ...params);
333
349
  const limit = query.limit;
334
350
  const hasMore = limit ? rows.length > limit : false;
335
351
  const resultRows = hasMore ? rows.slice(0, limit) : rows;
@@ -370,29 +386,28 @@ var SqliteDatabaseAdapter = class {
370
386
  } catch (error) {
371
387
  this.getDatabase().exec("ROLLBACK TO SAVEPOINT starkeep_tx");
372
388
  this.getDatabase().exec("RELEASE SAVEPOINT starkeep_tx");
373
- throw new import_storage_adapter.TransactionError("Transaction failed", error);
389
+ throw new import_storage_adapter2.TransactionError("Transaction failed", error);
374
390
  }
375
391
  }
376
392
  async putMetadata(typeId, row) {
377
393
  const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
378
- const cols = ["record_id"];
379
- const values = [row.recordId];
394
+ const values = { record_id: row.recordId };
380
395
  for (const [key, value] of Object.entries(row)) {
381
396
  if (key === "recordId") continue;
382
- cols.push(key);
383
- values.push(value);
397
+ values[key] = value;
384
398
  }
385
- const placeholders = cols.map(() => "?").join(", ");
386
- const updates = cols.filter((c) => c !== "record_id").map((c) => `${c} = excluded.${c}`).join(", ");
387
- const sql = updates ? `INSERT INTO ${table} (${cols.join(", ")}) VALUES (${placeholders}) ON CONFLICT(record_id) DO UPDATE SET ${updates}` : `INSERT INTO ${table} (${cols.join(", ")}) VALUES (${placeholders}) ON CONFLICT(record_id) DO NOTHING`;
388
- this.runStmt(sql, ...values);
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);
389
406
  }
390
407
  async getMetadata(typeId, recordId) {
391
408
  const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
392
- const row = this.getRow(
393
- `SELECT * FROM ${table} WHERE record_id = ?`,
394
- recordId
395
- );
409
+ const query = compiler.selectFrom(table).selectAll().where("record_id", "=", recordId).compile();
410
+ const row = this.getRow(query.sql, ...query.parameters);
396
411
  if (!row) return null;
397
412
  return columnsToMetadataRow(recordId, row);
398
413
  }
@@ -400,11 +415,8 @@ var SqliteDatabaseAdapter = class {
400
415
  const result = /* @__PURE__ */ new Map();
401
416
  if (recordIds.length === 0) return result;
402
417
  const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
403
- const placeholders = recordIds.map(() => "?").join(", ");
404
- const rows = this.allRows(
405
- `SELECT * FROM ${table} WHERE record_id IN (${placeholders})`,
406
- ...recordIds
407
- );
418
+ const query = compiler.selectFrom(table).selectAll().where("record_id", "in", recordIds).compile();
419
+ const rows = this.allRows(query.sql, ...query.parameters);
408
420
  for (const row of rows) {
409
421
  const recordId = row["record_id"];
410
422
  result.set(recordId, columnsToMetadataRow(recordId, row));
@@ -413,7 +425,93 @@ var SqliteDatabaseAdapter = class {
413
425
  }
414
426
  async deleteMetadata(typeId, recordId) {
415
427
  const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
416
- this.runStmt(`DELETE FROM ${table} WHERE record_id = ?`, recordId);
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);
417
515
  }
418
516
  };
419
517
  function columnsToMetadataRow(recordId, columns) {
@@ -425,61 +523,6 @@ function columnsToMetadataRow(recordId, columns) {
425
523
  return row;
426
524
  }
427
525
 
428
- // src/control-plane-stores.ts
429
- var import_protocol_primitives4 = require("@starkeep/protocol-primitives");
430
- function createSqliteAccessPolicyStore(db) {
431
- return {
432
- async putPolicy(policy) {
433
- db.prepare(
434
- `INSERT INTO access_policies (
435
- policy_id, subject_type, subject_id, resource_type, resource_id,
436
- permissions, granted_at, expires_at
437
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
438
- ON CONFLICT(policy_id) DO UPDATE SET
439
- subject_type = excluded.subject_type,
440
- subject_id = excluded.subject_id,
441
- resource_type = excluded.resource_type,
442
- resource_id = excluded.resource_id,
443
- permissions = excluded.permissions,
444
- granted_at = excluded.granted_at,
445
- expires_at = excluded.expires_at`
446
- ).run(
447
- policy.policyId,
448
- policy.subjectType,
449
- policy.subjectId,
450
- policy.resourceType,
451
- policy.resourceId,
452
- policy.permissions.join(","),
453
- (0, import_protocol_primitives4.serializeHLC)(policy.grantedAt),
454
- policy.expiresAt ? (0, import_protocol_primitives4.serializeHLC)(policy.expiresAt) : null
455
- );
456
- },
457
- async getPolicy(policyId) {
458
- const row = db.prepare("SELECT * FROM access_policies WHERE policy_id = ?").get(policyId);
459
- return row ? rowToPolicy(row) : null;
460
- },
461
- async listPolicies() {
462
- const rows = db.prepare("SELECT * FROM access_policies").all();
463
- return rows.map(rowToPolicy);
464
- },
465
- async deletePolicy(policyId) {
466
- db.prepare("DELETE FROM access_policies WHERE policy_id = ?").run(policyId);
467
- }
468
- };
469
- }
470
- function rowToPolicy(row) {
471
- return {
472
- policyId: (0, import_protocol_primitives4.createStarkeepId)(row.policy_id),
473
- subjectType: row.subject_type,
474
- subjectId: row.subject_id,
475
- resourceType: row.resource_type,
476
- resourceId: row.resource_id,
477
- permissions: row.permissions.split(",").filter(Boolean),
478
- grantedAt: (0, import_protocol_primitives4.deserializeHLC)(row.granted_at),
479
- expiresAt: row.expires_at ? (0, import_protocol_primitives4.deserializeHLC)(row.expires_at) : null
480
- };
481
- }
482
-
483
526
  // src/app-syncable/namespace.ts
484
527
  function normalizeAppId(appId) {
485
528
  return appId.toLowerCase().replace(/-/g, "_");
@@ -497,34 +540,38 @@ function rowToNamespace(r) {
497
540
  };
498
541
  }
499
542
  function upsertAppSyncableNamespace(db, appId, tables, filesEnabled) {
500
- db.prepare(
501
- `INSERT INTO app_syncable_namespaces (app_id, tables_json, files_enabled)
502
- VALUES (?, ?, ?)
503
- ON CONFLICT(app_id) DO UPDATE SET
504
- tables_json = excluded.tables_json,
505
- files_enabled = excluded.files_enabled`
506
- ).run(appId, JSON.stringify(tables), filesEnabled ? 1 : 0);
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);
507
554
  }
508
555
  function deleteAppSyncableNamespace(db, appId) {
509
- db.prepare("DELETE FROM app_syncable_namespaces WHERE app_id = ?").run(appId);
556
+ const query = compiler.deleteFrom("app_syncable_namespaces").where("app_id", "=", appId).compile();
557
+ db.prepare(query.sql).run(...query.parameters);
510
558
  }
511
559
  function getAppSyncableNamespace(db, appId) {
512
- const row = db.prepare(
513
- "SELECT app_id, tables_json, files_enabled FROM app_syncable_namespaces WHERE app_id = ?"
514
- ).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);
515
562
  if (!row) return null;
516
563
  return rowToNamespace(row);
517
564
  }
518
565
  function listAppSyncableNamespaces(db) {
519
- const rows = db.prepare(
520
- "SELECT app_id, tables_json, files_enabled FROM app_syncable_namespaces"
521
- ).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();
522
568
  return rows.map(rowToNamespace);
523
569
  }
524
570
  var SqliteAppSyncableNamespaceStore = class {
525
571
  constructor(db) {
526
572
  this.db = db;
527
573
  }
574
+ db;
528
575
  get(appId) {
529
576
  return getAppSyncableNamespace(this.db, appId);
530
577
  }
@@ -534,12 +581,21 @@ var SqliteAppSyncableNamespaceStore = class {
534
581
  };
535
582
 
536
583
  // src/app-syncable/apply.ts
537
- var import_protocol_primitives5 = require("@starkeep/protocol-primitives");
584
+ var import_kysely4 = require("kysely");
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
+ }
538
592
  var SqliteAppSyncableApplier = class {
539
593
  constructor(db, namespace) {
540
594
  this.db = db;
541
595
  this.namespace = namespace;
542
596
  }
597
+ db;
598
+ namespace;
543
599
  apply(entry) {
544
600
  const ns = this.namespace.get(entry.appId);
545
601
  if (!ns) {
@@ -564,69 +620,74 @@ var SqliteAppSyncableApplier = class {
564
620
  }
565
621
  }
566
622
  applyInsert(fullName, pkColumns, entry) {
567
- const row = entry.row ?? {};
623
+ const row = withNodeId(entry.row ?? {}, entry);
568
624
  const cols = Object.keys(row);
569
625
  if (cols.length === 0) return;
570
- const colList = cols.map(q).join(", ");
571
- const placeholders = cols.map(() => "?").join(", ");
572
- const values = cols.map((c) => row[c]);
573
- if (pkColumns.length === 0) {
574
- this.db.prepare(`INSERT OR IGNORE INTO ${q(fullName)} (${colList}) VALUES (${placeholders})`).run(...values);
575
- return;
576
- }
577
- const conflictTarget = pkColumns.map(q).join(", ");
578
626
  const updateCols = cols.filter((c) => !pkColumns.includes(c));
579
- if (updateCols.length === 0) {
580
- this.db.prepare(
581
- `INSERT OR IGNORE INTO ${q(fullName)} (${colList}) VALUES (${placeholders})`
582
- ).run(...values);
627
+ if (pkColumns.length === 0 || updateCols.length === 0) {
628
+ runCompiled(
629
+ this.db,
630
+ compiler.insertInto(fullName).orIgnore().values({ ...row }).compile()
631
+ );
583
632
  return;
584
633
  }
585
- const setClause = updateCols.map((c) => `${q(c)} = excluded.${q(c)}`).join(", ");
586
- this.db.prepare(
587
- `INSERT INTO ${q(fullName)} (${colList}) VALUES (${placeholders})
588
- ON CONFLICT(${conflictTarget}) DO UPDATE SET ${setClause}
589
- WHERE excluded.updated_at > ${q(fullName)}.updated_at`
590
- ).run(...values);
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
+ );
591
642
  }
592
643
  applyUpdate(fullName, entry) {
593
- const patch = entry.row ?? {};
644
+ const rawPatch = entry.row ?? {};
645
+ const patch = rawPatch["updated_at"] ? withNodeId(rawPatch, entry) : rawPatch;
594
646
  const where = entry.where ?? {};
595
647
  const patchCols = Object.keys(patch);
596
648
  const whereCols = Object.keys(where);
597
649
  if (patchCols.length === 0) return;
598
- const setClause = patchCols.map((c) => `${q(c)} = ?`).join(", ");
599
650
  const incomingUpdatedAt = patch["updated_at"];
600
- const conditions = whereCols.map((c) => `${q(c)} = ?`);
601
- if (incomingUpdatedAt) conditions.push(`updated_at < ?`);
602
- const whereClause = conditions.length ? " WHERE " + conditions.join(" AND ") : "";
603
- const params = [
604
- ...patchCols.map((c) => patch[c]),
605
- ...whereCols.map((c) => where[c])
606
- ];
607
- if (incomingUpdatedAt) params.push(incomingUpdatedAt);
608
- 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());
609
659
  }
610
660
  applyDelete(fullName, entry) {
611
661
  const where = entry.where ?? {};
612
662
  const whereCols = Object.keys(where);
613
663
  const incomingUpdatedAt = entry.row?.["updated_at"];
614
- const ts = incomingUpdatedAt ?? (0, import_protocol_primitives5.serializeHLC)(entry.timestamp);
615
- const conditions = [
616
- ...whereCols.map((c) => `${q(c)} = ?`),
617
- `(updated_at IS NULL OR updated_at < ?)`
618
- ];
619
- const whereClause = " WHERE " + conditions.join(" AND ");
620
- const params = [
621
- ts,
622
- ts,
623
- ...whereCols.map((c) => where[c]),
624
- ts
625
- // for the LWW updated_at < ? condition
626
- ];
627
- this.db.prepare(
628
- `UPDATE ${q(fullName)} SET deleted_at = ?, updated_at = ?${whereClause}`
629
- ).run(...params);
664
+ const ts = incomingUpdatedAt ?? (0, import_protocol_primitives4.serializeHLC)(entry.timestamp);
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());
673
+ }
674
+ /** See `ScanCapableApplier.getNodeWatermarks`. Missing table → `{}`. */
675
+ async getNodeWatermarks(appId, table) {
676
+ const fullName = appSyncableTableName(appId, table);
677
+ let rows;
678
+ try {
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
+ );
683
+ } catch {
684
+ return {};
685
+ }
686
+ const out = {};
687
+ for (const row of rows) {
688
+ out[row.node_id] = (0, import_protocol_primitives4.deserializeHLC)(row.max_updated_at);
689
+ }
690
+ return out;
630
691
  }
631
692
  /**
632
693
  * Pull-side synthesis: return rows updated after `sinceHlcStr` (or `cursor`
@@ -641,15 +702,11 @@ var SqliteAppSyncableApplier = class {
641
702
  const limit = options?.limit;
642
703
  let rows;
643
704
  try {
705
+ let query = compiler.selectFrom(fullName).selectAll().where("updated_at", ">", floor).orderBy("updated_at", "asc");
644
706
  if (limit !== void 0) {
645
- rows = this.db.prepare(
646
- `SELECT * FROM ${q(fullName)} WHERE updated_at > ? ORDER BY updated_at ASC LIMIT ?`
647
- ).all(floor, limit + 1);
648
- } else {
649
- rows = this.db.prepare(
650
- `SELECT * FROM ${q(fullName)} WHERE updated_at > ? ORDER BY updated_at ASC`
651
- ).all(floor);
707
+ query = query.limit(limit + 1);
652
708
  }
709
+ rows = allCompiled(this.db, query.compile());
653
710
  } catch {
654
711
  return { rows: [], nextCursor: null, hasMore: false };
655
712
  }
@@ -663,20 +720,29 @@ var SqliteAppSyncableApplier = class {
663
720
  queryRows(appId, table, where) {
664
721
  const fullName = appSyncableTableName(appId, table);
665
722
  const whereCols = where ? Object.keys(where) : [];
666
- const whereClause = [
667
- "deleted_at IS NULL",
668
- ...whereCols.map((c) => `${q(c)} = ?`)
669
- ].join(" AND ");
670
- return this.db.prepare(`SELECT * FROM ${q(fullName)} WHERE ${whereClause}`).all(...whereCols.map((c) => where[c]));
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());
671
728
  }
672
729
  };
673
- function q(name) {
674
- return `"${name}"`;
730
+ function withNodeId(row, entry) {
731
+ return { ...row, node_id: nodeIdOf(row["updated_at"], entry) };
732
+ }
733
+ function nodeIdOf(updatedAt, entry) {
734
+ if (typeof updatedAt === "string") {
735
+ try {
736
+ return (0, import_protocol_primitives4.deserializeHLC)(updatedAt).nodeId;
737
+ } catch {
738
+ }
739
+ }
740
+ return entry.timestamp.nodeId;
675
741
  }
676
742
  function rowToEntry(appId, table, row) {
677
743
  const updatedAtStr = row["updated_at"];
678
744
  const deletedAtStr = row["deleted_at"];
679
- const timestamp = (0, import_protocol_primitives5.deserializeHLC)(updatedAtStr);
745
+ const timestamp = (0, import_protocol_primitives4.deserializeHLC)(updatedAtStr);
680
746
  if (deletedAtStr) {
681
747
  return { timestamp, appId, table, op: "delete", row };
682
748
  }
@@ -688,11 +754,11 @@ function rowToEntry(appId, table, row) {
688
754
  SqliteAppSyncableNamespaceStore,
689
755
  SqliteDatabaseAdapter,
690
756
  appSyncableTableName,
691
- createSqliteAccessPolicyStore,
692
757
  deleteAppSyncableNamespace,
693
758
  getAppSyncableNamespace,
694
759
  initializeLocalSchema,
695
760
  listAppSyncableNamespaces,
761
+ sqliteCompiler,
696
762
  upsertAppSyncableNamespace
697
763
  });
698
764
  //# sourceMappingURL=index.cjs.map