@starkeep/storage-sqlite 0.4.0 → 0.5.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
@@ -20,13 +20,20 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ LibraryBoundElsewhereError: () => LibraryBoundElsewhereError,
23
24
  SqliteAppSyncableApplier: () => SqliteAppSyncableApplier,
24
25
  SqliteAppSyncableNamespaceStore: () => SqliteAppSyncableNamespaceStore,
25
26
  SqliteDatabaseAdapter: () => SqliteDatabaseAdapter,
26
27
  appSyncableTableName: () => appSyncableTableName,
28
+ applyConnectionPragmas: () => applyConnectionPragmas,
29
+ boundUserId: () => boundUserId,
30
+ checkLibraryBindable: () => checkLibraryBindable,
31
+ commitLibraryBinding: () => commitLibraryBinding,
27
32
  deleteAppSyncableNamespace: () => deleteAppSyncableNamespace,
28
33
  getAppSyncableNamespace: () => getAppSyncableNamespace,
29
34
  initializeLocalSchema: () => initializeLocalSchema,
35
+ libraryKeyColumns: () => libraryKeyColumns,
36
+ libraryUserSegment: () => libraryUserSegment,
30
37
  listAppSyncableNamespaces: () => listAppSyncableNamespaces,
31
38
  sqliteCompiler: () => compiler,
32
39
  upsertAppSyncableNamespace: () => upsertAppSyncableNamespace
