@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 +319 -253
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +27 -36
- package/dist/index.d.ts +27 -36
- package/dist/index.js +333 -254
- package/dist/index.js.map +1 -1
- package/package.json +8 -9
package/dist/index.js
CHANGED
|
@@ -2,17 +2,37 @@
|
|
|
2
2
|
import { DatabaseSync } from "sqlite";
|
|
3
3
|
import { mkdirSync, existsSync } from "fs";
|
|
4
4
|
import { dirname } from "path";
|
|
5
|
-
import { serializeHLC as serializeHLC2, sqliteMetadataTableName } from "@starkeep/protocol-primitives";
|
|
6
|
-
import {
|
|
5
|
+
import { serializeHLC as serializeHLC2, deserializeHLC as deserializeHLC2, sqliteMetadataTableName } from "@starkeep/protocol-primitives";
|
|
6
|
+
import {
|
|
7
|
+
StorageError,
|
|
8
|
+
TransactionError,
|
|
9
|
+
buildFindByLabel,
|
|
10
|
+
buildGetLabel,
|
|
11
|
+
buildLabelNodeWatermarks,
|
|
12
|
+
buildLabelRetraction,
|
|
13
|
+
buildLabelValueReplacementTombstone,
|
|
14
|
+
buildLabelSnapshotUpsert,
|
|
15
|
+
buildLabelUpsert,
|
|
16
|
+
buildLabelsByRecordIds,
|
|
17
|
+
buildQueryLabels,
|
|
18
|
+
buildTombstoneLabelsForRecord,
|
|
19
|
+
groupLabelsByRecordId,
|
|
20
|
+
paginateFindByLabel,
|
|
21
|
+
paginateLabelScan,
|
|
22
|
+
rowToLabel as rowToLabel2
|
|
23
|
+
} from "@starkeep/storage-adapter";
|
|
24
|
+
import { sql as kSql } from "kysely";
|
|
7
25
|
|
|
8
26
|
// src/serialization.ts
|
|
9
27
|
import { serializeHLC, deserializeHLC, createStarkeepId } from "@starkeep/protocol-primitives";
|
|
28
|
+
import { rowToLabel, labelToRow } from "@starkeep/storage-adapter";
|
|
10
29
|
function recordToRow(record) {
|
|
11
30
|
return {
|
|
12
31
|
id: record.id,
|
|
13
32
|
type: record.type,
|
|
14
33
|
created_at: serializeHLC(record.createdAt),
|
|
15
34
|
updated_at: serializeHLC(record.updatedAt),
|
|
35
|
+
node_id: record.updatedAt.nodeId,
|
|
16
36
|
deleted_at: record.deletedAt ? serializeHLC(record.deletedAt) : null,
|
|
17
37
|
version: record.version,
|
|
18
38
|
content_hash: record.contentHash,
|
|
@@ -132,96 +152,99 @@ function applyFilter(qb, filter) {
|
|
|
132
152
|
}
|
|
133
153
|
|
|
134
154
|
// src/schema/bootstrap.ts
|
|
155
|
+
import { sql } from "kysely";
|
|
135
156
|
import { CATEGORIES, sqliteMetadataDdl } from "@starkeep/protocol-primitives";
|
|
136
|
-
function
|
|
157
|
+
function applyLocalSchemaDdl(db) {
|
|
137
158
|
db.exec("PRAGMA journal_mode = WAL");
|
|
138
159
|
db.exec("PRAGMA foreign_keys = ON");
|
|
139
|
-
db.exec(`
|
|
140
|
-
CREATE TABLE IF NOT EXISTS shared_records (
|
|
141
|
-
id TEXT PRIMARY KEY,
|
|
142
|
-
type TEXT NOT NULL,
|
|
143
|
-
created_at TEXT NOT NULL,
|
|
144
|
-
updated_at TEXT NOT NULL,
|
|
145
|
-
deleted_at TEXT,
|
|
146
|
-
version INTEGER NOT NULL DEFAULT 1,
|
|
147
|
-
content_hash TEXT NOT NULL,
|
|
148
|
-
object_storage_key TEXT NOT NULL,
|
|
149
|
-
mime_type TEXT,
|
|
150
|
-
size_bytes INTEGER NOT NULL,
|
|
151
|
-
original_filename TEXT,
|
|
152
|
-
origin_app_id TEXT NOT NULL,
|
|
153
|
-
parent_id TEXT
|
|
154
|
-
)
|
|
155
|
-
`);
|
|
156
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_type ON shared_records(type)");
|
|
157
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_origin_app ON shared_records(origin_app_id)");
|
|
158
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_parent_id ON shared_records(parent_id)");
|
|
159
160
|
db.exec(
|
|
160
|
-
"
|
|
161
|
+
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
|
|
162
|
+
);
|
|
163
|
+
const sharedRecordsIndexes = [
|
|
164
|
+
compiler.schema.createIndex("idx_shared_records_type").ifNotExists().on("shared_records").column("type"),
|
|
165
|
+
compiler.schema.createIndex("idx_shared_records_node_watermark").ifNotExists().on("shared_records").columns(["node_id", "updated_at"]),
|
|
166
|
+
compiler.schema.createIndex("idx_shared_records_origin_app").ifNotExists().on("shared_records").column("origin_app_id"),
|
|
167
|
+
compiler.schema.createIndex("idx_shared_records_parent_id").ifNotExists().on("shared_records").column("parent_id"),
|
|
168
|
+
// Duplicate-file prevention: (filename + bytes) is unique among live
|
|
169
|
+
// records. Tombstoned rows (deleted_at IS NOT NULL) are excluded so a
|
|
170
|
+
// re-upload after delete is allowed. Records with NULL filename are not
|
|
171
|
+
// constrained — the rule requires both filename and content to match.
|
|
172
|
+
compiler.schema.createIndex("uq_shared_records_filename_hash").ifNotExists().unique().on("shared_records").columns(["original_filename", "content_hash"]).where(sql.ref("deleted_at"), "is", null).where(sql.ref("original_filename"), "is not", null)
|
|
173
|
+
];
|
|
174
|
+
for (const index of sharedRecordsIndexes) {
|
|
175
|
+
db.exec(index.compile().sql);
|
|
176
|
+
}
|
|
177
|
+
db.exec(
|
|
178
|
+
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(sql`access IN ('read', 'readwrite')`)).addColumn("metadata_write", "integer", (c) => c.notNull().defaultTo(0)).addColumn("created_at", "text", (c) => c.notNull().defaultTo(sql`(datetime('now'))`)).addPrimaryKeyConstraint("pk_shared_access_grants", ["app_id", "type_id"]).compile().sql
|
|
179
|
+
);
|
|
180
|
+
db.exec(
|
|
181
|
+
compiler.schema.createIndex("idx_shared_access_grants_app").ifNotExists().on("shared_access_grants").column("app_id").compile().sql
|
|
182
|
+
);
|
|
183
|
+
db.exec(
|
|
184
|
+
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", [
|
|
185
|
+
"record_id",
|
|
186
|
+
"app_id",
|
|
187
|
+
"key",
|
|
188
|
+
"value"
|
|
189
|
+
]).compile().sql
|
|
190
|
+
);
|
|
191
|
+
const sharedRecordLabelsIndexes = [
|
|
192
|
+
// The reverse path. Column order mirrors DSQL's idx_record_labels_reverse
|
|
193
|
+
// exactly — see the long comment there for why each column is where it is.
|
|
194
|
+
// SQLite *does* support partial indexes, so `deleted_at` could have been a
|
|
195
|
+
// WHERE clause here; it is a key column instead so the two backends have
|
|
196
|
+
// one index shape between them and a query planned on one is planned the
|
|
197
|
+
// same on the other.
|
|
198
|
+
compiler.schema.createIndex("idx_shared_record_labels_reverse").ifNotExists().on("shared_record_labels").columns(["app_id", "key", "deleted_at", "value", "record_id"]),
|
|
199
|
+
compiler.schema.createIndex("idx_shared_record_labels_node_watermark").ifNotExists().on("shared_record_labels").columns(["node_id", "updated_at"])
|
|
200
|
+
];
|
|
201
|
+
for (const index of sharedRecordLabelsIndexes) {
|
|
202
|
+
db.exec(index.compile().sql);
|
|
203
|
+
}
|
|
204
|
+
db.exec(
|
|
205
|
+
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(sql`(datetime('now'))`)).addPrimaryKeyConstraint("pk_shared_app_label_keys", ["app_id", "key"]).compile().sql
|
|
206
|
+
);
|
|
207
|
+
db.exec(
|
|
208
|
+
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(sql`(datetime('now'))`)).addColumn("updated_at", "text", (c) => c.notNull().defaultTo(sql`(datetime('now'))`)).compile().sql
|
|
209
|
+
);
|
|
210
|
+
db.exec(
|
|
211
|
+
compiler.schema.createTable("shared_app_install_steps").ifNotExists().addColumn("app_id", "text", (c) => c.notNull()).addColumn(
|
|
212
|
+
"operation",
|
|
213
|
+
"text",
|
|
214
|
+
(c) => c.notNull().check(sql`operation IN ('install', 'uninstall')`)
|
|
215
|
+
).addColumn("step", "text", (c) => c.notNull()).addColumn(
|
|
216
|
+
"status",
|
|
217
|
+
"text",
|
|
218
|
+
(c) => c.notNull().check(sql`status IN ('pending', 'done', 'failed')`)
|
|
219
|
+
).addColumn("error", "text").addColumn("updated_at", "text", (c) => c.notNull().defaultTo(sql`(datetime('now'))`)).addPrimaryKeyConstraint("pk_shared_app_install_steps", [
|
|
220
|
+
"app_id",
|
|
221
|
+
"operation",
|
|
222
|
+
"step"
|
|
223
|
+
]).compile().sql
|
|
224
|
+
);
|
|
225
|
+
db.exec(
|
|
226
|
+
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(sql`(datetime('now'))`)).compile().sql
|
|
161
227
|
);
|
|
162
|
-
db.exec(`
|
|
163
|
-
CREATE TABLE IF NOT EXISTS shared_access_grants (
|
|
164
|
-
app_id TEXT NOT NULL,
|
|
165
|
-
type_id TEXT NOT NULL,
|
|
166
|
-
access TEXT NOT NULL CHECK (access IN ('read', 'readwrite')),
|
|
167
|
-
metadata_write INTEGER NOT NULL DEFAULT 0,
|
|
168
|
-
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
169
|
-
PRIMARY KEY (app_id, type_id)
|
|
170
|
-
)
|
|
171
|
-
`);
|
|
172
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_access_grants_app ON shared_access_grants(app_id)");
|
|
173
|
-
db.exec(`
|
|
174
|
-
CREATE TABLE IF NOT EXISTS shared_app_registry (
|
|
175
|
-
app_id TEXT PRIMARY KEY,
|
|
176
|
-
name TEXT NOT NULL,
|
|
177
|
-
version TEXT NOT NULL,
|
|
178
|
-
tier TEXT NOT NULL DEFAULT 'app',
|
|
179
|
-
manifest TEXT NOT NULL,
|
|
180
|
-
status TEXT NOT NULL DEFAULT 'installing',
|
|
181
|
-
hmac_secret TEXT NOT NULL,
|
|
182
|
-
installed_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
183
|
-
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
184
|
-
)
|
|
185
|
-
`);
|
|
186
|
-
db.exec(`
|
|
187
|
-
CREATE TABLE IF NOT EXISTS shared_app_install_steps (
|
|
188
|
-
app_id TEXT NOT NULL,
|
|
189
|
-
operation TEXT NOT NULL CHECK (operation IN ('install', 'uninstall')),
|
|
190
|
-
step TEXT NOT NULL,
|
|
191
|
-
status TEXT NOT NULL CHECK (status IN ('pending', 'done', 'failed')),
|
|
192
|
-
error TEXT,
|
|
193
|
-
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
194
|
-
PRIMARY KEY (app_id, operation, step)
|
|
195
|
-
)
|
|
196
|
-
`);
|
|
197
|
-
db.exec(`
|
|
198
|
-
CREATE TABLE IF NOT EXISTS app_syncable_namespaces (
|
|
199
|
-
app_id TEXT PRIMARY KEY,
|
|
200
|
-
tables_json TEXT NOT NULL,
|
|
201
|
-
files_enabled INTEGER NOT NULL DEFAULT 0,
|
|
202
|
-
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
203
|
-
)
|
|
204
|
-
`);
|
|
205
|
-
db.exec(`
|
|
206
|
-
CREATE TABLE IF NOT EXISTS access_policies (
|
|
207
|
-
policy_id TEXT PRIMARY KEY,
|
|
208
|
-
subject_type TEXT NOT NULL,
|
|
209
|
-
subject_id TEXT NOT NULL,
|
|
210
|
-
resource_type TEXT NOT NULL,
|
|
211
|
-
resource_id TEXT NOT NULL,
|
|
212
|
-
permissions TEXT NOT NULL,
|
|
213
|
-
granted_at TEXT NOT NULL,
|
|
214
|
-
expires_at TEXT
|
|
215
|
-
)
|
|
216
|
-
`);
|
|
217
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_access_policies_subject ON access_policies(subject_type, subject_id)");
|
|
218
228
|
for (const c of CATEGORIES) {
|
|
219
229
|
if (c.id === "other") continue;
|
|
220
230
|
db.exec(sqliteMetadataDdl(c));
|
|
221
231
|
}
|
|
222
232
|
}
|
|
233
|
+
function initializeLocalSchema(db) {
|
|
234
|
+
try {
|
|
235
|
+
applyLocalSchemaDdl(db);
|
|
236
|
+
} catch (err) {
|
|
237
|
+
throw new Error(
|
|
238
|
+
`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.`,
|
|
239
|
+
{ cause: err }
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
223
243
|
|
|
224
244
|
// src/adapter.ts
|
|
245
|
+
var LABELS = {
|
|
246
|
+
table: "shared_record_labels"
|
|
247
|
+
};
|
|
225
248
|
var SqliteDatabaseAdapter = class {
|
|
226
249
|
database = null;
|
|
227
250
|
options;
|
|
@@ -246,7 +269,7 @@ var SqliteDatabaseAdapter = class {
|
|
|
246
269
|
async healthCheck() {
|
|
247
270
|
if (!this.database) return false;
|
|
248
271
|
try {
|
|
249
|
-
this.database.prepare(
|
|
272
|
+
this.database.prepare(kSql`SELECT 1`.compile(compiler).sql).get();
|
|
250
273
|
return true;
|
|
251
274
|
} catch {
|
|
252
275
|
return false;
|
|
@@ -264,43 +287,53 @@ var SqliteDatabaseAdapter = class {
|
|
|
264
287
|
getRawDatabase() {
|
|
265
288
|
return this.getDatabase();
|
|
266
289
|
}
|
|
267
|
-
runStmt(
|
|
268
|
-
this.getDatabase().prepare(
|
|
290
|
+
runStmt(sql3, ...params) {
|
|
291
|
+
this.getDatabase().prepare(sql3).run(...params);
|
|
269
292
|
}
|
|
270
|
-
getRow(
|
|
271
|
-
return this.getDatabase().prepare(
|
|
293
|
+
getRow(sql3, ...params) {
|
|
294
|
+
return this.getDatabase().prepare(sql3).get(
|
|
272
295
|
...params
|
|
273
296
|
);
|
|
274
297
|
}
|
|
275
|
-
allRows(
|
|
276
|
-
return this.getDatabase().prepare(
|
|
298
|
+
allRows(sql3, ...params) {
|
|
299
|
+
return this.getDatabase().prepare(sql3).all(
|
|
277
300
|
...params
|
|
278
301
|
);
|
|
279
302
|
}
|
|
280
303
|
async put(record) {
|
|
281
304
|
const row = recordToRow(record);
|
|
282
|
-
const
|
|
283
|
-
const
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
305
|
+
const updateColumns = Object.keys(row).filter((column) => column !== "id");
|
|
306
|
+
const query = compiler.insertInto("shared_records").values({ ...row }).onConflict(
|
|
307
|
+
(oc) => oc.column("id").doUpdateSet(
|
|
308
|
+
(eb) => Object.fromEntries(
|
|
309
|
+
updateColumns.map((column) => [column, eb.ref(`excluded.${column}`)])
|
|
310
|
+
)
|
|
311
|
+
)
|
|
312
|
+
).compile();
|
|
313
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
287
314
|
}
|
|
288
315
|
async get(id) {
|
|
289
|
-
const
|
|
316
|
+
const query = compiler.selectFrom("shared_records").selectAll().where("id", "=", id).compile();
|
|
317
|
+
const row = this.getRow(query.sql, ...query.parameters);
|
|
290
318
|
return row ? rowToRecord(row) : null;
|
|
291
319
|
}
|
|
292
320
|
async delete(id, hlc) {
|
|
293
321
|
const ts = serializeHLC2(hlc);
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
);
|
|
322
|
+
const query = compiler.updateTable("shared_records").set({ deleted_at: ts, updated_at: ts, node_id: hlc.nodeId }).where("id", "=", id).compile();
|
|
323
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
324
|
+
}
|
|
325
|
+
async getNodeWatermarks() {
|
|
326
|
+
const watermarksQuery = compiler.selectFrom("shared_records").select(({ fn }) => ["node_id", fn.max("updated_at").as("max_updated_at")]).groupBy("node_id").compile();
|
|
327
|
+
const rows = this.allRows(watermarksQuery.sql);
|
|
328
|
+
const out = {};
|
|
329
|
+
for (const row of rows) {
|
|
330
|
+
out[row.node_id] = deserializeHLC2(row.max_updated_at);
|
|
331
|
+
}
|
|
332
|
+
return out;
|
|
300
333
|
}
|
|
301
334
|
async query(query) {
|
|
302
|
-
const { sql, params } = buildSelectQuery(query);
|
|
303
|
-
const rows = this.allRows(
|
|
335
|
+
const { sql: sql3, params } = buildSelectQuery(query);
|
|
336
|
+
const rows = this.allRows(sql3, ...params);
|
|
304
337
|
const limit = query.limit;
|
|
305
338
|
const hasMore = limit ? rows.length > limit : false;
|
|
306
339
|
const resultRows = hasMore ? rows.slice(0, limit) : rows;
|
|
@@ -346,24 +379,23 @@ var SqliteDatabaseAdapter = class {
|
|
|
346
379
|
}
|
|
347
380
|
async putMetadata(typeId, row) {
|
|
348
381
|
const table = sqliteMetadataTableName(typeId);
|
|
349
|
-
const
|
|
350
|
-
const values = [row.recordId];
|
|
382
|
+
const values = { record_id: row.recordId };
|
|
351
383
|
for (const [key, value] of Object.entries(row)) {
|
|
352
384
|
if (key === "recordId") continue;
|
|
353
|
-
|
|
354
|
-
values.push(value);
|
|
385
|
+
values[key] = value;
|
|
355
386
|
}
|
|
356
|
-
const
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
|
|
387
|
+
const updateColumns = Object.keys(values).filter((c) => c !== "record_id");
|
|
388
|
+
const query = compiler.insertInto(table).values(values).onConflict(
|
|
389
|
+
(oc) => updateColumns.length > 0 ? oc.column("record_id").doUpdateSet(
|
|
390
|
+
(eb) => Object.fromEntries(updateColumns.map((c) => [c, eb.ref(`excluded.${c}`)]))
|
|
391
|
+
) : oc.column("record_id").doNothing()
|
|
392
|
+
).compile();
|
|
393
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
360
394
|
}
|
|
361
395
|
async getMetadata(typeId, recordId) {
|
|
362
396
|
const table = sqliteMetadataTableName(typeId);
|
|
363
|
-
const
|
|
364
|
-
|
|
365
|
-
recordId
|
|
366
|
-
);
|
|
397
|
+
const query = compiler.selectFrom(table).selectAll().where("record_id", "=", recordId).compile();
|
|
398
|
+
const row = this.getRow(query.sql, ...query.parameters);
|
|
367
399
|
if (!row) return null;
|
|
368
400
|
return columnsToMetadataRow(recordId, row);
|
|
369
401
|
}
|
|
@@ -371,11 +403,8 @@ var SqliteDatabaseAdapter = class {
|
|
|
371
403
|
const result = /* @__PURE__ */ new Map();
|
|
372
404
|
if (recordIds.length === 0) return result;
|
|
373
405
|
const table = sqliteMetadataTableName(typeId);
|
|
374
|
-
const
|
|
375
|
-
const rows = this.allRows(
|
|
376
|
-
`SELECT * FROM ${table} WHERE record_id IN (${placeholders})`,
|
|
377
|
-
...recordIds
|
|
378
|
-
);
|
|
406
|
+
const query = compiler.selectFrom(table).selectAll().where("record_id", "in", recordIds).compile();
|
|
407
|
+
const rows = this.allRows(query.sql, ...query.parameters);
|
|
379
408
|
for (const row of rows) {
|
|
380
409
|
const recordId = row["record_id"];
|
|
381
410
|
result.set(recordId, columnsToMetadataRow(recordId, row));
|
|
@@ -384,7 +413,93 @@ var SqliteDatabaseAdapter = class {
|
|
|
384
413
|
}
|
|
385
414
|
async deleteMetadata(typeId, recordId) {
|
|
386
415
|
const table = sqliteMetadataTableName(typeId);
|
|
387
|
-
|
|
416
|
+
const query = compiler.deleteFrom(table).where("record_id", "=", recordId).compile();
|
|
417
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
418
|
+
}
|
|
419
|
+
// ---- Cross-app record labels -------------------------------------------
|
|
420
|
+
//
|
|
421
|
+
// The SQL for all of these is built in `@starkeep/storage-adapter`, shared
|
|
422
|
+
// with the DSQL adapter: same columns, same ON CONFLICT set, same cursor
|
|
423
|
+
// predicate, same paging. Only the table name is this backend's business,
|
|
424
|
+
// and it lives in LABELS above.
|
|
425
|
+
async upsertLabels(labels) {
|
|
426
|
+
if (labels.length === 0) return;
|
|
427
|
+
const query = buildLabelUpsert(compiler, LABELS, labels);
|
|
428
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
429
|
+
}
|
|
430
|
+
async retractLabels(retractions) {
|
|
431
|
+
for (const r of retractions) {
|
|
432
|
+
const query = buildLabelRetraction(compiler, LABELS, r);
|
|
433
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
async replaceLabelValues(replacements) {
|
|
437
|
+
if (replacements.length === 0) return;
|
|
438
|
+
this.getDatabase().exec("BEGIN");
|
|
439
|
+
try {
|
|
440
|
+
for (const r of replacements) {
|
|
441
|
+
const tombstone = buildLabelValueReplacementTombstone(compiler, LABELS, r);
|
|
442
|
+
this.runStmt(tombstone.sql, ...tombstone.parameters);
|
|
443
|
+
if (r.values.length > 0) {
|
|
444
|
+
const upsert = buildLabelUpsert(
|
|
445
|
+
compiler,
|
|
446
|
+
LABELS,
|
|
447
|
+
r.values.map((value) => ({
|
|
448
|
+
recordId: r.recordId,
|
|
449
|
+
appId: r.appId,
|
|
450
|
+
key: r.key,
|
|
451
|
+
value,
|
|
452
|
+
recordType: r.recordType,
|
|
453
|
+
hlc: r.hlc
|
|
454
|
+
}))
|
|
455
|
+
);
|
|
456
|
+
this.runStmt(upsert.sql, ...upsert.parameters);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
this.getDatabase().exec("COMMIT");
|
|
460
|
+
} catch (error) {
|
|
461
|
+
this.getDatabase().exec("ROLLBACK");
|
|
462
|
+
throw error;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
async getLabelsByRecordIds(recordIds) {
|
|
466
|
+
if (recordIds.length === 0) return /* @__PURE__ */ new Map();
|
|
467
|
+
const query = buildLabelsByRecordIds(compiler, LABELS, recordIds);
|
|
468
|
+
return groupLabelsByRecordId(
|
|
469
|
+
this.allRows(query.sql, ...query.parameters)
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
async findByLabel(query) {
|
|
473
|
+
const compiled = buildFindByLabel(compiler, LABELS, query);
|
|
474
|
+
if (!compiled) return { labels: [], nextCursor: null, hasMore: false };
|
|
475
|
+
const rows = this.allRows(compiled.sql, ...compiled.parameters);
|
|
476
|
+
return paginateFindByLabel(rows, query.limit);
|
|
477
|
+
}
|
|
478
|
+
// ---- Label sync ---------------------------------------------------------
|
|
479
|
+
async putLabel(label) {
|
|
480
|
+
const query = buildLabelSnapshotUpsert(compiler, LABELS, label);
|
|
481
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
482
|
+
}
|
|
483
|
+
async getLabel(recordId, appId, key, value) {
|
|
484
|
+
const query = buildGetLabel(compiler, LABELS, recordId, appId, key, value);
|
|
485
|
+
const row = this.getRow(query.sql, ...query.parameters);
|
|
486
|
+
return row ? rowToLabel2(row) : null;
|
|
487
|
+
}
|
|
488
|
+
async queryLabels(query) {
|
|
489
|
+
const compiled = buildQueryLabels(compiler, LABELS, query);
|
|
490
|
+
const rows = this.allRows(compiled.sql, ...compiled.parameters);
|
|
491
|
+
return paginateLabelScan(rows, query.limit);
|
|
492
|
+
}
|
|
493
|
+
async getLabelNodeWatermarks() {
|
|
494
|
+
const query = buildLabelNodeWatermarks(compiler, LABELS);
|
|
495
|
+
const rows = this.allRows(query.sql);
|
|
496
|
+
const out = {};
|
|
497
|
+
for (const row of rows) out[row.node_id] = deserializeHLC2(row.max_updated_at);
|
|
498
|
+
return out;
|
|
499
|
+
}
|
|
500
|
+
async tombstoneLabelsForRecord(recordId, hlc) {
|
|
501
|
+
const query = buildTombstoneLabelsForRecord(compiler, LABELS, recordId, hlc);
|
|
502
|
+
this.runStmt(query.sql, ...query.parameters);
|
|
388
503
|
}
|
|
389
504
|
};
|
|
390
505
|
function columnsToMetadataRow(recordId, columns) {
|
|
@@ -396,65 +511,6 @@ function columnsToMetadataRow(recordId, columns) {
|
|
|
396
511
|
return row;
|
|
397
512
|
}
|
|
398
513
|
|
|
399
|
-
// src/control-plane-stores.ts
|
|
400
|
-
import {
|
|
401
|
-
createStarkeepId as createStarkeepId2,
|
|
402
|
-
deserializeHLC as deserializeHLC2,
|
|
403
|
-
serializeHLC as serializeHLC3
|
|
404
|
-
} from "@starkeep/protocol-primitives";
|
|
405
|
-
function createSqliteAccessPolicyStore(db) {
|
|
406
|
-
return {
|
|
407
|
-
async putPolicy(policy) {
|
|
408
|
-
db.prepare(
|
|
409
|
-
`INSERT INTO access_policies (
|
|
410
|
-
policy_id, subject_type, subject_id, resource_type, resource_id,
|
|
411
|
-
permissions, granted_at, expires_at
|
|
412
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
413
|
-
ON CONFLICT(policy_id) DO UPDATE SET
|
|
414
|
-
subject_type = excluded.subject_type,
|
|
415
|
-
subject_id = excluded.subject_id,
|
|
416
|
-
resource_type = excluded.resource_type,
|
|
417
|
-
resource_id = excluded.resource_id,
|
|
418
|
-
permissions = excluded.permissions,
|
|
419
|
-
granted_at = excluded.granted_at,
|
|
420
|
-
expires_at = excluded.expires_at`
|
|
421
|
-
).run(
|
|
422
|
-
policy.policyId,
|
|
423
|
-
policy.subjectType,
|
|
424
|
-
policy.subjectId,
|
|
425
|
-
policy.resourceType,
|
|
426
|
-
policy.resourceId,
|
|
427
|
-
policy.permissions.join(","),
|
|
428
|
-
serializeHLC3(policy.grantedAt),
|
|
429
|
-
policy.expiresAt ? serializeHLC3(policy.expiresAt) : null
|
|
430
|
-
);
|
|
431
|
-
},
|
|
432
|
-
async getPolicy(policyId) {
|
|
433
|
-
const row = db.prepare("SELECT * FROM access_policies WHERE policy_id = ?").get(policyId);
|
|
434
|
-
return row ? rowToPolicy(row) : null;
|
|
435
|
-
},
|
|
436
|
-
async listPolicies() {
|
|
437
|
-
const rows = db.prepare("SELECT * FROM access_policies").all();
|
|
438
|
-
return rows.map(rowToPolicy);
|
|
439
|
-
},
|
|
440
|
-
async deletePolicy(policyId) {
|
|
441
|
-
db.prepare("DELETE FROM access_policies WHERE policy_id = ?").run(policyId);
|
|
442
|
-
}
|
|
443
|
-
};
|
|
444
|
-
}
|
|
445
|
-
function rowToPolicy(row) {
|
|
446
|
-
return {
|
|
447
|
-
policyId: createStarkeepId2(row.policy_id),
|
|
448
|
-
subjectType: row.subject_type,
|
|
449
|
-
subjectId: row.subject_id,
|
|
450
|
-
resourceType: row.resource_type,
|
|
451
|
-
resourceId: row.resource_id,
|
|
452
|
-
permissions: row.permissions.split(",").filter(Boolean),
|
|
453
|
-
grantedAt: deserializeHLC2(row.granted_at),
|
|
454
|
-
expiresAt: row.expires_at ? deserializeHLC2(row.expires_at) : null
|
|
455
|
-
};
|
|
456
|
-
}
|
|
457
|
-
|
|
458
514
|
// src/app-syncable/namespace.ts
|
|
459
515
|
function normalizeAppId(appId) {
|
|
460
516
|
return appId.toLowerCase().replace(/-/g, "_");
|
|
@@ -472,34 +528,38 @@ function rowToNamespace(r) {
|
|
|
472
528
|
};
|
|
473
529
|
}
|
|
474
530
|
function upsertAppSyncableNamespace(db, appId, tables, filesEnabled) {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
531
|
+
const query = compiler.insertInto("app_syncable_namespaces").values({
|
|
532
|
+
app_id: appId,
|
|
533
|
+
tables_json: JSON.stringify(tables),
|
|
534
|
+
files_enabled: filesEnabled ? 1 : 0
|
|
535
|
+
}).onConflict(
|
|
536
|
+
(oc) => oc.column("app_id").doUpdateSet((eb) => ({
|
|
537
|
+
tables_json: eb.ref("excluded.tables_json"),
|
|
538
|
+
files_enabled: eb.ref("excluded.files_enabled")
|
|
539
|
+
}))
|
|
540
|
+
).compile();
|
|
541
|
+
db.prepare(query.sql).run(...query.parameters);
|
|
482
542
|
}
|
|
483
543
|
function deleteAppSyncableNamespace(db, appId) {
|
|
484
|
-
|
|
544
|
+
const query = compiler.deleteFrom("app_syncable_namespaces").where("app_id", "=", appId).compile();
|
|
545
|
+
db.prepare(query.sql).run(...query.parameters);
|
|
485
546
|
}
|
|
486
547
|
function getAppSyncableNamespace(db, appId) {
|
|
487
|
-
const
|
|
488
|
-
|
|
489
|
-
).get(appId);
|
|
548
|
+
const query = compiler.selectFrom("app_syncable_namespaces").select(["app_id", "tables_json", "files_enabled"]).where("app_id", "=", appId).compile();
|
|
549
|
+
const row = db.prepare(query.sql).get(...query.parameters);
|
|
490
550
|
if (!row) return null;
|
|
491
551
|
return rowToNamespace(row);
|
|
492
552
|
}
|
|
493
553
|
function listAppSyncableNamespaces(db) {
|
|
494
|
-
const
|
|
495
|
-
|
|
496
|
-
).all();
|
|
554
|
+
const query = compiler.selectFrom("app_syncable_namespaces").select(["app_id", "tables_json", "files_enabled"]).compile();
|
|
555
|
+
const rows = db.prepare(query.sql).all();
|
|
497
556
|
return rows.map(rowToNamespace);
|
|
498
557
|
}
|
|
499
558
|
var SqliteAppSyncableNamespaceStore = class {
|
|
500
559
|
constructor(db) {
|
|
501
560
|
this.db = db;
|
|
502
561
|
}
|
|
562
|
+
db;
|
|
503
563
|
get(appId) {
|
|
504
564
|
return getAppSyncableNamespace(this.db, appId);
|
|
505
565
|
}
|
|
@@ -509,12 +569,21 @@ var SqliteAppSyncableNamespaceStore = class {
|
|
|
509
569
|
};
|
|
510
570
|
|
|
511
571
|
// src/app-syncable/apply.ts
|
|
512
|
-
import {
|
|
572
|
+
import { sql as sql2 } from "kysely";
|
|
573
|
+
import { serializeHLC as serializeHLC3, deserializeHLC as deserializeHLC3 } from "@starkeep/protocol-primitives";
|
|
574
|
+
function runCompiled(db, compiled) {
|
|
575
|
+
db.prepare(compiled.sql).run(...compiled.parameters);
|
|
576
|
+
}
|
|
577
|
+
function allCompiled(db, compiled) {
|
|
578
|
+
return db.prepare(compiled.sql).all(...compiled.parameters);
|
|
579
|
+
}
|
|
513
580
|
var SqliteAppSyncableApplier = class {
|
|
514
581
|
constructor(db, namespace) {
|
|
515
582
|
this.db = db;
|
|
516
583
|
this.namespace = namespace;
|
|
517
584
|
}
|
|
585
|
+
db;
|
|
586
|
+
namespace;
|
|
518
587
|
apply(entry) {
|
|
519
588
|
const ns = this.namespace.get(entry.appId);
|
|
520
589
|
if (!ns) {
|
|
@@ -539,69 +608,74 @@ var SqliteAppSyncableApplier = class {
|
|
|
539
608
|
}
|
|
540
609
|
}
|
|
541
610
|
applyInsert(fullName, pkColumns, entry) {
|
|
542
|
-
const row = entry.row ?? {};
|
|
611
|
+
const row = withNodeId(entry.row ?? {}, entry);
|
|
543
612
|
const cols = Object.keys(row);
|
|
544
613
|
if (cols.length === 0) return;
|
|
545
|
-
const colList = cols.map(q).join(", ");
|
|
546
|
-
const placeholders = cols.map(() => "?").join(", ");
|
|
547
|
-
const values = cols.map((c) => row[c]);
|
|
548
|
-
if (pkColumns.length === 0) {
|
|
549
|
-
this.db.prepare(`INSERT OR IGNORE INTO ${q(fullName)} (${colList}) VALUES (${placeholders})`).run(...values);
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
const conflictTarget = pkColumns.map(q).join(", ");
|
|
553
614
|
const updateCols = cols.filter((c) => !pkColumns.includes(c));
|
|
554
|
-
if (updateCols.length === 0) {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
615
|
+
if (pkColumns.length === 0 || updateCols.length === 0) {
|
|
616
|
+
runCompiled(
|
|
617
|
+
this.db,
|
|
618
|
+
compiler.insertInto(fullName).orIgnore().values({ ...row }).compile()
|
|
619
|
+
);
|
|
558
620
|
return;
|
|
559
621
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
622
|
+
runCompiled(
|
|
623
|
+
this.db,
|
|
624
|
+
compiler.insertInto(fullName).values({ ...row }).onConflict(
|
|
625
|
+
(oc) => oc.columns(pkColumns).doUpdateSet(
|
|
626
|
+
(eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
|
|
627
|
+
).where(sql2.ref("excluded.updated_at"), ">", sql2.ref(`${fullName}.updated_at`))
|
|
628
|
+
).compile()
|
|
629
|
+
);
|
|
566
630
|
}
|
|
567
631
|
applyUpdate(fullName, entry) {
|
|
568
|
-
const
|
|
632
|
+
const rawPatch = entry.row ?? {};
|
|
633
|
+
const patch = rawPatch["updated_at"] ? withNodeId(rawPatch, entry) : rawPatch;
|
|
569
634
|
const where = entry.where ?? {};
|
|
570
635
|
const patchCols = Object.keys(patch);
|
|
571
636
|
const whereCols = Object.keys(where);
|
|
572
637
|
if (patchCols.length === 0) return;
|
|
573
|
-
const setClause = patchCols.map((c) => `${q(c)} = ?`).join(", ");
|
|
574
638
|
const incomingUpdatedAt = patch["updated_at"];
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
this.db.prepare(`UPDATE ${q(fullName)} SET ${setClause}${whereClause}`).run(...params);
|
|
639
|
+
let query = compiler.updateTable(fullName).set({ ...patch });
|
|
640
|
+
for (const c of whereCols) {
|
|
641
|
+
query = query.where(c, "=", where[c]);
|
|
642
|
+
}
|
|
643
|
+
if (incomingUpdatedAt) {
|
|
644
|
+
query = query.where("updated_at", "<", incomingUpdatedAt);
|
|
645
|
+
}
|
|
646
|
+
runCompiled(this.db, query.compile());
|
|
584
647
|
}
|
|
585
648
|
applyDelete(fullName, entry) {
|
|
586
649
|
const where = entry.where ?? {};
|
|
587
650
|
const whereCols = Object.keys(where);
|
|
588
651
|
const incomingUpdatedAt = entry.row?.["updated_at"];
|
|
589
|
-
const ts = incomingUpdatedAt ??
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
652
|
+
const ts = incomingUpdatedAt ?? serializeHLC3(entry.timestamp);
|
|
653
|
+
let query = compiler.updateTable(fullName).set({ deleted_at: ts, updated_at: ts, node_id: nodeIdOf(ts, entry) });
|
|
654
|
+
for (const c of whereCols) {
|
|
655
|
+
query = query.where(c, "=", where[c]);
|
|
656
|
+
}
|
|
657
|
+
query = query.where(
|
|
658
|
+
(eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)])
|
|
659
|
+
);
|
|
660
|
+
runCompiled(this.db, query.compile());
|
|
661
|
+
}
|
|
662
|
+
/** See `ScanCapableApplier.getNodeWatermarks`. Missing table → `{}`. */
|
|
663
|
+
async getNodeWatermarks(appId, table) {
|
|
664
|
+
const fullName = appSyncableTableName(appId, table);
|
|
665
|
+
let rows;
|
|
666
|
+
try {
|
|
667
|
+
rows = allCompiled(
|
|
668
|
+
this.db,
|
|
669
|
+
compiler.selectFrom(fullName).select(({ fn }) => ["node_id", fn.max("updated_at").as("max_updated_at")]).groupBy("node_id").compile()
|
|
670
|
+
);
|
|
671
|
+
} catch {
|
|
672
|
+
return {};
|
|
673
|
+
}
|
|
674
|
+
const out = {};
|
|
675
|
+
for (const row of rows) {
|
|
676
|
+
out[row.node_id] = deserializeHLC3(row.max_updated_at);
|
|
677
|
+
}
|
|
678
|
+
return out;
|
|
605
679
|
}
|
|
606
680
|
/**
|
|
607
681
|
* Pull-side synthesis: return rows updated after `sinceHlcStr` (or `cursor`
|
|
@@ -616,15 +690,11 @@ var SqliteAppSyncableApplier = class {
|
|
|
616
690
|
const limit = options?.limit;
|
|
617
691
|
let rows;
|
|
618
692
|
try {
|
|
693
|
+
let query = compiler.selectFrom(fullName).selectAll().where("updated_at", ">", floor).orderBy("updated_at", "asc");
|
|
619
694
|
if (limit !== void 0) {
|
|
620
|
-
|
|
621
|
-
`SELECT * FROM ${q(fullName)} WHERE updated_at > ? ORDER BY updated_at ASC LIMIT ?`
|
|
622
|
-
).all(floor, limit + 1);
|
|
623
|
-
} else {
|
|
624
|
-
rows = this.db.prepare(
|
|
625
|
-
`SELECT * FROM ${q(fullName)} WHERE updated_at > ? ORDER BY updated_at ASC`
|
|
626
|
-
).all(floor);
|
|
695
|
+
query = query.limit(limit + 1);
|
|
627
696
|
}
|
|
697
|
+
rows = allCompiled(this.db, query.compile());
|
|
628
698
|
} catch {
|
|
629
699
|
return { rows: [], nextCursor: null, hasMore: false };
|
|
630
700
|
}
|
|
@@ -638,15 +708,24 @@ var SqliteAppSyncableApplier = class {
|
|
|
638
708
|
queryRows(appId, table, where) {
|
|
639
709
|
const fullName = appSyncableTableName(appId, table);
|
|
640
710
|
const whereCols = where ? Object.keys(where) : [];
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
return this.db
|
|
711
|
+
let query = compiler.selectFrom(fullName).selectAll().where("deleted_at", "is", null);
|
|
712
|
+
for (const c of whereCols) {
|
|
713
|
+
query = query.where(c, "=", where[c]);
|
|
714
|
+
}
|
|
715
|
+
return allCompiled(this.db, query.compile());
|
|
646
716
|
}
|
|
647
717
|
};
|
|
648
|
-
function
|
|
649
|
-
return
|
|
718
|
+
function withNodeId(row, entry) {
|
|
719
|
+
return { ...row, node_id: nodeIdOf(row["updated_at"], entry) };
|
|
720
|
+
}
|
|
721
|
+
function nodeIdOf(updatedAt, entry) {
|
|
722
|
+
if (typeof updatedAt === "string") {
|
|
723
|
+
try {
|
|
724
|
+
return deserializeHLC3(updatedAt).nodeId;
|
|
725
|
+
} catch {
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
return entry.timestamp.nodeId;
|
|
650
729
|
}
|
|
651
730
|
function rowToEntry(appId, table, row) {
|
|
652
731
|
const updatedAtStr = row["updated_at"];
|
|
@@ -662,11 +741,11 @@ export {
|
|
|
662
741
|
SqliteAppSyncableNamespaceStore,
|
|
663
742
|
SqliteDatabaseAdapter,
|
|
664
743
|
appSyncableTableName,
|
|
665
|
-
createSqliteAccessPolicyStore,
|
|
666
744
|
deleteAppSyncableNamespace,
|
|
667
745
|
getAppSyncableNamespace,
|
|
668
746
|
initializeLocalSchema,
|
|
669
747
|
listAppSyncableNamespaces,
|
|
748
|
+
compiler as sqliteCompiler,
|
|
670
749
|
upsertAppSyncableNamespace
|
|
671
750
|
};
|
|
672
751
|
//# sourceMappingURL=index.js.map
|