@starkeep/storage-sqlite 0.2.1 → 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 +281 -201
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -22
- package/dist/index.d.ts +24 -22
- package/dist/index.js +295 -199
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -3,10 +3,29 @@ import { DatabaseSync } from "sqlite";
|
|
|
3
3
|
import { mkdirSync, existsSync } from "fs";
|
|
4
4
|
import { dirname } from "path";
|
|
5
5
|
import { serializeHLC as serializeHLC2, deserializeHLC as deserializeHLC2, sqliteMetadataTableName } from "@starkeep/protocol-primitives";
|
|
6
|
-
import {
|
|
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,
|
|
@@ -22,8 +41,7 @@ function recordToRow(record) {
|
|
|
22
41
|
size_bytes: record.sizeBytes,
|
|
23
42
|
original_filename: record.originalFilename,
|
|
24
43
|
origin_app_id: record.originAppId,
|
|
25
|
-
parent_id: record.parentId
|
|
26
|
-
label: record.label
|
|
44
|
+
parent_id: record.parentId
|
|
27
45
|
};
|
|
28
46
|
}
|
|
29
47
|
function rowToRecord(row) {
|
|
@@ -41,8 +59,7 @@ function rowToRecord(row) {
|
|
|
41
59
|
sizeBytes: row.size_bytes,
|
|
42
60
|
originalFilename: row.original_filename,
|
|
43
61
|
originAppId: row.origin_app_id,
|
|
44
|
-
parentId: row.parent_id ? createStarkeepId(row.parent_id) : null
|
|
45
|
-
label: row.label
|
|
62
|
+
parentId: row.parent_id ? createStarkeepId(row.parent_id) : null
|
|
46
63
|
};
|
|
47
64
|
}
|
|
48
65
|
|
|
@@ -67,8 +84,7 @@ var FIELD_MAP = {
|
|
|
67
84
|
sizeBytes: "size_bytes",
|
|
68
85
|
originAppId: "origin_app_id",
|
|
69
86
|
parentId: "parent_id",
|
|
70
|
-
originalFilename: "original_filename"
|
|
71
|
-
label: "label"
|
|
87
|
+
originalFilename: "original_filename"
|
|
72
88
|
};
|
|
73
89
|
function mapField(field) {
|
|
74
90
|
return FIELD_MAP[field] ?? field;
|
|
@@ -136,93 +152,99 @@ function applyFilter(qb, filter) {
|
|
|
136
152
|
}
|
|
137
153
|
|
|
138
154
|
// src/schema/bootstrap.ts
|
|
155
|
+
import { sql } from "kysely";
|
|
139
156
|
import { CATEGORIES, sqliteMetadataDdl } from "@starkeep/protocol-primitives";
|
|
140
|
-
function
|
|
157
|
+
function applyLocalSchemaDdl(db) {
|
|
141
158
|
db.exec("PRAGMA journal_mode = WAL");
|
|
142
159
|
db.exec("PRAGMA foreign_keys = ON");
|
|
143
|
-
db.exec(`
|
|
144
|
-
CREATE TABLE IF NOT EXISTS shared_records (
|
|
145
|
-
id TEXT PRIMARY KEY,
|
|
146
|
-
type TEXT NOT NULL,
|
|
147
|
-
created_at TEXT NOT NULL,
|
|
148
|
-
updated_at TEXT NOT NULL,
|
|
149
|
-
-- Denormalized from updated_at (its nodeId component) on every write.
|
|
150
|
-
-- Feeds the sync responder's per-node coverage watermark via the
|
|
151
|
-
-- (node_id, updated_at) index without scanning the table.
|
|
152
|
-
node_id TEXT NOT NULL,
|
|
153
|
-
deleted_at TEXT,
|
|
154
|
-
version INTEGER NOT NULL DEFAULT 1,
|
|
155
|
-
content_hash TEXT NOT NULL,
|
|
156
|
-
object_storage_key TEXT NOT NULL,
|
|
157
|
-
mime_type TEXT,
|
|
158
|
-
size_bytes INTEGER NOT NULL,
|
|
159
|
-
original_filename TEXT,
|
|
160
|
-
origin_app_id TEXT NOT NULL,
|
|
161
|
-
parent_id TEXT,
|
|
162
|
-
-- Advisory appId/purpose interest-filter marker (e.g. photos/thumbnail);
|
|
163
|
-
-- NULL = general interest. See DataRecord.label.
|
|
164
|
-
label TEXT
|
|
165
|
-
)
|
|
166
|
-
`);
|
|
167
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_type ON shared_records(type)");
|
|
168
160
|
db.exec(
|
|
169
|
-
"
|
|
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
|
|
170
209
|
);
|
|
171
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_origin_app ON shared_records(origin_app_id)");
|
|
172
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_records_parent_id ON shared_records(parent_id)");
|
|
173
210
|
db.exec(
|
|
174
|
-
"
|
|
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
|
|
175
227
|
);
|
|
176
|
-
db.exec(`
|
|
177
|
-
CREATE TABLE IF NOT EXISTS shared_access_grants (
|
|
178
|
-
app_id TEXT NOT NULL,
|
|
179
|
-
type_id TEXT NOT NULL,
|
|
180
|
-
access TEXT NOT NULL CHECK (access IN ('read', 'readwrite')),
|
|
181
|
-
metadata_write INTEGER NOT NULL DEFAULT 0,
|
|
182
|
-
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
183
|
-
PRIMARY KEY (app_id, type_id)
|
|
184
|
-
)
|
|
185
|
-
`);
|
|
186
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_shared_access_grants_app ON shared_access_grants(app_id)");
|
|
187
|
-
db.exec(`
|
|
188
|
-
CREATE TABLE IF NOT EXISTS shared_app_registry (
|
|
189
|
-
app_id TEXT PRIMARY KEY,
|
|
190
|
-
name TEXT NOT NULL,
|
|
191
|
-
version TEXT NOT NULL,
|
|
192
|
-
tier TEXT NOT NULL DEFAULT 'app',
|
|
193
|
-
manifest TEXT NOT NULL,
|
|
194
|
-
status TEXT NOT NULL DEFAULT 'installing',
|
|
195
|
-
hmac_secret TEXT NOT NULL,
|
|
196
|
-
installed_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
197
|
-
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
198
|
-
)
|
|
199
|
-
`);
|
|
200
|
-
db.exec(`
|
|
201
|
-
CREATE TABLE IF NOT EXISTS shared_app_install_steps (
|
|
202
|
-
app_id TEXT NOT NULL,
|
|
203
|
-
operation TEXT NOT NULL CHECK (operation IN ('install', 'uninstall')),
|
|
204
|
-
step TEXT NOT NULL,
|
|
205
|
-
status TEXT NOT NULL CHECK (status IN ('pending', 'done', 'failed')),
|
|
206
|
-
error TEXT,
|
|
207
|
-
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
208
|
-
PRIMARY KEY (app_id, operation, step)
|
|
209
|
-
)
|
|
210
|
-
`);
|
|
211
|
-
db.exec(`
|
|
212
|
-
CREATE TABLE IF NOT EXISTS app_syncable_namespaces (
|
|
213
|
-
app_id TEXT PRIMARY KEY,
|
|
214
|
-
tables_json TEXT NOT NULL,
|
|
215
|
-
files_enabled INTEGER NOT NULL DEFAULT 0,
|
|
216
|
-
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
217
|
-
)
|
|
218
|
-
`);
|
|
219
228
|
for (const c of CATEGORIES) {
|
|
220
229
|
if (c.id === "other") continue;
|
|
221
230
|
db.exec(sqliteMetadataDdl(c));
|
|
222
231
|
}
|
|
223
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
|
+
}
|
|
224
243
|
|
|
225
244
|
// src/adapter.ts
|
|
245
|
+
var LABELS = {
|
|
246
|
+
table: "shared_record_labels"
|
|
247
|
+
};
|
|
226
248
|
var SqliteDatabaseAdapter = class {
|
|
227
249
|
database = null;
|
|
228
250
|
options;
|
|
@@ -247,7 +269,7 @@ var SqliteDatabaseAdapter = class {
|
|
|
247
269
|
async healthCheck() {
|
|
248
270
|
if (!this.database) return false;
|
|
249
271
|
try {
|
|
250
|
-
this.database.prepare(
|
|
272
|
+
this.database.prepare(kSql`SELECT 1`.compile(compiler).sql).get();
|
|
251
273
|
return true;
|
|
252
274
|
} catch {
|
|
253
275
|
return false;
|
|
@@ -265,45 +287,44 @@ var SqliteDatabaseAdapter = class {
|
|
|
265
287
|
getRawDatabase() {
|
|
266
288
|
return this.getDatabase();
|
|
267
289
|
}
|
|
268
|
-
runStmt(
|
|
269
|
-
this.getDatabase().prepare(
|
|
290
|
+
runStmt(sql3, ...params) {
|
|
291
|
+
this.getDatabase().prepare(sql3).run(...params);
|
|
270
292
|
}
|
|
271
|
-
getRow(
|
|
272
|
-
return this.getDatabase().prepare(
|
|
293
|
+
getRow(sql3, ...params) {
|
|
294
|
+
return this.getDatabase().prepare(sql3).get(
|
|
273
295
|
...params
|
|
274
296
|
);
|
|
275
297
|
}
|
|
276
|
-
allRows(
|
|
277
|
-
return this.getDatabase().prepare(
|
|
298
|
+
allRows(sql3, ...params) {
|
|
299
|
+
return this.getDatabase().prepare(sql3).all(
|
|
278
300
|
...params
|
|
279
301
|
);
|
|
280
302
|
}
|
|
281
303
|
async put(record) {
|
|
282
304
|
const row = recordToRow(record);
|
|
283
|
-
const
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
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);
|
|
288
314
|
}
|
|
289
315
|
async get(id) {
|
|
290
|
-
const
|
|
316
|
+
const query = compiler.selectFrom("shared_records").selectAll().where("id", "=", id).compile();
|
|
317
|
+
const row = this.getRow(query.sql, ...query.parameters);
|
|
291
318
|
return row ? rowToRecord(row) : null;
|
|
292
319
|
}
|
|
293
320
|
async delete(id, hlc) {
|
|
294
321
|
const ts = serializeHLC2(hlc);
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
ts,
|
|
298
|
-
ts,
|
|
299
|
-
hlc.nodeId,
|
|
300
|
-
id
|
|
301
|
-
);
|
|
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);
|
|
302
324
|
}
|
|
303
325
|
async getNodeWatermarks() {
|
|
304
|
-
const
|
|
305
|
-
|
|
306
|
-
);
|
|
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);
|
|
307
328
|
const out = {};
|
|
308
329
|
for (const row of rows) {
|
|
309
330
|
out[row.node_id] = deserializeHLC2(row.max_updated_at);
|
|
@@ -311,8 +332,8 @@ var SqliteDatabaseAdapter = class {
|
|
|
311
332
|
return out;
|
|
312
333
|
}
|
|
313
334
|
async query(query) {
|
|
314
|
-
const { sql, params } = buildSelectQuery(query);
|
|
315
|
-
const rows = this.allRows(
|
|
335
|
+
const { sql: sql3, params } = buildSelectQuery(query);
|
|
336
|
+
const rows = this.allRows(sql3, ...params);
|
|
316
337
|
const limit = query.limit;
|
|
317
338
|
const hasMore = limit ? rows.length > limit : false;
|
|
318
339
|
const resultRows = hasMore ? rows.slice(0, limit) : rows;
|
|
@@ -358,24 +379,23 @@ var SqliteDatabaseAdapter = class {
|
|
|
358
379
|
}
|
|
359
380
|
async putMetadata(typeId, row) {
|
|
360
381
|
const table = sqliteMetadataTableName(typeId);
|
|
361
|
-
const
|
|
362
|
-
const values = [row.recordId];
|
|
382
|
+
const values = { record_id: row.recordId };
|
|
363
383
|
for (const [key, value] of Object.entries(row)) {
|
|
364
384
|
if (key === "recordId") continue;
|
|
365
|
-
|
|
366
|
-
values.push(value);
|
|
385
|
+
values[key] = value;
|
|
367
386
|
}
|
|
368
|
-
const
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
|
|
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);
|
|
372
394
|
}
|
|
373
395
|
async getMetadata(typeId, recordId) {
|
|
374
396
|
const table = sqliteMetadataTableName(typeId);
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
recordId
|
|
378
|
-
);
|
|
397
|
+
const query = compiler.selectFrom(table).selectAll().where("record_id", "=", recordId).compile();
|
|
398
|
+
const row = this.getRow(query.sql, ...query.parameters);
|
|
379
399
|
if (!row) return null;
|
|
380
400
|
return columnsToMetadataRow(recordId, row);
|
|
381
401
|
}
|
|
@@ -383,11 +403,8 @@ var SqliteDatabaseAdapter = class {
|
|
|
383
403
|
const result = /* @__PURE__ */ new Map();
|
|
384
404
|
if (recordIds.length === 0) return result;
|
|
385
405
|
const table = sqliteMetadataTableName(typeId);
|
|
386
|
-
const
|
|
387
|
-
const rows = this.allRows(
|
|
388
|
-
`SELECT * FROM ${table} WHERE record_id IN (${placeholders})`,
|
|
389
|
-
...recordIds
|
|
390
|
-
);
|
|
406
|
+
const query = compiler.selectFrom(table).selectAll().where("record_id", "in", recordIds).compile();
|
|
407
|
+
const rows = this.allRows(query.sql, ...query.parameters);
|
|
391
408
|
for (const row of rows) {
|
|
392
409
|
const recordId = row["record_id"];
|
|
393
410
|
result.set(recordId, columnsToMetadataRow(recordId, row));
|
|
@@ -396,7 +413,93 @@ var SqliteDatabaseAdapter = class {
|
|
|
396
413
|
}
|
|
397
414
|
async deleteMetadata(typeId, recordId) {
|
|
398
415
|
const table = sqliteMetadataTableName(typeId);
|
|
399
|
-
|
|
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);
|
|
400
503
|
}
|
|
401
504
|
};
|
|
402
505
|
function columnsToMetadataRow(recordId, columns) {
|
|
@@ -425,34 +528,38 @@ function rowToNamespace(r) {
|
|
|
425
528
|
};
|
|
426
529
|
}
|
|
427
530
|
function upsertAppSyncableNamespace(db, appId, tables, filesEnabled) {
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
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);
|
|
435
542
|
}
|
|
436
543
|
function deleteAppSyncableNamespace(db, appId) {
|
|
437
|
-
|
|
544
|
+
const query = compiler.deleteFrom("app_syncable_namespaces").where("app_id", "=", appId).compile();
|
|
545
|
+
db.prepare(query.sql).run(...query.parameters);
|
|
438
546
|
}
|
|
439
547
|
function getAppSyncableNamespace(db, appId) {
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
).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);
|
|
443
550
|
if (!row) return null;
|
|
444
551
|
return rowToNamespace(row);
|
|
445
552
|
}
|
|
446
553
|
function listAppSyncableNamespaces(db) {
|
|
447
|
-
const
|
|
448
|
-
|
|
449
|
-
).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();
|
|
450
556
|
return rows.map(rowToNamespace);
|
|
451
557
|
}
|
|
452
558
|
var SqliteAppSyncableNamespaceStore = class {
|
|
453
559
|
constructor(db) {
|
|
454
560
|
this.db = db;
|
|
455
561
|
}
|
|
562
|
+
db;
|
|
456
563
|
get(appId) {
|
|
457
564
|
return getAppSyncableNamespace(this.db, appId);
|
|
458
565
|
}
|
|
@@ -462,12 +569,21 @@ var SqliteAppSyncableNamespaceStore = class {
|
|
|
462
569
|
};
|
|
463
570
|
|
|
464
571
|
// src/app-syncable/apply.ts
|
|
572
|
+
import { sql as sql2 } from "kysely";
|
|
465
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
|
+
}
|
|
466
580
|
var SqliteAppSyncableApplier = class {
|
|
467
581
|
constructor(db, namespace) {
|
|
468
582
|
this.db = db;
|
|
469
583
|
this.namespace = namespace;
|
|
470
584
|
}
|
|
585
|
+
db;
|
|
586
|
+
namespace;
|
|
471
587
|
apply(entry) {
|
|
472
588
|
const ns = this.namespace.get(entry.appId);
|
|
473
589
|
if (!ns) {
|
|
@@ -495,27 +611,22 @@ var SqliteAppSyncableApplier = class {
|
|
|
495
611
|
const row = withNodeId(entry.row ?? {}, entry);
|
|
496
612
|
const cols = Object.keys(row);
|
|
497
613
|
if (cols.length === 0) return;
|
|
498
|
-
const colList = cols.map(q).join(", ");
|
|
499
|
-
const placeholders = cols.map(() => "?").join(", ");
|
|
500
|
-
const values = cols.map((c) => row[c]);
|
|
501
|
-
if (pkColumns.length === 0) {
|
|
502
|
-
this.db.prepare(`INSERT OR IGNORE INTO ${q(fullName)} (${colList}) VALUES (${placeholders})`).run(...values);
|
|
503
|
-
return;
|
|
504
|
-
}
|
|
505
|
-
const conflictTarget = pkColumns.map(q).join(", ");
|
|
506
614
|
const updateCols = cols.filter((c) => !pkColumns.includes(c));
|
|
507
|
-
if (updateCols.length === 0) {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
615
|
+
if (pkColumns.length === 0 || updateCols.length === 0) {
|
|
616
|
+
runCompiled(
|
|
617
|
+
this.db,
|
|
618
|
+
compiler.insertInto(fullName).orIgnore().values({ ...row }).compile()
|
|
619
|
+
);
|
|
511
620
|
return;
|
|
512
621
|
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
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
|
+
);
|
|
519
630
|
}
|
|
520
631
|
applyUpdate(fullName, entry) {
|
|
521
632
|
const rawPatch = entry.row ?? {};
|
|
@@ -524,48 +635,39 @@ var SqliteAppSyncableApplier = class {
|
|
|
524
635
|
const patchCols = Object.keys(patch);
|
|
525
636
|
const whereCols = Object.keys(where);
|
|
526
637
|
if (patchCols.length === 0) return;
|
|
527
|
-
const setClause = patchCols.map((c) => `${q(c)} = ?`).join(", ");
|
|
528
638
|
const incomingUpdatedAt = patch["updated_at"];
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
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());
|
|
538
647
|
}
|
|
539
648
|
applyDelete(fullName, entry) {
|
|
540
649
|
const where = entry.where ?? {};
|
|
541
650
|
const whereCols = Object.keys(where);
|
|
542
651
|
const incomingUpdatedAt = entry.row?.["updated_at"];
|
|
543
652
|
const ts = incomingUpdatedAt ?? serializeHLC3(entry.timestamp);
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
nodeIdOf(ts, entry),
|
|
553
|
-
...whereCols.map((c) => where[c]),
|
|
554
|
-
ts
|
|
555
|
-
// for the LWW updated_at < ? condition
|
|
556
|
-
];
|
|
557
|
-
this.db.prepare(
|
|
558
|
-
`UPDATE ${q(fullName)} SET deleted_at = ?, updated_at = ?, node_id = ?${whereClause}`
|
|
559
|
-
).run(...params);
|
|
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());
|
|
560
661
|
}
|
|
561
662
|
/** See `ScanCapableApplier.getNodeWatermarks`. Missing table → `{}`. */
|
|
562
663
|
async getNodeWatermarks(appId, table) {
|
|
563
664
|
const fullName = appSyncableTableName(appId, table);
|
|
564
665
|
let rows;
|
|
565
666
|
try {
|
|
566
|
-
rows =
|
|
567
|
-
|
|
568
|
-
|
|
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
|
+
);
|
|
569
671
|
} catch {
|
|
570
672
|
return {};
|
|
571
673
|
}
|
|
@@ -588,15 +690,11 @@ var SqliteAppSyncableApplier = class {
|
|
|
588
690
|
const limit = options?.limit;
|
|
589
691
|
let rows;
|
|
590
692
|
try {
|
|
693
|
+
let query = compiler.selectFrom(fullName).selectAll().where("updated_at", ">", floor).orderBy("updated_at", "asc");
|
|
591
694
|
if (limit !== void 0) {
|
|
592
|
-
|
|
593
|
-
`SELECT * FROM ${q(fullName)} WHERE updated_at > ? ORDER BY updated_at ASC LIMIT ?`
|
|
594
|
-
).all(floor, limit + 1);
|
|
595
|
-
} else {
|
|
596
|
-
rows = this.db.prepare(
|
|
597
|
-
`SELECT * FROM ${q(fullName)} WHERE updated_at > ? ORDER BY updated_at ASC`
|
|
598
|
-
).all(floor);
|
|
695
|
+
query = query.limit(limit + 1);
|
|
599
696
|
}
|
|
697
|
+
rows = allCompiled(this.db, query.compile());
|
|
600
698
|
} catch {
|
|
601
699
|
return { rows: [], nextCursor: null, hasMore: false };
|
|
602
700
|
}
|
|
@@ -610,16 +708,13 @@ var SqliteAppSyncableApplier = class {
|
|
|
610
708
|
queryRows(appId, table, where) {
|
|
611
709
|
const fullName = appSyncableTableName(appId, table);
|
|
612
710
|
const whereCols = where ? Object.keys(where) : [];
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
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());
|
|
618
716
|
}
|
|
619
717
|
};
|
|
620
|
-
function q(name) {
|
|
621
|
-
return `"${name}"`;
|
|
622
|
-
}
|
|
623
718
|
function withNodeId(row, entry) {
|
|
624
719
|
return { ...row, node_id: nodeIdOf(row["updated_at"], entry) };
|
|
625
720
|
}
|
|
@@ -650,6 +745,7 @@ export {
|
|
|
650
745
|
getAppSyncableNamespace,
|
|
651
746
|
initializeLocalSchema,
|
|
652
747
|
listAppSyncableNamespaces,
|
|
748
|
+
compiler as sqliteCompiler,
|
|
653
749
|
upsertAppSyncableNamespace
|
|
654
750
|
};
|
|
655
751
|
//# sourceMappingURL=index.js.map
|