@@ -34,8 +41,9 @@ __export(index_exports, {
34
41
  module.exports = __toCommonJS(index_exports);
35
42
 
36
43
  // src/adapter.ts
37
- var import_protocol_primitives3 = require("@starkeep/protocol-primitives");
38
- var import_storage_adapter2 = require("@starkeep/storage-adapter");
44
+ var import_storage_adapter3 = require("@starkeep/storage-adapter");
45
+ var import_protocol_primitives4 = require("@starkeep/protocol-primitives");
46
+ var import_storage_adapter4 = require("@starkeep/storage-adapter");
39
47
  var import_kysely3 = require("kysely");
40
48
 
41
49
  // src/serialization.ts
@@ -56,7 +64,12 @@ function recordToRow(record) {
56
64
  size_bytes: record.sizeBytes,
57
65
  original_filename: record.originalFilename,
58
66
  origin_app_id: record.originAppId,
59
- parent_id: record.parentId
67
+ parent_id: record.parentId,
68
+ stand_in_role: record.standInRole,
69
+ fidelity: record.fidelity,
70
+ canonical_threshold: record.canonicalThreshold,
71
+ self_canonical: record.selfCanonical ? 1 : 0,
72
+ stand_in_slot: (0, import_protocol_primitives.standInSlot)(record)
60
73
  };
61
74
  }
62
75
  function rowToRecord(row) {
@@ -74,30 +87,20 @@ function rowToRecord(row) {
74
87
  sizeBytes: row.size_bytes,
75
88
  originalFilename: row.original_filename,
76
89
  originAppId: row.origin_app_id,
77
- parentId: row.parent_id ? (0, import_protocol_primitives.createStarkeepId)(row.parent_id) : null
90
+ parentId: row.parent_id ? (0, import_protocol_primitives.createStarkeepId)(row.parent_id) : null,
91
+ standInRole: (0, import_protocol_primitives.isStandInRole)(row.stand_in_role) ? row.stand_in_role : null,
92
+ // DSQL returns a bigint column as a string; the value is small, so a
93
+ // Number is exact.
94
+ fidelity: row.fidelity === null || row.fidelity === void 0 ? null : Number(row.fidelity),
95
+ canonicalThreshold: row.canonical_threshold === null || row.canonical_threshold === void 0 ? null : Number(row.canonical_threshold),
96
+ selfCanonical: row.self_canonical === 1
78
97
  };
79
98
  }
80
99
 
81
100
  // src/query-builder.ts
101
+ var import_storage_adapter2 = require("@starkeep/storage-adapter");
102
+ var import_protocol_primitives2 = require("@starkeep/protocol-primitives");
82
103
  var import_kysely = require("kysely");
83
- var FIELD_MAP = {
84
- id: "id",
85
- type: "type",
86
- createdAt: "created_at",
87
- updatedAt: "updated_at",
88
- deletedAt: "deleted_at",
89
- version: "version",
90
- contentHash: "content_hash",
91
- objectStorageKey: "object_storage_key",
92
- mimeType: "mime_type",
93
- sizeBytes: "size_bytes",
94
- originAppId: "origin_app_id",
95
- parentId: "parent_id",
96
- originalFilename: "original_filename"
97
- };
98
- function mapField(field) {
99
- return FIELD_MAP[field] ?? field;
100
- }
101
104
  var compiler = new import_kysely.Kysely({
102
105
  dialect: {
103
106
  createAdapter: () => new import_kysely.SqliteAdapter(),
@@ -106,78 +109,40 @@ var compiler = new import_kysely.Kysely({
106
109
  createQueryCompiler: () => new import_kysely.SqliteQueryCompiler()
107
110
  }
108
111
  });
112
+ var DIALECT = {
113
+ records: "shared_records",
114
+ labels: "shared_record_labels",
115
+ metadataTable: import_protocol_primitives2.sqliteMetadataTableName
116
+ };
109
117
  function buildSelectQuery(query) {
110
- let qb = compiler.selectFrom("shared_records").selectAll();
111
- if (query.type) {
112
- qb = qb.where("type", "=", query.type);
113
- }
114
- if (query.filters) {
115
- for (const filter of query.filters) {
116
- qb = applyFilter(qb, filter);
117
- }
118
- }
119
- if (query.excludeLabel) {
120
- const { appId, key } = query.excludeLabel;
121
- qb = qb.where(
122
- (eb) => eb.not(
123
- eb.exists(
124
- eb.selectFrom("shared_record_labels").select("record_id").whereRef("shared_record_labels.record_id", "=", "shared_records.id").where("app_id", "=", appId).where("key", "=", key).where("deleted_at", "is", null)
125
- )
126
- )
127
- );
128
- }
129
- if (query.cursor) {
130
- qb = qb.where("id", ">", query.cursor);
131
- }
132
- if (query.sort && query.sort.length > 0) {
133
- for (const s of query.sort) {
134
- qb = qb.orderBy(mapField(s.field), s.direction === "desc" ? "desc" : "asc");
135
- }
136
- } else {
137
- qb = qb.orderBy("id", "asc");
138
- }
139
- if (query.limit) {
140
- qb = qb.limit(query.limit + 1);
141
- }
142
- const compiled = qb.compile();
118
+ const compiled = (0, import_storage_adapter2.buildRecordSelect)(compiler, DIALECT, query);
143
119
  return { sql: compiled.sql, params: [...compiled.parameters] };
144
120
  }
145
- function applyFilter(qb, filter) {
146
- const col = mapField(filter.field);
147
- switch (filter.operator) {
148
- case "eq":
149
- return qb.where(col, "=", filter.value);
150
- case "neq":
151
- return qb.where(col, "!=", filter.value);
152
- case "gt":
153
- return qb.where(col, ">", filter.value);
154
- case "gte":
155
- return qb.where(col, ">=", filter.value);
156
- case "lt":
157
- return qb.where(col, "<", filter.value);
158
- case "lte":
159
- return qb.where(col, "<=", filter.value);
160
- case "in":
161
- return qb.where(col, "in", filter.value);
162
- case "like":
163
- return qb.where(col, "like", `%${filter.value}%`);
164
- case "isNull":
165
- return qb.where((eb) => eb(col, "is", null));
166
- case "isNotNull":
167
- return qb.where((eb) => eb(col, "is not", null));
168
- default:
169
- return qb;
170
- }
121
+ function buildCountQuery(query) {
122
+ const compiled = (0, import_storage_adapter2.buildRecordCount)(compiler, DIALECT, query);
123
+ return { sql: compiled.sql, params: [...compiled.parameters] };
124
+ }
125
+ function buildTypeCountsQuery(query) {
126
+ const compiled = (0, import_storage_adapter2.buildRecordTypeCounts)(compiler, DIALECT, query);
127
+ return { sql: compiled.sql, params: [...compiled.parameters] };
171
128
  }
172
129
 
173
130
  // src/schema/bootstrap.ts
174
131
  var import_kysely2 = require("kysely");
175
- var import_protocol_primitives2 = require("@starkeep/protocol-primitives");
132
+ var import_protocol_primitives3 = require("@starkeep/protocol-primitives");
133
+ function applyConnectionPragmas(db) {
134
+ db.exec("PRAGMA case_sensitive_like = ON");
135
+ }
176
136
  function applyLocalSchemaDdl(db) {
177
137
  db.exec("PRAGMA journal_mode = WAL");
178
138
  db.exec("PRAGMA foreign_keys = ON");
139
+ applyConnectionPragmas(db);
179
140
  db.exec(
180
- 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
141
+ 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").addColumn(
142
+ "stand_in_role",
143
+ "text",
144
+ (c) => c.check(import_kysely2.sql`stand_in_role IN ('canonical', 'smaller')`)
145
+ ).addColumn("fidelity", "integer").addColumn("canonical_threshold", "integer").addColumn("self_canonical", "integer").addColumn("stand_in_slot", "text").compile().sql
181
146
  );
182
147
  const sharedRecordsIndexes = [
183
148
  compiler.schema.createIndex("idx_shared_records_type").ifNotExists().on("shared_records").column("type"),
@@ -192,16 +157,17 @@ function applyLocalSchemaDdl(db) {
192
157
  // table once per write. Leading with content_hash is what makes the dedup
193
158
  // check affordable enough to be unconditional, and unconditional is what
194
159
  // makes "one object key, one record" an invariant rather than a hope.
195
- compiler.schema.createIndex("idx_shared_records_content_hash").ifNotExists().on("shared_records").columns(["content_hash", "parent_id", "deleted_at"]),
196
- // Duplicate-file prevention: (filename + bytes) is unique among live
197
- // records. Tombstoned rows (deleted_at IS NOT NULL) are excluded so a
198
- // re-upload after delete is allowed. Records with NULL filename are not
199
- // constrained — the rule requires both filename and content to match.
200
- 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)
160
+ compiler.schema.createIndex("idx_shared_records_content_hash").ifNotExists().on("shared_records").columns(["content_hash", "parent_id", "deleted_at"])
201
161
  ];
202
162
  for (const index of sharedRecordsIndexes) {
203
163
  db.exec(index.compile().sql);
204
164
  }
165
+ db.exec(
166
+ compiler.schema.createIndex("uq_shared_records_stand_in_slot").ifNotExists().unique().on("shared_records").columns(["parent_id", "stand_in_slot"]).compile().sql
167
+ );
168
+ db.exec(
169
+ compiler.schema.createIndex("uq_shared_records_parent_filename_hash").ifNotExists().unique().on("shared_records").expression(import_kysely2.sql`COALESCE(parent_id, ''), COALESCE(original_filename, ''), content_hash`).where(import_kysely2.sql.ref("deleted_at"), "is", null).compile().sql
170
+ );
205
171
  db.exec(
206
172
  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
207
173
  );
@@ -256,9 +222,10 @@ function applyLocalSchemaDdl(db) {
256
222
  db.exec(
257
223
  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
258
224
  );
259
- for (const c of import_protocol_primitives2.CATEGORIES) {
260
- if (c.id === "other") continue;
261
- db.exec((0, import_protocol_primitives2.sqliteMetadataDdl)(c));
225
+ for (const c of import_protocol_primitives3.CATEGORIES) {
226
+ if (!(0, import_protocol_primitives3.hasMetadataTable)(c.id)) continue;
227
+ db.exec((0, import_protocol_primitives3.sqliteMetadataDdl)(c));
228
+ for (const index of (0, import_protocol_primitives3.metadataIndexDdls)(c, "sqlite")) db.exec(index.sql);
262
229
  }
263
230
  }
264
231
  function initializeLocalSchema(db) {
@@ -272,6 +239,28 @@ function initializeLocalSchema(db) {
272
239
  }
273
240
  }
274
241
 
242
+ // src/row-values.ts
243
+ function booleanColumnNames(columns) {
244
+ if (!columns) return null;
245
+ const names = columns.filter((c) => c.type === "boolean").map((c) => c.name);
246
+ return names.length > 0 ? new Set(names) : null;
247
+ }
248
+ function fromSqliteRows(rows, booleanColumns) {
249
+ if (!booleanColumns || booleanColumns.size === 0) return rows;
250
+ return rows.map((row) => {
251
+ const out = { ...row };
252
+ for (const name of booleanColumns) {
253
+ const value = out[name];
254
+ if (typeof value === "number") out[name] = value !== 0;
255
+ }
256
+ return out;
257
+ });
258
+ }
259
+ function toSqliteParam(value) {
260
+ if (typeof value === "boolean") return value ? 1 : 0;
261
+ return value;
262
+ }
263
+
275
264
  // src/adapter.ts
276
265
  var AVAILABILITY = "shared_object_availability";
277
266
  function rowToAvailability(row) {
@@ -288,6 +277,7 @@ function rowToAvailability(row) {
288
277
  var LABELS = {
289
278
  table: "shared_record_labels"
290
279
  };
280
+ var RECORDS = "shared_records";
291
281
  var SqliteDatabaseAdapter = class {
292
282
  database = null;
293
283
  options;
@@ -315,7 +305,7 @@ var SqliteDatabaseAdapter = class {
315
305
  }
316
306
  }
317
307
  getDatabase() {
318
- if (!this.database) throw new import_storage_adapter2.StorageError("Database not initialized. Call init() first.");
308
+ if (!this.database) throw new import_storage_adapter4.StorageError("Database not initialized. Call init() first.");
319
309
  return this.database;
320
310
  }
321
311
  /**
@@ -336,14 +326,22 @@ var SqliteDatabaseAdapter = class {
336
326
  // borrow node:sqlite's parameter types. `RawStatement` declares `unknown[]`
337
327
  // directly, so they were already no-ops — and they were the last thing in
338
328
  // this file referring to a concrete driver.
339
- runStmt(sql3, ...params) {
340
- this.getDatabase().prepare(sql3).run(...params);
329
+ //
330
+ // All three map their parameters through `toSqliteParam`, which is what makes
331
+ // `row-values.ts`'s claim true — every bind on this connection, reads and
332
+ // writes alike. It was true only of the query path until 2026-09-10, so a
333
+ // declared `boolean` written through `putMetadata` reached the driver as a
334
+ // JavaScript boolean and threw "Provided value cannot be bound to SQLite
335
+ // parameter". Nothing had exercised it because no metadata column was a
336
+ // boolean until `exif_present`.
337
+ runStmt(sql4, ...params) {
338
+ this.getDatabase().prepare(sql4).run(...params.map(toSqliteParam));
341
339
  }
342
- getRow(sql3, ...params) {
343
- return this.getDatabase().prepare(sql3).get(...params);
340
+ getRow(sql4, ...params) {
341
+ return this.getDatabase().prepare(sql4).get(...params.map(toSqliteParam));
344
342
  }
345
- allRows(sql3, ...params) {
346
- return this.getDatabase().prepare(sql3).all(...params);
343
+ allRows(sql4, ...params) {
344
+ return this.getDatabase().prepare(sql4).all(...params.map(toSqliteParam));
347
345
  }
348
346
  async put(record) {
349
347
  const row = recordToRow(record);
@@ -363,8 +361,8 @@ var SqliteDatabaseAdapter = class {
363
361
  return row ? rowToRecord(row) : null;
364
362
  }
365
363
  async delete(id, hlc) {
366
- const ts = (0, import_protocol_primitives3.serializeHLC)(hlc);
367
- const query = compiler.updateTable("shared_records").set({ deleted_at: ts, updated_at: ts, node_id: hlc.nodeId }).where("id", "=", id).compile();
364
+ const ts = (0, import_protocol_primitives4.serializeHLC)(hlc);
365
+ const query = compiler.updateTable("shared_records").set({ deleted_at: ts, updated_at: ts, node_id: hlc.nodeId, stand_in_slot: null }).where("id", "=", id).compile();
368
366
  this.runStmt(query.sql, ...query.parameters);
369
367
  }
370
368
  async getNodeWatermarks() {
@@ -372,22 +370,104 @@ var SqliteDatabaseAdapter = class {
372
370
  const rows = this.allRows(watermarksQuery.sql);
373
371
  const out = {};
374
372
  for (const row of rows) {
375
- out[row.node_id] = (0, import_protocol_primitives3.deserializeHLC)(row.max_updated_at);
373
+ out[row.node_id] = (0, import_protocol_primitives4.deserializeHLC)(row.max_updated_at);
376
374
  }
377
375
  return out;
378
376
  }
377
+ async querySince(peerWatermarks, limit) {
378
+ const scans = (0, import_storage_adapter4.planNodeScans)(await this.getNodeWatermarks(), peerWatermarks);
379
+ const { rows, hasMore, truncated } = await (0, import_storage_adapter4.collectSince)(
380
+ scans,
381
+ limit,
382
+ async (scan, remaining) => {
383
+ const compiled = (0, import_storage_adapter4.buildScanSinceForNode)(compiler, RECORDS, scan, remaining);
384
+ return this.allRows(compiled.sql, ...compiled.parameters);
385
+ },
386
+ (row) => (0, import_protocol_primitives4.deserializeHLC)(row["updated_at"])
387
+ );
388
+ return { rows: rows.map(rowToRecord), hasMore, truncated };
389
+ }
390
+ async queryLabelsSince(peerWatermarks, limit) {
391
+ const scans = (0, import_storage_adapter4.planNodeScans)(await this.getLabelNodeWatermarks(), peerWatermarks);
392
+ const { rows, hasMore, truncated } = await (0, import_storage_adapter4.collectSince)(
393
+ scans,
394
+ limit,
395
+ async (scan, remaining) => {
396
+ const compiled = (0, import_storage_adapter4.buildScanSinceForNode)(compiler, LABELS.table, scan, remaining);
397
+ return this.allRows(compiled.sql, ...compiled.parameters);
398
+ },
399
+ (row) => (0, import_protocol_primitives4.deserializeHLC)(row["updated_at"])
400
+ );
401
+ return { rows: rows.map(import_storage_adapter4.rowToLabel), hasMore, truncated };
402
+ }
403
+ async bucketDigest(prefixLength = import_storage_adapter4.DEFAULT_BUCKET_PREFIX_LENGTH) {
404
+ const out = [];
405
+ for (const table of [RECORDS, LABELS.table]) {
406
+ const compiled = (0, import_storage_adapter4.buildBucketDigest)(compiler, table, prefixLength);
407
+ out.push(
408
+ ...(0, import_storage_adapter4.toDigestBuckets)(
409
+ this.allRows(compiled.sql, ...compiled.parameters)
410
+ )
411
+ );
412
+ }
413
+ return (0, import_storage_adapter4.mergeDigestBuckets)(out);
414
+ }
379
415
  async query(query) {
380
- const { sql: sql3, params } = buildSelectQuery(query);
381
- const rows = this.allRows(sql3, ...params);
416
+ const { sql: sql4, params } = buildSelectQuery(query);
417
+ const rows = this.allRows(sql4, ...params);
382
418
  const limit = query.limit;
383
419
  const hasMore = limit ? rows.length > limit : false;
384
420
  const resultRows = hasMore ? rows.slice(0, limit) : rows;
385
421
  return {
386
422
  records: resultRows.map(rowToRecord),
387
- nextCursor: hasMore ? resultRows[resultRows.length - 1].id : null,
423
+ // Cut from the row itself, ordering values and all — see `nextCursorFrom`.
424
+ // The page carries those values back under reserved aliases precisely so
425
+ // this does not have to recompute what the database ordered on.
426
+ nextCursor: hasMore ? (0, import_storage_adapter3.nextCursorFrom)(query, resultRows[resultRows.length - 1]) : null,
388
427
  hasMore
389
428
  };
390
429
  }
430
+ async countRecords(query) {
431
+ const { sql: sql4, params } = buildCountQuery(query);
432
+ const row = this.getRow(sql4, ...params);
433
+ return row?.total ?? 0;
434
+ }
435
+ async countRecordsByType(query) {
436
+ const { sql: sql4, params } = buildTypeCountsQuery(query);
437
+ const rows = this.allRows(sql4, ...params);
438
+ return rows.map((row) => ({
439
+ type: row.type,
440
+ count: Number(row.count),
441
+ latestUpdatedAt: row.latest_updated_at
442
+ }));
443
+ }
444
+ /**
445
+ * See `DatabaseAdapter.queryShared`. Compiled by the same builder the
446
+ * app-syncable plane uses, against the table the target names on this engine.
447
+ */
448
+ async queryShared(target, query, options = {}) {
449
+ const schema = (0, import_storage_adapter4.sharedQuerySchema)(target);
450
+ const table = (0, import_storage_adapter4.sharedQueryTableName)(target, "sqlite");
451
+ const build = {
452
+ serverWhere: options.serverWhere,
453
+ excludeSoftDeleted: (0, import_storage_adapter4.sharedQueryExcludesSoftDeleted)(target),
454
+ ...options.softDeleted ? { softDeleted: options.softDeleted } : {}
455
+ };
456
+ const booleans = booleanColumnNames(schema.columns);
457
+ if (query.mode === "aggregate") {
458
+ const compiled2 = (0, import_storage_adapter4.buildAppAggregateQuery)(compiler, table, query, import_storage_adapter4.SQLITE_APP_QUERY_DIALECT, build);
459
+ return (0, import_storage_adapter4.collectAggregatePage)(query, fromSqliteRows(this.selectCompiled(compiled2), booleans));
460
+ }
461
+ const compiled = (0, import_storage_adapter4.buildAppRowQuery)(compiler, table, query, build);
462
+ return (0, import_storage_adapter4.collectRowPage)(query, fromSqliteRows(this.selectCompiled(compiled), booleans));
463
+ }
464
+ /** Bind and run a compiled SELECT, adapting values SQLite cannot bind. */
465
+ selectCompiled(compiled) {
466
+ return this.allRows(
467
+ compiled.sql,
468
+ ...compiled.parameters.map(toSqliteParam)
469
+ );
470
+ }
391
471
  async batch(operations) {
392
472
  this.getDatabase().exec("BEGIN");
393
473
  try {
@@ -419,16 +499,12 @@ var SqliteDatabaseAdapter = class {
419
499
  } catch (error) {
420
500
  this.getDatabase().exec("ROLLBACK TO SAVEPOINT starkeep_tx");
421
501
  this.getDatabase().exec("RELEASE SAVEPOINT starkeep_tx");
422
- throw new import_storage_adapter2.TransactionError("Transaction failed", error);
502
+ throw new import_storage_adapter4.TransactionError("Transaction failed", error);
423
503
  }
424
504
  }
425
- async putMetadata(typeId, row) {
426
- const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
427
- const values = { record_id: row.recordId };
428
- for (const [key, value] of Object.entries(row)) {
429
- if (key === "recordId") continue;
430
- values[key] = value;
431
- }
505
+ async putMetadata(recordType, row) {
506
+ const table = (0, import_protocol_primitives4.sqliteMetadataTableName)(recordType);
507
+ const values = metadataValues(recordType, row);
432
508
  const updateColumns = Object.keys(values).filter((c) => c !== "record_id");
433
509
  const query = compiler.insertInto(table).values(values).onConflict(
434
510
  (oc) => updateColumns.length > 0 ? oc.column("record_id").doUpdateSet(
@@ -438,29 +514,40 @@ var SqliteDatabaseAdapter = class {
438
514
  this.runStmt(query.sql, ...query.parameters);
439
515
  }
440
516
  async getMetadata(typeId, recordId) {
441
- const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
517
+ const table = (0, import_protocol_primitives4.sqliteMetadataTableName)(typeId);
442
518
  const query = compiler.selectFrom(table).selectAll().where("record_id", "=", recordId).compile();
443
519
  const row = this.getRow(query.sql, ...query.parameters);
444
520
  if (!row) return null;
445
- return columnsToMetadataRow(recordId, row);
521
+ return columnsToMetadataRow(recordId, typeId, row);
446
522
  }
447
523
  async getMetadataByIds(typeId, recordIds) {
448
524
  const result = /* @__PURE__ */ new Map();
449
525
  if (recordIds.length === 0) return result;
450
- const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
526
+ const table = (0, import_protocol_primitives4.sqliteMetadataTableName)(typeId);
451
527
  const query = compiler.selectFrom(table).selectAll().where("record_id", "in", recordIds).compile();
452
528
  const rows = this.allRows(query.sql, ...query.parameters);
453
529
  for (const row of rows) {
454
530
  const recordId = row["record_id"];
455
- result.set(recordId, columnsToMetadataRow(recordId, row));
531
+ result.set(recordId, columnsToMetadataRow(recordId, typeId, row));
456
532
  }
457
533
  return result;
458
534
  }
459
535
  async deleteMetadata(typeId, recordId) {
460
- const table = (0, import_protocol_primitives3.sqliteMetadataTableName)(typeId);
536
+ const table = (0, import_protocol_primitives4.sqliteMetadataTableName)(typeId);
461
537
  const query = compiler.deleteFrom(table).where("record_id", "=", recordId).compile();
462
538
  this.runStmt(query.sql, ...query.parameters);
463
539
  }
540
+ async tombstoneMetadata(typeId, recordId, hlc) {
541
+ this.setMetadataTombstone(typeId, recordId, (0, import_protocol_primitives4.serializeHLC)(hlc));
542
+ }
543
+ async restoreMetadata(typeId, recordId) {
544
+ this.setMetadataTombstone(typeId, recordId, null);
545
+ }
546
+ setMetadataTombstone(typeId, recordId, value) {
547
+ const table = (0, import_protocol_primitives4.sqliteMetadataTableName)(typeId);
548
+ const query = compiler.updateTable(table).set({ [import_protocol_primitives4.METADATA_DELETED_AT_COLUMN]: value }).where("record_id", "=", recordId).compile();
549
+ this.runStmt(query.sql, ...query.parameters);
550
+ }
464
551
  // ---- Cross-app record labels -------------------------------------------
465
552
  //
466
553
  // The SQL for all of these is built in `@starkeep/storage-adapter`, shared
@@ -469,12 +556,12 @@ var SqliteDatabaseAdapter = class {
469
556
  // and it lives in LABELS above.
470
557
  async upsertLabels(labels) {
471
558
  if (labels.length === 0) return;
472
- const query = (0, import_storage_adapter2.buildLabelUpsert)(compiler, LABELS, labels);
559
+ const query = (0, import_storage_adapter4.buildLabelUpsert)(compiler, LABELS, labels);
473
560
  this.runStmt(query.sql, ...query.parameters);
474
561
  }
475
562
  async retractLabels(retractions) {
476
563
  for (const r of retractions) {
477
- const query = (0, import_storage_adapter2.buildLabelRetraction)(compiler, LABELS, r);
564
+ const query = (0, import_storage_adapter4.buildLabelRetraction)(compiler, LABELS, r);
478
565
  this.runStmt(query.sql, ...query.parameters);
479
566
  }
480
567
  }
@@ -483,10 +570,10 @@ var SqliteDatabaseAdapter = class {
483
570
  this.getDatabase().exec("BEGIN");
484
571
  try {
485
572
  for (const r of replacements) {
486
- const tombstone = (0, import_storage_adapter2.buildLabelValueReplacementTombstone)(compiler, LABELS, r);
573
+ const tombstone = (0, import_storage_adapter4.buildLabelValueReplacementTombstone)(compiler, LABELS, r);
487
574
  this.runStmt(tombstone.sql, ...tombstone.parameters);
488
575
  if (r.values.length > 0) {
489
- const upsert = (0, import_storage_adapter2.buildLabelUpsert)(
576
+ const upsert = (0, import_storage_adapter4.buildLabelUpsert)(
490
577
  compiler,
491
578
  LABELS,
492
579
  r.values.map((value) => ({
@@ -509,37 +596,44 @@ var SqliteDatabaseAdapter = class {
509
596
  }
510
597
  async getLabelsByRecordIds(recordIds) {
511
598
  if (recordIds.length === 0) return /* @__PURE__ */ new Map();
512
- const query = (0, import_storage_adapter2.buildLabelsByRecordIds)(compiler, LABELS, recordIds);
513
- return (0, import_storage_adapter2.groupLabelsByRecordId)(
514
- this.allRows(query.sql, ...query.parameters)
515
- );
599
+ const query = (0, import_storage_adapter4.buildLabelsByRecordIds)(compiler, LABELS, recordIds);
600
+ return (0, import_storage_adapter4.groupLabelsByRecordId)(this.allRows(query.sql, ...query.parameters));
516
601
  }
602
+ /**
603
+ * The reverse label read, run through the query grammar — see
604
+ * `label-find.ts`. The grant rides in as `serverWhere` rather than as one of
605
+ * the caller's own predicates, which is the separation every other shared
606
+ * query draws.
607
+ */
517
608
  async findByLabel(query) {
518
- const compiled = (0, import_storage_adapter2.buildFindByLabel)(compiler, LABELS, query);
519
- if (!compiled) return { labels: [], nextCursor: null, hasMore: false };
520
- const rows = this.allRows(compiled.sql, ...compiled.parameters);
521
- return (0, import_storage_adapter2.paginateFindByLabel)(rows, query.limit);
609
+ const plan = (0, import_storage_adapter4.planFindByLabel)(query);
610
+ if (!plan) return (0, import_storage_adapter4.emptyLabelPage)();
611
+ return (0, import_storage_adapter4.labelPageFrom)(
612
+ await this.queryShared(import_storage_adapter4.LABEL_QUERY_TARGET, plan.query, {
613
+ serverWhere: plan.serverWhere
614
+ })
615
+ );
522
616
  }
523
617
  // ---- Label sync ---------------------------------------------------------
524
618
  async putLabel(label) {
525
- const query = (0, import_storage_adapter2.buildLabelSnapshotUpsert)(compiler, LABELS, label);
619
+ const query = (0, import_storage_adapter4.buildLabelSnapshotUpsert)(compiler, LABELS, label);
526
620
  this.runStmt(query.sql, ...query.parameters);
527
621
  }
528
622
  async getLabel(recordId, appId, key, value) {
529
- const query = (0, import_storage_adapter2.buildGetLabel)(compiler, LABELS, recordId, appId, key, value);
623
+ const query = (0, import_storage_adapter4.buildGetLabel)(compiler, LABELS, recordId, appId, key, value);
530
624
  const row = this.getRow(query.sql, ...query.parameters);
531
- return row ? (0, import_storage_adapter2.rowToLabel)(row) : null;
625
+ return row ? (0, import_storage_adapter4.rowToLabel)(row) : null;
532
626
  }
533
627
  async queryLabels(query) {
534
- const compiled = (0, import_storage_adapter2.buildQueryLabels)(compiler, LABELS, query);
628
+ const compiled = (0, import_storage_adapter4.buildQueryLabels)(compiler, LABELS, query);
535
629
  const rows = this.allRows(compiled.sql, ...compiled.parameters);
536
- return (0, import_storage_adapter2.paginateLabelScan)(rows, query.limit);
630
+ return (0, import_storage_adapter4.paginateLabelScan)(rows, query.limit);
537
631
  }
538
632
  async getLabelNodeWatermarks() {
539
- const query = (0, import_storage_adapter2.buildLabelNodeWatermarks)(compiler, LABELS);
633
+ const query = (0, import_storage_adapter4.buildLabelNodeWatermarks)(compiler, LABELS);
540
634
  const rows = this.allRows(query.sql);
541
635
  const out = {};
542
- for (const row of rows) out[row.node_id] = (0, import_protocol_primitives3.deserializeHLC)(row.max_updated_at);
636
+ for (const row of rows) out[row.node_id] = (0, import_protocol_primitives4.deserializeHLC)(row.max_updated_at);
543
637
  return out;
544
638
  }
545
639
  // ---- Object availability ------------------------------------------------
@@ -573,7 +667,11 @@ var SqliteDatabaseAdapter = class {
573
667
  this.runStmt(compiled.sql, ...compiled.parameters);
574
668
  }
575
669
  async countRestoringObjects() {
576
- const compiled = compiler.selectFrom(AVAILABILITY).innerJoin("shared_records", "shared_records.object_storage_key", `${AVAILABILITY}.object_storage_key`).select(({ fn }) => [
670
+ const compiled = compiler.selectFrom(AVAILABILITY).innerJoin(
671
+ "shared_records",
672
+ "shared_records.object_storage_key",
673
+ `${AVAILABILITY}.object_storage_key`
674
+ ).select(({ fn }) => [
577
675
  fn.count(`${AVAILABILITY}.object_storage_key`).as("object_count"),
578
676
  fn.sum("shared_records.size_bytes").as("bytes")
579
677
  ]).where(`${AVAILABILITY}.state`, "=", import_kysely3.sql.lit("restoring")).compile();
@@ -584,20 +682,50 @@ var SqliteDatabaseAdapter = class {
584
682
  return { objectCount: row?.object_count ?? 0, bytes: row?.bytes ?? 0 };
585
683
  }
586
684
  async tombstoneLabelsForRecord(recordId, hlc) {
587
- const query = (0, import_storage_adapter2.buildTombstoneLabelsForRecord)(compiler, LABELS, recordId, hlc);
685
+ const query = (0, import_storage_adapter4.buildTombstoneLabelsForRecord)(compiler, LABELS, recordId, hlc);
686
+ this.runStmt(query.sql, ...query.parameters);
687
+ }
688
+ async restoreLabelsForRecord(recordId, deletedAt, hlc) {
689
+ const query = (0, import_storage_adapter4.buildRestoreLabelsForRecord)(compiler, LABELS, recordId, deletedAt, hlc);
588
690
  this.runStmt(query.sql, ...query.parameters);
589
691
  }
590
692
  };
591
- function columnsToMetadataRow(recordId, columns) {
693
+ function columnsToMetadataRow(recordId, typeId, columns) {
694
+ const category = (0, import_protocol_primitives4.typeCategory)(typeId) ?? typeId;
695
+ const booleans = booleanColumnNames((0, import_protocol_primitives4.getCategory)(category)?.metadataColumns);
592
696
  const row = { recordId };
593
697
  for (const [key, value] of Object.entries(columns)) {
594
- if (key === "record_id") continue;
595
- row[key] = value;
698
+ if (key === "record_id" || key === import_protocol_primitives4.METADATA_DELETED_AT_COLUMN) continue;
699
+ row[key] = booleans?.has(key) && typeof value === "number" ? value !== 0 : value;
596
700
  }
597
701
  return row;
598
702
  }
703
+ function metadataValues(recordType, row) {
704
+ if (!(0, import_protocol_primitives4.isKnownType)(recordType)) {
705
+ throw new import_storage_adapter4.StorageError(
706
+ `putMetadata needs the record's own type, not "${recordType}": the ${import_protocol_primitives4.METADATA_DISCRIMINANT_COLUMN} column gates every read of this row`
707
+ );
708
+ }
709
+ const values = {
710
+ record_id: row.recordId,
711
+ [import_protocol_primitives4.METADATA_DISCRIMINANT_COLUMN]: recordType,
712
+ // Writing metadata asserts the row is live. Without this an app re-reporting
713
+ // a restored record's dimensions would write into a row still carrying its
714
+ // tombstone, and the one read that filters on the stamp would keep answering
715
+ // nothing. Server-owned either way: a wire row carrying it is ignored below.
716
+ [import_protocol_primitives4.METADATA_DELETED_AT_COLUMN]: null
717
+ };
718
+ for (const [key, value] of Object.entries(row)) {
719
+ if (key === "recordId" || key === import_protocol_primitives4.METADATA_DISCRIMINANT_COLUMN || key === import_protocol_primitives4.METADATA_DELETED_AT_COLUMN) {
720
+ continue;
721
+ }
722
+ values[key] = value;
723
+ }
724
+ return values;
725
+ }
599
726
 
600
727
  // src/app-syncable/namespace.ts
728
+ var import_shared_space_api = require("@starkeep/shared-space-api");
601
729
  function normalizeAppId(appId) {
602
730
  return appId.toLowerCase().replace(/-/g, "_");
603
731
  }
@@ -605,7 +733,7 @@ function appSyncableTableName(appId, tableName) {
605
733
  return `${normalizeAppId(appId)}_syncable_${tableName}`;
606
734
  }
607
735
  function rowToNamespace(r) {
608
- const tables = JSON.parse(r.tables_json);
736
+ const tables = (0, import_shared_space_api.parseAppSyncableTables)(r.app_id, r.tables_json);
609
737
  return {
610
738
  appId: r.app_id,
611
739
  tables,
@@ -656,12 +784,13 @@ var SqliteAppSyncableNamespaceStore = class {
656
784
 
657
785
  // src/app-syncable/apply.ts
658
786
  var import_kysely4 = require("kysely");
659
- var import_protocol_primitives4 = require("@starkeep/protocol-primitives");
787
+ var import_protocol_primitives5 = require("@starkeep/protocol-primitives");
788
+ var import_storage_adapter5 = require("@starkeep/storage-adapter");
660
789
  function runCompiled(db, compiled) {
661
- db.prepare(compiled.sql).run(...compiled.parameters);
790
+ db.prepare(compiled.sql).run(...compiled.parameters.map(toSqliteParam));
662
791
  }
663
792
  function allCompiled(db, compiled) {
664
- return db.prepare(compiled.sql).all(...compiled.parameters);
793
+ return db.prepare(compiled.sql).all(...compiled.parameters.map(toSqliteParam));
665
794
  }
666
795
  var SqliteAppSyncableApplier = class {
667
796
  constructor(db, namespace) {
@@ -673,9 +802,7 @@ var SqliteAppSyncableApplier = class {
673
802
  apply(entry) {
674
803
  const ns = this.namespace.get(entry.appId);
675
804
  if (!ns) {
676
- throw new Error(
677
- `SqliteAppSyncableApplier: app "${entry.appId}" not installed`
678
- );
805
+ throw new Error(`SqliteAppSyncableApplier: app "${entry.appId}" not installed`);
679
806
  }
680
807
  const tableInfo = ns.tables.find((t) => t.name === entry.table);
681
808
  if (!tableInfo) {
@@ -688,9 +815,9 @@ var SqliteAppSyncableApplier = class {
688
815
  if (entry.op === "insert") {
689
816
  this.applyInsert(fullName, pkColumns, entry);
690
817
  } else if (entry.op === "update") {
691
- this.applyUpdate(fullName, entry);
818
+ this.applyUpdate(fullName, pkColumns, entry);
692
819
  } else {
693
- this.applyDelete(fullName, entry);
820
+ this.applyDelete(fullName, pkColumns, entry);
694
821
  }
695
822
  }
696
823
  applyInsert(fullName, pkColumns, entry) {
@@ -714,10 +841,10 @@ var SqliteAppSyncableApplier = class {
714
841
  ).compile()
715
842
  );
716
843
  }
717
- applyUpdate(fullName, entry) {
844
+ applyUpdate(fullName, pkColumns, entry) {
718
845
  const rawPatch = entry.row ?? {};
719
846
  const patch = rawPatch["updated_at"] ? withNodeId(rawPatch, entry) : rawPatch;
720
- const where = entry.where ?? {};
847
+ const where = (0, import_storage_adapter5.requireKeyedWhere)(entry, "update", pkColumns);
721
848
  const patchCols = Object.keys(patch);
722
849
  const whereCols = Object.keys(where);
723
850
  if (patchCols.length === 0) return;
@@ -727,78 +854,165 @@ var SqliteAppSyncableApplier = class {
727
854
  query = query.where(c, "=", where[c]);
728
855
  }
729
856
  if (incomingUpdatedAt) {
730
- query = query.where("updated_at", "<", incomingUpdatedAt);
857
+ query = query.where(
858
+ (eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", incomingUpdatedAt)])
859
+ );
731
860
  }
732
861
  runCompiled(this.db, query.compile());
733
862
  }
734
- applyDelete(fullName, entry) {
735
- const where = entry.where ?? {};
863
+ applyDelete(fullName, pkColumns, entry) {
864
+ const where = (0, import_storage_adapter5.requireKeyedWhere)(entry, "delete", pkColumns);
736
865
  const whereCols = Object.keys(where);
737
866
  const incomingUpdatedAt = entry.row?.["updated_at"];
738
- const ts = incomingUpdatedAt ?? (0, import_protocol_primitives4.serializeHLC)(entry.timestamp);
867
+ const ts = incomingUpdatedAt ?? (0, import_protocol_primitives5.serializeHLC)(entry.timestamp);
868
+ const carriesFullRow = entry.row !== void 0 && pkColumns.length > 0 && pkColumns.every((column) => entry.row?.[column] !== void 0);
869
+ if (carriesFullRow) {
870
+ const row = withNodeId({ ...entry.row, updated_at: ts, deleted_at: ts }, entry);
871
+ const updateCols = Object.keys(row).filter((c) => !pkColumns.includes(c));
872
+ runCompiled(
873
+ this.db,
874
+ compiler.insertInto(fullName).values({ ...row }).onConflict(
875
+ (oc) => oc.columns(pkColumns).doUpdateSet(
876
+ (eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
877
+ ).where(import_kysely4.sql.ref("excluded.updated_at"), ">", import_kysely4.sql.ref(`${fullName}.updated_at`))
878
+ ).compile()
879
+ );
880
+ return;
881
+ }
739
882
  let query = compiler.updateTable(fullName).set({ deleted_at: ts, updated_at: ts, node_id: nodeIdOf(ts, entry) });
740
883
  for (const c of whereCols) {
741
884
  query = query.where(c, "=", where[c]);
742
885
  }
743
- query = query.where(
744
- (eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)])
745
- );
886
+ query = query.where((eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)]));
746
887
  runCompiled(this.db, query.compile());
747
888
  }
748
- /** See `ScanCapableApplier.getNodeWatermarks`. Missing table → `{}`. */
889
+ /**
890
+ * Whether this table exists in *this* database.
891
+ *
892
+ * Asked before the digest rather than inferred from a failed read, because
893
+ * those are two different answers and only one of them is `[]`. An app that
894
+ * is not installed here genuinely holds nothing; a table that is locked,
895
+ * corrupt or unreadable holds an unknown number of rows, and reporting zero
896
+ * for it is the loudest possible false alarm — every bucket in it reads as a
897
+ * hole, in whichever direction the comparison runs.
898
+ *
899
+ * `sqlite_master` is the cheapest question that separates them, and asking it
900
+ * cannot itself fail for a reason worth swallowing: a database whose schema
901
+ * table will not read is not one whose app tables are merely absent.
902
+ */
903
+ tableExists(fullName) {
904
+ const row = this.db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?`).all(fullName);
905
+ return row.length > 0;
906
+ }
907
+ /**
908
+ * See `ScanCapableApplier.bucketDigest`. Missing table → `[]`; a table that
909
+ * exists and will not read **throws**, so the caller's `supported: false`
910
+ * path can do its job.
911
+ *
912
+ * This used to be `try { … } catch { return [] }`, which is the same mistake
913
+ * `scanSince` made: `[]` is the wire value for "this table holds nothing", not
914
+ * for "I could not count it". It made both engine-side "could not count ⇒
915
+ * supported: false" guards unreachable on every production path, because no
916
+ * real applier ever threw.
917
+ */
918
+ async bucketDigest(appId, table, prefixLength = import_storage_adapter5.DEFAULT_BUCKET_PREFIX_LENGTH) {
919
+ const fullName = appSyncableTableName(appId, table);
920
+ if (!this.tableExists(fullName)) return [];
921
+ const compiled = (0, import_storage_adapter5.buildBucketDigest)(compiler, fullName, prefixLength);
922
+ return (0, import_storage_adapter5.toDigestBuckets)(allCompiled(this.db, compiled));
923
+ }
924
+ /**
925
+ * See `ScanCapableApplier.getNodeWatermarks`. Missing table → `{}`; a table
926
+ * that exists and will not read **throws**.
927
+ *
928
+ * The distinction matters more here than the `{}` return suggests, because
929
+ * `scanSince` plans its authors from this map: an unreadable table that
930
+ * answered `{}` produced no scans, and the scan then reported "nothing owed,
931
+ * every author complete" — the exact silent-loss shape `scanSince`'s own catch
932
+ * was written to prevent, reached one frame earlier.
933
+ */
749
934
  async getNodeWatermarks(appId, table) {
750
935
  const fullName = appSyncableTableName(appId, table);
751
- let rows;
752
- try {
753
- rows = allCompiled(
754
- this.db,
755
- compiler.selectFrom(fullName).select(({ fn }) => ["node_id", fn.max("updated_at").as("max_updated_at")]).groupBy("node_id").compile()
756
- );
757
- } catch {
758
- return {};
759
- }
936
+ if (!this.tableExists(fullName)) return {};
937
+ const rows = allCompiled(
938
+ this.db,
939
+ compiler.selectFrom(fullName).select(({ fn }) => ["node_id", fn.max("updated_at").as("max_updated_at")]).groupBy("node_id").compile()
940
+ );
760
941
  const out = {};
761
942
  for (const row of rows) {
762
- out[row.node_id] = (0, import_protocol_primitives4.deserializeHLC)(row.max_updated_at);
943
+ out[row.node_id] = (0, import_protocol_primitives5.deserializeHLC)(row.max_updated_at);
763
944
  }
764
945
  return out;
765
946
  }
766
947
  /**
767
- * Pull-side synthesis: return rows updated after `sinceHlcStr` (or `cursor`
768
- * if higher) in HLC order, paginated. `updated_at` is a serialized HLC
769
- * whose lexicographic order matches HLC order (fixed-width hex), and each
770
- * row's HLC is unique per node, so it doubles as the cursor — no separate
771
- * tiebreaker column is needed.
948
+ * Pull-side synthesis: rows the peer hasn't seen, per author, seeking the
949
+ * `(node_id, updated_at)` index rather than reading the table and filtering.
950
+ * See `ScanCapableApplier.scanSince` for the contract and
951
+ * `storage-adapter/src/database/since-queries.ts` for why it is a loop.
772
952
  */
773
- async scanSince(appId, table, sinceHlcStr, options) {
953
+ async scanSince(appId, table, peerWatermarks, limit) {
774
954
  const fullName = appSyncableTableName(appId, table);
775
- const floor = options?.cursor !== void 0 && options.cursor > sinceHlcStr ? options.cursor : sinceHlcStr;
776
- const limit = options?.limit;
777
- let rows;
955
+ const scans = (0, import_storage_adapter5.planNodeScans)(await this.getNodeWatermarks(appId, table), peerWatermarks);
956
+ const pkColumns = this.namespace.get(appId)?.tables.find((t) => t.name === table)?.pkColumns ?? [];
778
957
  try {
779
- let query = compiler.selectFrom(fullName).selectAll().where("updated_at", ">", floor).orderBy("updated_at", "asc");
780
- if (limit !== void 0) {
781
- query = query.limit(limit + 1);
782
- }
783
- rows = allCompiled(this.db, query.compile());
784
- } catch {
785
- return { rows: [], nextCursor: null, hasMore: false };
958
+ const { rows, hasMore, truncated } = await (0, import_storage_adapter5.collectSince)(
959
+ scans,
960
+ limit,
961
+ async (scan, remaining) => {
962
+ const compiled = (0, import_storage_adapter5.buildScanSinceForNode)(compiler, fullName, scan, remaining);
963
+ return allCompiled(this.db, compiled);
964
+ },
965
+ (row) => (0, import_protocol_primitives5.deserializeHLC)(row["updated_at"])
966
+ );
967
+ return {
968
+ // Converted before the row becomes a wire entry: what goes on the wire
969
+ // is what the app sees, and a peer's applier binds a boolean it can.
970
+ rows: fromSqliteRows(rows, this.booleanColumnsOf(appId, table)).map((row) => (0, import_storage_adapter5.rowToWireEntry)(appId, table, row, pkColumns, import_protocol_primitives5.deserializeHLC)).filter((entry) => entry !== null),
971
+ hasMore,
972
+ truncated
973
+ };
974
+ } catch (err) {
975
+ console.warn(
976
+ `[app-syncable] scanSince failed for ${appId}.${table}: ${err.message}`
977
+ );
978
+ const truncated = {};
979
+ for (const scan of scans) truncated[scan.nodeId] = null;
980
+ return { rows: [], hasMore: true, truncated };
786
981
  }
787
- const hasMore = limit !== void 0 && rows.length > limit;
788
- const pageRows = hasMore ? rows.slice(0, limit) : rows;
789
- const entries = pageRows.map((row) => rowToEntry(appId, table, row));
790
- const nextCursor = hasMore && pageRows.length > 0 ? pageRows[pageRows.length - 1]["updated_at"] : null;
791
- return { rows: entries, nextCursor, hasMore };
792
- }
793
- /** Support read path from the factory's queryRows. */
794
- queryRows(appId, table, where) {
982
+ }
983
+ /**
984
+ * Run a parsed query. The read half of the app-data plane.
985
+ *
986
+ * Compiled by the shared builder rather than here, so the SQL this emits and
987
+ * the SQL the DSQL applier emits are one piece of code — the two hand-written
988
+ * read grammars this replaces had already drifted on their limit defaults
989
+ * alone.
990
+ */
991
+ async runQuery(appId, table, query, options = {}) {
795
992
  const fullName = appSyncableTableName(appId, table);
796
- const whereCols = where ? Object.keys(where) : [];
797
- let query = compiler.selectFrom(fullName).selectAll().where("deleted_at", "is", null);
798
- for (const c of whereCols) {
799
- query = query.where(c, "=", where[c]);
993
+ const booleanColumns = this.booleanColumnsOf(appId, table);
994
+ if (query.mode === "aggregate") {
995
+ const compiled2 = (0, import_storage_adapter5.buildAppAggregateQuery)(
996
+ compiler,
997
+ fullName,
998
+ query,
999
+ import_storage_adapter5.SQLITE_APP_QUERY_DIALECT,
1000
+ options
1001
+ );
1002
+ return (0, import_storage_adapter5.collectAggregatePage)(query, fromSqliteRows(this.selectRows(compiled2), booleanColumns));
800
1003
  }
801
- return allCompiled(this.db, query.compile());
1004
+ const compiled = (0, import_storage_adapter5.buildAppRowQuery)(compiler, fullName, query, options);
1005
+ return (0, import_storage_adapter5.collectRowPage)(query, fromSqliteRows(this.selectRows(compiled), booleanColumns));
1006
+ }
1007
+ /** The names of a declared table's `boolean` columns, or null when it has none. */
1008
+ booleanColumnsOf(appId, table) {
1009
+ return booleanColumnNames(
1010
+ this.namespace.get(appId)?.tables.find((t) => t.name === table)?.columns
1011
+ );
1012
+ }
1013
+ /** Bind and run a compiled SELECT, adapting values SQLite cannot bind. */
1014
+ selectRows(compiled) {
1015
+ return this.db.prepare(compiled.sql).all(...compiled.parameters.map(toSqliteParam));
802
1016
  }
803
1017
  };
804
1018
  function withNodeId(row, entry) {
@@ -807,30 +1021,145 @@ function withNodeId(row, entry) {
807
1021
  function nodeIdOf(updatedAt, entry) {
808
1022
  if (typeof updatedAt === "string") {
809
1023
  try {
810
- return (0, import_protocol_primitives4.deserializeHLC)(updatedAt).nodeId;
1024
+ return (0, import_protocol_primitives5.deserializeHLC)(updatedAt).nodeId;
811
1025
  } catch {
812
1026
  }
813
1027
  }
814
1028
  return entry.timestamp.nodeId;
815
1029
  }
816
- function rowToEntry(appId, table, row) {
817
- const updatedAtStr = row["updated_at"];
818
- const deletedAtStr = row["deleted_at"];
819
- const timestamp = (0, import_protocol_primitives4.deserializeHLC)(updatedAtStr);
820
- if (deletedAtStr) {
821
- return { timestamp, appId, table, op: "delete", row };
1030
+
1031
+ // src/library-owner.ts
1032
+ var import_kysely5 = require("kysely");
1033
+ var import_protocol_primitives6 = require("@starkeep/protocol-primitives");
1034
+ var OWNER_TABLE = "library_owner";
1035
+ function ensureOwnerTable(db) {
1036
+ db.exec(
1037
+ compiler.schema.createTable(OWNER_TABLE).ifNotExists().addColumn("id", "integer", (c) => c.primaryKey()).addColumn("user_id", "text", (c) => c.notNull()).compile().sql
1038
+ );
1039
+ }
1040
+ function boundUserId(db) {
1041
+ ensureOwnerTable(db);
1042
+ const row = db.prepare(
1043
+ compiler.selectFrom(OWNER_TABLE).select("user_id").where("id", "=", import_kysely5.sql.lit(0)).compile().sql
1044
+ ).get();
1045
+ return row?.user_id ?? null;
1046
+ }
1047
+ function libraryUserSegment(db) {
1048
+ return boundUserId(db) ?? import_protocol_primitives6.LOCAL_USER_SEGMENT;
1049
+ }
1050
+ function libraryKeyColumns(db) {
1051
+ return coreKeyColumns(db).filter(({ table }) => tableExists(db, table));
1052
+ }
1053
+ function coreKeyColumns(db) {
1054
+ const columns = [
1055
+ { table: "shared_records", column: "object_storage_key", primaryKey: false },
1056
+ { table: "shared_object_availability", column: "object_storage_key", primaryKey: true },
1057
+ { table: "resident_keys", column: "object_storage_key", primaryKey: true },
1058
+ { table: "resident_keys", column: "record_id", primaryKey: false }
1059
+ ];
1060
+ const tables = db.prepare(
1061
+ compiler.selectFrom("sqlite_master").select("name").where("type", "=", import_kysely5.sql.lit("table")).compile().sql
1062
+ ).all();
1063
+ for (const { name } of tables) {
1064
+ if (name.endsWith("__starkeep_sync_records")) {
1065
+ columns.push(
1066
+ { table: name, column: "id", primaryKey: true },
1067
+ { table: name, column: "object_storage_key", primaryKey: false }
1068
+ );
1069
+ }
1070
+ }
1071
+ return columns;
1072
+ }
1073
+ function tableExists(db, table) {
1074
+ return db.prepare(
1075
+ compiler.selectFrom("sqlite_master").select("name").where("type", "=", import_kysely5.sql.lit("table")).where("name", "=", import_kysely5.sql.lit(table)).compile().sql
1076
+ ).get() !== void 0;
1077
+ }
1078
+ function namespaces(column, userId) {
1079
+ return [
1080
+ {
1081
+ local: import_kysely5.sql.lit(`shared/${import_protocol_primitives6.LOCAL_USER_SEGMENT}/%`),
1082
+ // Keeps the slash after `local`, so `shared/local/x` becomes `shared/<userId>/x`.
1083
+ rebound: import_kysely5.sql`'shared/' || ${import_kysely5.sql.lit(userId)} || substr(${import_kysely5.sql.ref(column)}, ${import_kysely5.sql.lit(`shared/${import_protocol_primitives6.LOCAL_USER_SEGMENT}`.length + 1)})`
1084
+ },
1085
+ // apps/<appId>/local/syncable/... — the app id is the segment before `local`.
1086
+ {
1087
+ local: import_kysely5.sql.lit(`apps/%/${import_protocol_primitives6.LOCAL_USER_SEGMENT}/syncable/%`),
1088
+ rebound: import_kysely5.sql`replace(${import_kysely5.sql.ref(column)}, ${import_kysely5.sql.lit(`/${import_protocol_primitives6.LOCAL_USER_SEGMENT}/syncable/`)}, ${import_kysely5.sql.lit(`/${userId}/syncable/`)})`
1089
+ }
1090
+ ];
1091
+ }
1092
+ function rebindColumn(db, { table, column, primaryKey }, userId) {
1093
+ let changed = 0;
1094
+ for (const { local, rebound } of namespaces(column, userId)) {
1095
+ if (primaryKey) {
1096
+ const duplicates = compiler.deleteFrom(table).where(import_kysely5.sql.ref(column), "like", local).where(
1097
+ import_kysely5.sql`${rebound} in (${compiler.selectFrom(table).select(import_kysely5.sql.ref(column).as("k"))})`
1098
+ ).compile();
1099
+ db.prepare(duplicates.sql).run(...duplicates.parameters);
1100
+ }
1101
+ const update = compiler.updateTable(table).set({ [column]: rebound }).where(import_kysely5.sql.ref(column), "like", local).compile();
1102
+ const result = db.prepare(update.sql).run(...update.parameters);
1103
+ changed += Number(result.changes ?? 0);
1104
+ }
1105
+ return changed;
1106
+ }
1107
+ var LibraryBoundElsewhereError = class extends Error {
1108
+ constructor(boundTo, cloudSays) {
1109
+ super(
1110
+ `This library belongs to user ${boundTo}, but the cloud says this device syncs for user ${cloudSays}. Sync stays off so the two libraries do not mix.`
1111
+ );
1112
+ this.boundTo = boundTo;
1113
+ this.cloudSays = cloudSays;
1114
+ this.name = "LibraryBoundElsewhereError";
1115
+ }
1116
+ boundTo;
1117
+ cloudSays;
1118
+ };
1119
+ function checkLibraryBindable(db, userId) {
1120
+ if (!import_protocol_primitives6.USER_SEGMENT_PATTERN.test(userId) || userId === import_protocol_primitives6.LOCAL_USER_SEGMENT) {
1121
+ throw new Error(`cannot bind the library to ${JSON.stringify(userId)}`);
1122
+ }
1123
+ const current = boundUserId(db);
1124
+ if (current !== null && current !== userId) throw new LibraryBoundElsewhereError(current, userId);
1125
+ }
1126
+ function commitLibraryBinding(db, userId, extraKeyColumns = []) {
1127
+ checkLibraryBindable(db, userId);
1128
+ const columns = [...coreKeyColumns(db), ...extraKeyColumns].filter(
1129
+ ({ table }) => tableExists(db, table)
1130
+ );
1131
+ let rewritten = 0;
1132
+ db.exec("BEGIN");
1133
+ try {
1134
+ for (const column of columns) rewritten += rebindColumn(db, column, userId);
1135
+ db.prepare(
1136
+ compiler.insertInto(OWNER_TABLE).values({ id: import_kysely5.sql.lit(0), user_id: import_kysely5.sql.raw("?") }).onConflict(
1137
+ (oc) => oc.column("id").doUpdateSet((eb) => ({ user_id: eb.ref("excluded.user_id") }))
1138
+ ).compile().sql
1139
+ ).run(userId);
1140
+ db.exec("COMMIT");
1141
+ } catch (err) {
1142
+ db.exec("ROLLBACK");
1143
+ throw err;
822
1144
  }
823
- return { timestamp, appId, table, op: "insert", row };
1145
+ return rewritten;
824
1146
  }
825
1147
  // Annotate the CommonJS export names for ESM import in node:
826
1148
  0 && (module.exports = {
1149
+ LibraryBoundElsewhereError,
827
1150
  SqliteAppSyncableApplier,
828
1151
  SqliteAppSyncableNamespaceStore,
829
1152
  SqliteDatabaseAdapter,
830
1153
  appSyncableTableName,
1154
+ applyConnectionPragmas,
1155
+ boundUserId,
1156
+ checkLibraryBindable,
1157
+ commitLibraryBinding,
831
1158
  deleteAppSyncableNamespace,
832
1159
  getAppSyncableNamespace,
833
1160
  initializeLocalSchema,
1161
+ libraryKeyColumns,
1162
+ libraryUserSegment,
834
1163
  listAppSyncableNamespaces,
835
1164
  sqliteCompiler,
836
1165
  upsertAppSyncableNamespace