@starkeep/storage-sqlite 0.3.1 → 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/README.md +4 -4
- package/dist/adapter-DoVOV6K-.d.cts +126 -0
- package/dist/adapter-DoVOV6K-.d.ts +126 -0
- package/dist/index.cjs +617 -214
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +172 -71
- package/dist/index.d.ts +172 -71
- package/dist/index.js +645 -193
- package/dist/index.js.map +1 -1
- package/dist/node-entry.cjs +45 -0
- package/dist/node-entry.cjs.map +1 -0
- package/dist/node-entry.d.cts +32 -0
- package/dist/node-entry.d.ts +32 -0
- package/dist/node-entry.js +18 -0
- package/dist/node-entry.js.map +1 -0
- package/package.json +10 -5
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,11 +41,9 @@ __export(index_exports, {
|
|
|
34
41
|
module.exports = __toCommonJS(index_exports);
|
|
35
42
|
|
|
36
43
|
// src/adapter.ts
|
|
37
|
-
var
|
|
38
|
-
var
|
|
39
|
-
var
|
|
40
|
-
var import_protocol_primitives3 = require("@starkeep/protocol-primitives");
|
|
41
|
-
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");
|
|
42
47
|
var import_kysely3 = require("kysely");
|
|
43
48
|
|
|
44
49
|
// src/serialization.ts
|
|
@@ -59,7 +64,12 @@ function recordToRow(record) {
|
|
|
59
64
|
size_bytes: record.sizeBytes,
|
|
60
65
|
original_filename: record.originalFilename,
|
|
61
66
|
origin_app_id: record.originAppId,
|
|
62
|
-
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)
|
|
63
73
|
};
|
|
64
74
|
}
|
|
65
75
|
function rowToRecord(row) {
|
|
@@ -77,30 +87,20 @@ function rowToRecord(row) {
|
|
|
77
87
|
sizeBytes: row.size_bytes,
|
|
78
88
|
originalFilename: row.original_filename,
|
|
79
89
|
originAppId: row.origin_app_id,
|
|
80
|
-
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
|
|
81
97
|
};
|
|
82
98
|
}
|
|
83
99
|
|
|
84
100
|
// src/query-builder.ts
|
|
101
|
+
var import_storage_adapter2 = require("@starkeep/storage-adapter");
|
|
102
|
+
var import_protocol_primitives2 = require("@starkeep/protocol-primitives");
|
|
85
103
|
var import_kysely = require("kysely");
|
|
86
|
-
var FIELD_MAP = {
|
|
87
|
-
id: "id",
|
|
88
|
-
type: "type",
|
|
89
|
-
createdAt: "created_at",
|
|
90
|
-
updatedAt: "updated_at",
|
|
91
|
-
deletedAt: "deleted_at",
|
|
92
|
-
version: "version",
|
|
93
|
-
contentHash: "content_hash",
|
|
94
|
-
objectStorageKey: "object_storage_key",
|
|
95
|
-
mimeType: "mime_type",
|
|
96
|
-
sizeBytes: "size_bytes",
|
|
97
|
-
originAppId: "origin_app_id",
|
|
98
|
-
parentId: "parent_id",
|
|
99
|
-
originalFilename: "original_filename"
|
|
100
|
-
};
|
|
101
|
-
function mapField(field) {
|
|
102
|
-
return FIELD_MAP[field] ?? field;
|
|
103
|
-
}
|
|
104
104
|
var compiler = new import_kysely.Kysely({
|
|
105
105
|
dialect: {
|
|
106
106
|
createAdapter: () => new import_kysely.SqliteAdapter(),
|
|
@@ -109,83 +109,65 @@ var compiler = new import_kysely.Kysely({
|
|
|
109
109
|
createQueryCompiler: () => new import_kysely.SqliteQueryCompiler()
|
|
110
110
|
}
|
|
111
111
|
});
|
|
112
|
+
var DIALECT = {
|
|
113
|
+
records: "shared_records",
|
|
114
|
+
labels: "shared_record_labels",
|
|
115
|
+
metadataTable: import_protocol_primitives2.sqliteMetadataTableName
|
|
116
|
+
};
|
|
112
117
|
function buildSelectQuery(query) {
|
|
113
|
-
|
|
114
|
-
if (query.type) {
|
|
115
|
-
qb = qb.where("type", "=", query.type);
|
|
116
|
-
}
|
|
117
|
-
if (query.filters) {
|
|
118
|
-
for (const filter of query.filters) {
|
|
119
|
-
qb = applyFilter(qb, filter);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
if (query.cursor) {
|
|
123
|
-
qb = qb.where("id", ">", query.cursor);
|
|
124
|
-
}
|
|
125
|
-
if (query.sort && query.sort.length > 0) {
|
|
126
|
-
for (const s of query.sort) {
|
|
127
|
-
qb = qb.orderBy(mapField(s.field), s.direction === "desc" ? "desc" : "asc");
|
|
128
|
-
}
|
|
129
|
-
} else {
|
|
130
|
-
qb = qb.orderBy("id", "asc");
|
|
131
|
-
}
|
|
132
|
-
if (query.limit) {
|
|
133
|
-
qb = qb.limit(query.limit + 1);
|
|
134
|
-
}
|
|
135
|
-
const compiled = qb.compile();
|
|
118
|
+
const compiled = (0, import_storage_adapter2.buildRecordSelect)(compiler, DIALECT, query);
|
|
136
119
|
return { sql: compiled.sql, params: [...compiled.parameters] };
|
|
137
120
|
}
|
|
138
|
-
function
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
case "gt":
|
|
146
|
-
return qb.where(col, ">", filter.value);
|
|
147
|
-
case "gte":
|
|
148
|
-
return qb.where(col, ">=", filter.value);
|
|
149
|
-
case "lt":
|
|
150
|
-
return qb.where(col, "<", filter.value);
|
|
151
|
-
case "lte":
|
|
152
|
-
return qb.where(col, "<=", filter.value);
|
|
153
|
-
case "in":
|
|
154
|
-
return qb.where(col, "in", filter.value);
|
|
155
|
-
case "like":
|
|
156
|
-
return qb.where(col, "like", `%${filter.value}%`);
|
|
157
|
-
case "isNull":
|
|
158
|
-
return qb.where((eb) => eb(col, "is", null));
|
|
159
|
-
case "isNotNull":
|
|
160
|
-
return qb.where((eb) => eb(col, "is not", null));
|
|
161
|
-
default:
|
|
162
|
-
return qb;
|
|
163
|
-
}
|
|
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] };
|
|
164
128
|
}
|
|
165
129
|
|
|
166
130
|
// src/schema/bootstrap.ts
|
|
167
131
|
var import_kysely2 = require("kysely");
|
|
168
|
-
var
|
|
132
|
+
var import_protocol_primitives3 = require("@starkeep/protocol-primitives");
|
|
133
|
+
function applyConnectionPragmas(db) {
|
|
134
|
+
db.exec("PRAGMA case_sensitive_like = ON");
|
|
135
|
+
}
|
|
169
136
|
function applyLocalSchemaDdl(db) {
|
|
170
137
|
db.exec("PRAGMA journal_mode = WAL");
|
|
171
138
|
db.exec("PRAGMA foreign_keys = ON");
|
|
139
|
+
applyConnectionPragmas(db);
|
|
172
140
|
db.exec(
|
|
173
|
-
compiler.schema.createTable("shared_records").ifNotExists().addColumn("id", "text", (c) => c.primaryKey()).addColumn("type", "text", (c) => c.notNull()).addColumn("created_at", "text", (c) => c.notNull()).addColumn("updated_at", "text", (c) => c.notNull()).addColumn("node_id", "text", (c) => c.notNull()).addColumn("deleted_at", "text").addColumn("version", "integer", (c) => c.notNull().defaultTo(1)).addColumn("content_hash", "text", (c) => c.notNull()).addColumn("object_storage_key", "text", (c) => c.notNull()).addColumn("mime_type", "text").addColumn("size_bytes", "integer", (c) => c.notNull()).addColumn("original_filename", "text").addColumn("origin_app_id", "text", (c) => c.notNull()).addColumn("parent_id", "text").
|
|
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
|
|
174
146
|
);
|
|
175
147
|
const sharedRecordsIndexes = [
|
|
176
148
|
compiler.schema.createIndex("idx_shared_records_type").ifNotExists().on("shared_records").column("type"),
|
|
177
149
|
compiler.schema.createIndex("idx_shared_records_node_watermark").ifNotExists().on("shared_records").columns(["node_id", "updated_at"]),
|
|
178
150
|
compiler.schema.createIndex("idx_shared_records_origin_app").ifNotExists().on("shared_records").column("origin_app_id"),
|
|
179
151
|
compiler.schema.createIndex("idx_shared_records_parent_id").ifNotExists().on("shared_records").column("parent_id"),
|
|
180
|
-
//
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
//
|
|
184
|
-
|
|
152
|
+
// Record-level dedup: "is there already a live record for these bytes
|
|
153
|
+
// under this parent?", asked on **every** registration.
|
|
154
|
+
//
|
|
155
|
+
// The (filename, content_hash) unique index below cannot serve it — its
|
|
156
|
+
// leading column is the filename, so a hash-only lookup would scan the
|
|
157
|
+
// table once per write. Leading with content_hash is what makes the dedup
|
|
158
|
+
// check affordable enough to be unconditional, and unconditional is what
|
|
159
|
+
// makes "one object key, one record" an invariant rather than a hope.
|
|
160
|
+
compiler.schema.createIndex("idx_shared_records_content_hash").ifNotExists().on("shared_records").columns(["content_hash", "parent_id", "deleted_at"])
|
|
185
161
|
];
|
|
186
162
|
for (const index of sharedRecordsIndexes) {
|
|
187
163
|
db.exec(index.compile().sql);
|
|
188
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
|
+
);
|
|
189
171
|
db.exec(
|
|
190
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
|
|
191
173
|
);
|
|
@@ -213,6 +195,9 @@ function applyLocalSchemaDdl(db) {
|
|
|
213
195
|
for (const index of sharedRecordLabelsIndexes) {
|
|
214
196
|
db.exec(index.compile().sql);
|
|
215
197
|
}
|
|
198
|
+
db.exec(
|
|
199
|
+
compiler.schema.createTable("shared_object_availability").ifNotExists().addColumn("object_storage_key", "text", (c) => c.primaryKey()).addColumn("state", "text", (c) => c.notNull()).addColumn("tier", "text").addColumn("expected_latency_hours", "integer").addColumn("ready_at_ms", "integer").addColumn("restored_until_ms", "integer").addColumn("observed_at_ms", "integer", (c) => c.notNull()).compile().sql
|
|
200
|
+
);
|
|
216
201
|
db.exec(
|
|
217
202
|
compiler.schema.createTable("shared_app_label_keys").ifNotExists().addColumn("app_id", "text", (c) => c.notNull()).addColumn("key", "text", (c) => c.notNull()).addColumn("description", "text").addColumn("created_at", "text", (c) => c.notNull().defaultTo(import_kysely2.sql`(datetime('now'))`)).addPrimaryKeyConstraint("pk_shared_app_label_keys", ["app_id", "key"]).compile().sql
|
|
218
203
|
);
|
|
@@ -237,9 +222,10 @@ function applyLocalSchemaDdl(db) {
|
|
|
237
222
|
db.exec(
|
|
238
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
|
|
239
224
|
);
|
|
240
|
-
for (const c of
|
|
241
|
-
if (c.id
|
|
242
|
-
db.exec((0,
|
|
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);
|
|
243
229
|
}
|
|
244
230
|
}
|
|
245
231
|
function initializeLocalSchema(db) {
|
|
@@ -253,29 +239,60 @@ function initializeLocalSchema(db) {
|
|
|
253
239
|
}
|
|
254
240
|
}
|
|
255
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
|
+
|
|
256
264
|
// src/adapter.ts
|
|
265
|
+
var AVAILABILITY = "shared_object_availability";
|
|
266
|
+
function rowToAvailability(row) {
|
|
267
|
+
return {
|
|
268
|
+
objectStorageKey: row.object_storage_key,
|
|
269
|
+
state: row.state,
|
|
270
|
+
tier: row.tier,
|
|
271
|
+
expectedLatencyHours: row.expected_latency_hours,
|
|
272
|
+
readyAtMs: row.ready_at_ms,
|
|
273
|
+
restoredUntilMs: row.restored_until_ms,
|
|
274
|
+
observedAtMs: row.observed_at_ms
|
|
275
|
+
};
|
|
276
|
+
}
|
|
257
277
|
var LABELS = {
|
|
258
278
|
table: "shared_record_labels"
|
|
259
279
|
};
|
|
280
|
+
var RECORDS = "shared_records";
|
|
260
281
|
var SqliteDatabaseAdapter = class {
|
|
261
282
|
database = null;
|
|
262
283
|
options;
|
|
284
|
+
driver;
|
|
263
285
|
constructor(options) {
|
|
264
286
|
this.options = options;
|
|
287
|
+
this.driver = options.driver;
|
|
265
288
|
}
|
|
266
289
|
async init() {
|
|
267
290
|
if (this.database) return;
|
|
268
|
-
|
|
269
|
-
const dir = (0, import_node_path.dirname)(this.options.path);
|
|
270
|
-
if (!(0, import_node_fs.existsSync)(dir)) {
|
|
271
|
-
(0, import_node_fs.mkdirSync)(dir, { recursive: true });
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
this.database = new import_node_sqlite.DatabaseSync(this.options.path);
|
|
291
|
+
this.database = this.driver.open(this.options.path);
|
|
275
292
|
initializeLocalSchema(this.database);
|
|
276
293
|
}
|
|
277
294
|
async close() {
|
|
278
|
-
this.database
|
|
295
|
+
if (this.database) this.driver.close(this.database);
|
|
279
296
|
this.database = null;
|
|
280
297
|
}
|
|
281
298
|
async healthCheck() {
|
|
@@ -288,29 +305,43 @@ var SqliteDatabaseAdapter = class {
|
|
|
288
305
|
}
|
|
289
306
|
}
|
|
290
307
|
getDatabase() {
|
|
291
|
-
if (!this.database) throw new
|
|
308
|
+
if (!this.database) throw new import_storage_adapter4.StorageError("Database not initialized. Call init() first.");
|
|
292
309
|
return this.database;
|
|
293
310
|
}
|
|
294
311
|
/**
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
* database file.
|
|
312
|
+
* The raw SQLite connection, so sibling subsystems (the sync engine's change
|
|
313
|
+
* log and state store, the resident set) can create side tables in the same
|
|
314
|
+
* database file. Only valid after `init()`.
|
|
315
|
+
*
|
|
316
|
+
* Typed as {@link RawDatabase} rather than `DatabaseSync`: returning the
|
|
317
|
+
* concrete Node type is what made a second driver impossible, since every
|
|
318
|
+
* consumer was then nailed to `node:sqlite`, which React Native does not
|
|
319
|
+
* have. `DatabaseSync` satisfies the narrower interface structurally, so
|
|
320
|
+
* nothing about this connection changes — only what callers may reach for.
|
|
298
321
|
*/
|
|
299
322
|
getRawDatabase() {
|
|
300
323
|
return this.getDatabase();
|
|
301
324
|
}
|
|
302
|
-
|
|
303
|
-
|
|
325
|
+
// The casts these three used to carry named `DatabaseSync["prepare"]` to
|
|
326
|
+
// borrow node:sqlite's parameter types. `RawStatement` declares `unknown[]`
|
|
327
|
+
// directly, so they were already no-ops — and they were the last thing in
|
|
328
|
+
// this file referring to a concrete driver.
|
|
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));
|
|
304
339
|
}
|
|
305
|
-
getRow(
|
|
306
|
-
return this.getDatabase().prepare(
|
|
307
|
-
...params
|
|
308
|
-
);
|
|
340
|
+
getRow(sql4, ...params) {
|
|
341
|
+
return this.getDatabase().prepare(sql4).get(...params.map(toSqliteParam));
|
|
309
342
|
}
|
|
310
|
-
allRows(
|
|
311
|
-
return this.getDatabase().prepare(
|
|
312
|
-
...params
|
|
313
|
-
);
|
|
343
|
+
allRows(sql4, ...params) {
|
|
344
|
+
return this.getDatabase().prepare(sql4).all(...params.map(toSqliteParam));
|
|
314
345
|
}
|
|
315
346
|
async put(record) {
|
|
316
347
|
const row = recordToRow(record);
|
|
@@ -330,8 +361,8 @@ var SqliteDatabaseAdapter = class {
|
|
|
330
361
|
return row ? rowToRecord(row) : null;
|
|
331
362
|
}
|
|
332
363
|
async delete(id, hlc) {
|
|
333
|
-
const ts = (0,
|
|
334
|
-
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();
|
|
335
366
|
this.runStmt(query.sql, ...query.parameters);
|
|
336
367
|
}
|
|
337
368
|
async getNodeWatermarks() {
|
|
@@ -339,22 +370,104 @@ var SqliteDatabaseAdapter = class {
|
|
|
339
370
|
const rows = this.allRows(watermarksQuery.sql);
|
|
340
371
|
const out = {};
|
|
341
372
|
for (const row of rows) {
|
|
342
|
-
out[row.node_id] = (0,
|
|
373
|
+
out[row.node_id] = (0, import_protocol_primitives4.deserializeHLC)(row.max_updated_at);
|
|
343
374
|
}
|
|
344
375
|
return out;
|
|
345
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
|
+
}
|
|
346
415
|
async query(query) {
|
|
347
|
-
const { sql:
|
|
348
|
-
const rows = this.allRows(
|
|
416
|
+
const { sql: sql4, params } = buildSelectQuery(query);
|
|
417
|
+
const rows = this.allRows(sql4, ...params);
|
|
349
418
|
const limit = query.limit;
|
|
350
419
|
const hasMore = limit ? rows.length > limit : false;
|
|
351
420
|
const resultRows = hasMore ? rows.slice(0, limit) : rows;
|
|
352
421
|
return {
|
|
353
422
|
records: resultRows.map(rowToRecord),
|
|
354
|
-
|
|
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,
|
|
355
427
|
hasMore
|
|
356
428
|
};
|
|
357
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
|
+
}
|
|
358
471
|
async batch(operations) {
|
|
359
472
|
this.getDatabase().exec("BEGIN");
|
|
360
473
|
try {
|
|
@@ -386,16 +499,12 @@ var SqliteDatabaseAdapter = class {
|
|
|
386
499
|
} catch (error) {
|
|
387
500
|
this.getDatabase().exec("ROLLBACK TO SAVEPOINT starkeep_tx");
|
|
388
501
|
this.getDatabase().exec("RELEASE SAVEPOINT starkeep_tx");
|
|
389
|
-
throw new
|
|
502
|
+
throw new import_storage_adapter4.TransactionError("Transaction failed", error);
|
|
390
503
|
}
|
|
391
504
|
}
|
|
392
|
-
async putMetadata(
|
|
393
|
-
const table = (0,
|
|
394
|
-
const values =
|
|
395
|
-
for (const [key, value] of Object.entries(row)) {
|
|
396
|
-
if (key === "recordId") continue;
|
|
397
|
-
values[key] = value;
|
|
398
|
-
}
|
|
505
|
+
async putMetadata(recordType, row) {
|
|
506
|
+
const table = (0, import_protocol_primitives4.sqliteMetadataTableName)(recordType);
|
|
507
|
+
const values = metadataValues(recordType, row);
|
|
399
508
|
const updateColumns = Object.keys(values).filter((c) => c !== "record_id");
|
|
400
509
|
const query = compiler.insertInto(table).values(values).onConflict(
|
|
401
510
|
(oc) => updateColumns.length > 0 ? oc.column("record_id").doUpdateSet(
|
|
@@ -405,29 +514,40 @@ var SqliteDatabaseAdapter = class {
|
|
|
405
514
|
this.runStmt(query.sql, ...query.parameters);
|
|
406
515
|
}
|
|
407
516
|
async getMetadata(typeId, recordId) {
|
|
408
|
-
const table = (0,
|
|
517
|
+
const table = (0, import_protocol_primitives4.sqliteMetadataTableName)(typeId);
|
|
409
518
|
const query = compiler.selectFrom(table).selectAll().where("record_id", "=", recordId).compile();
|
|
410
519
|
const row = this.getRow(query.sql, ...query.parameters);
|
|
411
520
|
if (!row) return null;
|
|
412
|
-
return columnsToMetadataRow(recordId, row);
|
|
521
|
+
return columnsToMetadataRow(recordId, typeId, row);
|
|
413
522
|
}
|
|
414
523
|
async getMetadataByIds(typeId, recordIds) {
|
|
415
524
|
const result = /* @__PURE__ */ new Map();
|
|
416
525
|
if (recordIds.length === 0) return result;
|
|
417
|
-
const table = (0,
|
|
526
|
+
const table = (0, import_protocol_primitives4.sqliteMetadataTableName)(typeId);
|
|
418
527
|
const query = compiler.selectFrom(table).selectAll().where("record_id", "in", recordIds).compile();
|
|
419
528
|
const rows = this.allRows(query.sql, ...query.parameters);
|
|
420
529
|
for (const row of rows) {
|
|
421
530
|
const recordId = row["record_id"];
|
|
422
|
-
result.set(recordId, columnsToMetadataRow(recordId, row));
|
|
531
|
+
result.set(recordId, columnsToMetadataRow(recordId, typeId, row));
|
|
423
532
|
}
|
|
424
533
|
return result;
|
|
425
534
|
}
|
|
426
535
|
async deleteMetadata(typeId, recordId) {
|
|
427
|
-
const table = (0,
|
|
536
|
+
const table = (0, import_protocol_primitives4.sqliteMetadataTableName)(typeId);
|
|
428
537
|
const query = compiler.deleteFrom(table).where("record_id", "=", recordId).compile();
|
|
429
538
|
this.runStmt(query.sql, ...query.parameters);
|
|
430
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
|
+
}
|
|
431
551
|
// ---- Cross-app record labels -------------------------------------------
|
|
432
552
|
//
|
|
433
553
|
// The SQL for all of these is built in `@starkeep/storage-adapter`, shared
|
|
@@ -436,12 +556,12 @@ var SqliteDatabaseAdapter = class {
|
|
|
436
556
|
// and it lives in LABELS above.
|
|
437
557
|
async upsertLabels(labels) {
|
|
438
558
|
if (labels.length === 0) return;
|
|
439
|
-
const query = (0,
|
|
559
|
+
const query = (0, import_storage_adapter4.buildLabelUpsert)(compiler, LABELS, labels);
|
|
440
560
|
this.runStmt(query.sql, ...query.parameters);
|
|
441
561
|
}
|
|
442
562
|
async retractLabels(retractions) {
|
|
443
563
|
for (const r of retractions) {
|
|
444
|
-
const query = (0,
|
|
564
|
+
const query = (0, import_storage_adapter4.buildLabelRetraction)(compiler, LABELS, r);
|
|
445
565
|
this.runStmt(query.sql, ...query.parameters);
|
|
446
566
|
}
|
|
447
567
|
}
|
|
@@ -450,10 +570,10 @@ var SqliteDatabaseAdapter = class {
|
|
|
450
570
|
this.getDatabase().exec("BEGIN");
|
|
451
571
|
try {
|
|
452
572
|
for (const r of replacements) {
|
|
453
|
-
const tombstone = (0,
|
|
573
|
+
const tombstone = (0, import_storage_adapter4.buildLabelValueReplacementTombstone)(compiler, LABELS, r);
|
|
454
574
|
this.runStmt(tombstone.sql, ...tombstone.parameters);
|
|
455
575
|
if (r.values.length > 0) {
|
|
456
|
-
const upsert = (0,
|
|
576
|
+
const upsert = (0, import_storage_adapter4.buildLabelUpsert)(
|
|
457
577
|
compiler,
|
|
458
578
|
LABELS,
|
|
459
579
|
r.values.map((value) => ({
|
|
@@ -476,54 +596,136 @@ var SqliteDatabaseAdapter = class {
|
|
|
476
596
|
}
|
|
477
597
|
async getLabelsByRecordIds(recordIds) {
|
|
478
598
|
if (recordIds.length === 0) return /* @__PURE__ */ new Map();
|
|
479
|
-
const query = (0,
|
|
480
|
-
return (0,
|
|
481
|
-
this.allRows(query.sql, ...query.parameters)
|
|
482
|
-
);
|
|
599
|
+
const query = (0, import_storage_adapter4.buildLabelsByRecordIds)(compiler, LABELS, recordIds);
|
|
600
|
+
return (0, import_storage_adapter4.groupLabelsByRecordId)(this.allRows(query.sql, ...query.parameters));
|
|
483
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
|
+
*/
|
|
484
608
|
async findByLabel(query) {
|
|
485
|
-
const
|
|
486
|
-
if (!
|
|
487
|
-
|
|
488
|
-
|
|
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
|
+
);
|
|
489
616
|
}
|
|
490
617
|
// ---- Label sync ---------------------------------------------------------
|
|
491
618
|
async putLabel(label) {
|
|
492
|
-
const query = (0,
|
|
619
|
+
const query = (0, import_storage_adapter4.buildLabelSnapshotUpsert)(compiler, LABELS, label);
|
|
493
620
|
this.runStmt(query.sql, ...query.parameters);
|
|
494
621
|
}
|
|
495
622
|
async getLabel(recordId, appId, key, value) {
|
|
496
|
-
const query = (0,
|
|
623
|
+
const query = (0, import_storage_adapter4.buildGetLabel)(compiler, LABELS, recordId, appId, key, value);
|
|
497
624
|
const row = this.getRow(query.sql, ...query.parameters);
|
|
498
|
-
return row ? (0,
|
|
625
|
+
return row ? (0, import_storage_adapter4.rowToLabel)(row) : null;
|
|
499
626
|
}
|
|
500
627
|
async queryLabels(query) {
|
|
501
|
-
const compiled = (0,
|
|
628
|
+
const compiled = (0, import_storage_adapter4.buildQueryLabels)(compiler, LABELS, query);
|
|
502
629
|
const rows = this.allRows(compiled.sql, ...compiled.parameters);
|
|
503
|
-
return (0,
|
|
630
|
+
return (0, import_storage_adapter4.paginateLabelScan)(rows, query.limit);
|
|
504
631
|
}
|
|
505
632
|
async getLabelNodeWatermarks() {
|
|
506
|
-
const query = (0,
|
|
633
|
+
const query = (0, import_storage_adapter4.buildLabelNodeWatermarks)(compiler, LABELS);
|
|
507
634
|
const rows = this.allRows(query.sql);
|
|
508
635
|
const out = {};
|
|
509
|
-
for (const row of rows) out[row.node_id] = (0,
|
|
636
|
+
for (const row of rows) out[row.node_id] = (0, import_protocol_primitives4.deserializeHLC)(row.max_updated_at);
|
|
510
637
|
return out;
|
|
511
638
|
}
|
|
639
|
+
// ---- Object availability ------------------------------------------------
|
|
640
|
+
async getAvailability(objectStorageKeys) {
|
|
641
|
+
const out = /* @__PURE__ */ new Map();
|
|
642
|
+
if (objectStorageKeys.length === 0) return out;
|
|
643
|
+
const compiled = compiler.selectFrom(AVAILABILITY).selectAll().where("object_storage_key", "in", objectStorageKeys).compile();
|
|
644
|
+
const rows = this.allRows(compiled.sql, ...compiled.parameters);
|
|
645
|
+
for (const row of rows) out.set(row.object_storage_key, rowToAvailability(row));
|
|
646
|
+
return out;
|
|
647
|
+
}
|
|
648
|
+
async putAvailability(row) {
|
|
649
|
+
const compiled = compiler.insertInto(AVAILABILITY).values({
|
|
650
|
+
object_storage_key: row.objectStorageKey,
|
|
651
|
+
state: row.state,
|
|
652
|
+
tier: row.tier,
|
|
653
|
+
expected_latency_hours: row.expectedLatencyHours,
|
|
654
|
+
ready_at_ms: row.readyAtMs,
|
|
655
|
+
restored_until_ms: row.restoredUntilMs,
|
|
656
|
+
observed_at_ms: row.observedAtMs
|
|
657
|
+
}).onConflict(
|
|
658
|
+
(oc) => oc.column("object_storage_key").doUpdateSet((eb) => ({
|
|
659
|
+
state: eb.ref("excluded.state"),
|
|
660
|
+
tier: eb.ref("excluded.tier"),
|
|
661
|
+
expected_latency_hours: eb.ref("excluded.expected_latency_hours"),
|
|
662
|
+
ready_at_ms: eb.ref("excluded.ready_at_ms"),
|
|
663
|
+
restored_until_ms: eb.ref("excluded.restored_until_ms"),
|
|
664
|
+
observed_at_ms: eb.ref("excluded.observed_at_ms")
|
|
665
|
+
}))
|
|
666
|
+
).compile();
|
|
667
|
+
this.runStmt(compiled.sql, ...compiled.parameters);
|
|
668
|
+
}
|
|
669
|
+
async countRestoringObjects() {
|
|
670
|
+
const compiled = compiler.selectFrom(AVAILABILITY).innerJoin(
|
|
671
|
+
"shared_records",
|
|
672
|
+
"shared_records.object_storage_key",
|
|
673
|
+
`${AVAILABILITY}.object_storage_key`
|
|
674
|
+
).select(({ fn }) => [
|
|
675
|
+
fn.count(`${AVAILABILITY}.object_storage_key`).as("object_count"),
|
|
676
|
+
fn.sum("shared_records.size_bytes").as("bytes")
|
|
677
|
+
]).where(`${AVAILABILITY}.state`, "=", import_kysely3.sql.lit("restoring")).compile();
|
|
678
|
+
const row = this.getRow(
|
|
679
|
+
compiled.sql,
|
|
680
|
+
...compiled.parameters
|
|
681
|
+
);
|
|
682
|
+
return { objectCount: row?.object_count ?? 0, bytes: row?.bytes ?? 0 };
|
|
683
|
+
}
|
|
512
684
|
async tombstoneLabelsForRecord(recordId, hlc) {
|
|
513
|
-
const query = (0,
|
|
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);
|
|
514
690
|
this.runStmt(query.sql, ...query.parameters);
|
|
515
691
|
}
|
|
516
692
|
};
|
|
517
|
-
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);
|
|
518
696
|
const row = { recordId };
|
|
519
697
|
for (const [key, value] of Object.entries(columns)) {
|
|
520
|
-
if (key === "record_id") continue;
|
|
521
|
-
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;
|
|
522
700
|
}
|
|
523
701
|
return row;
|
|
524
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
|
+
}
|
|
525
726
|
|
|
526
727
|
// src/app-syncable/namespace.ts
|
|
728
|
+
var import_shared_space_api = require("@starkeep/shared-space-api");
|
|
527
729
|
function normalizeAppId(appId) {
|
|
528
730
|
return appId.toLowerCase().replace(/-/g, "_");
|
|
529
731
|
}
|
|
@@ -531,7 +733,7 @@ function appSyncableTableName(appId, tableName) {
|
|
|
531
733
|
return `${normalizeAppId(appId)}_syncable_${tableName}`;
|
|
532
734
|
}
|
|
533
735
|
function rowToNamespace(r) {
|
|
534
|
-
const tables =
|
|
736
|
+
const tables = (0, import_shared_space_api.parseAppSyncableTables)(r.app_id, r.tables_json);
|
|
535
737
|
return {
|
|
536
738
|
appId: r.app_id,
|
|
537
739
|
tables,
|
|
@@ -582,12 +784,13 @@ var SqliteAppSyncableNamespaceStore = class {
|
|
|
582
784
|
|
|
583
785
|
// src/app-syncable/apply.ts
|
|
584
786
|
var import_kysely4 = require("kysely");
|
|
585
|
-
var
|
|
787
|
+
var import_protocol_primitives5 = require("@starkeep/protocol-primitives");
|
|
788
|
+
var import_storage_adapter5 = require("@starkeep/storage-adapter");
|
|
586
789
|
function runCompiled(db, compiled) {
|
|
587
|
-
db.prepare(compiled.sql).run(...compiled.parameters);
|
|
790
|
+
db.prepare(compiled.sql).run(...compiled.parameters.map(toSqliteParam));
|
|
588
791
|
}
|
|
589
792
|
function allCompiled(db, compiled) {
|
|
590
|
-
return db.prepare(compiled.sql).all(...compiled.parameters);
|
|
793
|
+
return db.prepare(compiled.sql).all(...compiled.parameters.map(toSqliteParam));
|
|
591
794
|
}
|
|
592
795
|
var SqliteAppSyncableApplier = class {
|
|
593
796
|
constructor(db, namespace) {
|
|
@@ -599,9 +802,7 @@ var SqliteAppSyncableApplier = class {
|
|
|
599
802
|
apply(entry) {
|
|
600
803
|
const ns = this.namespace.get(entry.appId);
|
|
601
804
|
if (!ns) {
|
|
602
|
-
throw new Error(
|
|
603
|
-
`SqliteAppSyncableApplier: app "${entry.appId}" not installed`
|
|
604
|
-
);
|
|
805
|
+
throw new Error(`SqliteAppSyncableApplier: app "${entry.appId}" not installed`);
|
|
605
806
|
}
|
|
606
807
|
const tableInfo = ns.tables.find((t) => t.name === entry.table);
|
|
607
808
|
if (!tableInfo) {
|
|
@@ -614,9 +815,9 @@ var SqliteAppSyncableApplier = class {
|
|
|
614
815
|
if (entry.op === "insert") {
|
|
615
816
|
this.applyInsert(fullName, pkColumns, entry);
|
|
616
817
|
} else if (entry.op === "update") {
|
|
617
|
-
this.applyUpdate(fullName, entry);
|
|
818
|
+
this.applyUpdate(fullName, pkColumns, entry);
|
|
618
819
|
} else {
|
|
619
|
-
this.applyDelete(fullName, entry);
|
|
820
|
+
this.applyDelete(fullName, pkColumns, entry);
|
|
620
821
|
}
|
|
621
822
|
}
|
|
622
823
|
applyInsert(fullName, pkColumns, entry) {
|
|
@@ -640,10 +841,10 @@ var SqliteAppSyncableApplier = class {
|
|
|
640
841
|
).compile()
|
|
641
842
|
);
|
|
642
843
|
}
|
|
643
|
-
applyUpdate(fullName, entry) {
|
|
844
|
+
applyUpdate(fullName, pkColumns, entry) {
|
|
644
845
|
const rawPatch = entry.row ?? {};
|
|
645
846
|
const patch = rawPatch["updated_at"] ? withNodeId(rawPatch, entry) : rawPatch;
|
|
646
|
-
const where = entry
|
|
847
|
+
const where = (0, import_storage_adapter5.requireKeyedWhere)(entry, "update", pkColumns);
|
|
647
848
|
const patchCols = Object.keys(patch);
|
|
648
849
|
const whereCols = Object.keys(where);
|
|
649
850
|
if (patchCols.length === 0) return;
|
|
@@ -653,78 +854,165 @@ var SqliteAppSyncableApplier = class {
|
|
|
653
854
|
query = query.where(c, "=", where[c]);
|
|
654
855
|
}
|
|
655
856
|
if (incomingUpdatedAt) {
|
|
656
|
-
query = query.where(
|
|
857
|
+
query = query.where(
|
|
858
|
+
(eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", incomingUpdatedAt)])
|
|
859
|
+
);
|
|
657
860
|
}
|
|
658
861
|
runCompiled(this.db, query.compile());
|
|
659
862
|
}
|
|
660
|
-
applyDelete(fullName, entry) {
|
|
661
|
-
const where = entry
|
|
863
|
+
applyDelete(fullName, pkColumns, entry) {
|
|
864
|
+
const where = (0, import_storage_adapter5.requireKeyedWhere)(entry, "delete", pkColumns);
|
|
662
865
|
const whereCols = Object.keys(where);
|
|
663
866
|
const incomingUpdatedAt = entry.row?.["updated_at"];
|
|
664
|
-
const ts = incomingUpdatedAt ?? (0,
|
|
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
|
+
}
|
|
665
882
|
let query = compiler.updateTable(fullName).set({ deleted_at: ts, updated_at: ts, node_id: nodeIdOf(ts, entry) });
|
|
666
883
|
for (const c of whereCols) {
|
|
667
884
|
query = query.where(c, "=", where[c]);
|
|
668
885
|
}
|
|
669
|
-
query = query.where(
|
|
670
|
-
(eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)])
|
|
671
|
-
);
|
|
886
|
+
query = query.where((eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)]));
|
|
672
887
|
runCompiled(this.db, query.compile());
|
|
673
888
|
}
|
|
674
|
-
/**
|
|
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
|
+
*/
|
|
675
934
|
async getNodeWatermarks(appId, table) {
|
|
676
935
|
const fullName = appSyncableTableName(appId, table);
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
);
|
|
683
|
-
} catch {
|
|
684
|
-
return {};
|
|
685
|
-
}
|
|
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
|
+
);
|
|
686
941
|
const out = {};
|
|
687
942
|
for (const row of rows) {
|
|
688
|
-
out[row.node_id] = (0,
|
|
943
|
+
out[row.node_id] = (0, import_protocol_primitives5.deserializeHLC)(row.max_updated_at);
|
|
689
944
|
}
|
|
690
945
|
return out;
|
|
691
946
|
}
|
|
692
947
|
/**
|
|
693
|
-
* Pull-side synthesis:
|
|
694
|
-
*
|
|
695
|
-
*
|
|
696
|
-
*
|
|
697
|
-
* 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.
|
|
698
952
|
*/
|
|
699
|
-
async scanSince(appId, table,
|
|
953
|
+
async scanSince(appId, table, peerWatermarks, limit) {
|
|
700
954
|
const fullName = appSyncableTableName(appId, table);
|
|
701
|
-
const
|
|
702
|
-
const
|
|
703
|
-
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 ?? [];
|
|
704
957
|
try {
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
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 };
|
|
712
981
|
}
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
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 = {}) {
|
|
721
992
|
const fullName = appSyncableTableName(appId, table);
|
|
722
|
-
const
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
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));
|
|
726
1003
|
}
|
|
727
|
-
|
|
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));
|
|
728
1016
|
}
|
|
729
1017
|
};
|
|
730
1018
|
function withNodeId(row, entry) {
|
|
@@ -733,30 +1021,145 @@ function withNodeId(row, entry) {
|
|
|
733
1021
|
function nodeIdOf(updatedAt, entry) {
|
|
734
1022
|
if (typeof updatedAt === "string") {
|
|
735
1023
|
try {
|
|
736
|
-
return (0,
|
|
1024
|
+
return (0, import_protocol_primitives5.deserializeHLC)(updatedAt).nodeId;
|
|
737
1025
|
} catch {
|
|
738
1026
|
}
|
|
739
1027
|
}
|
|
740
1028
|
return entry.timestamp.nodeId;
|
|
741
1029
|
}
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
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;
|
|
748
1144
|
}
|
|
749
|
-
return
|
|
1145
|
+
return rewritten;
|
|
750
1146
|
}
|
|
751
1147
|
// Annotate the CommonJS export names for ESM import in node:
|
|
752
1148
|
0 && (module.exports = {
|
|
1149
|
+
LibraryBoundElsewhereError,
|
|
753
1150
|
SqliteAppSyncableApplier,
|
|
754
1151
|
SqliteAppSyncableNamespaceStore,
|
|
755
1152
|
SqliteDatabaseAdapter,
|
|
756
1153
|
appSyncableTableName,
|
|
1154
|
+
applyConnectionPragmas,
|
|
1155
|
+
boundUserId,
|
|
1156
|
+
checkLibraryBindable,
|
|
1157
|
+
commitLibraryBinding,
|
|
757
1158
|
deleteAppSyncableNamespace,
|
|
758
1159
|
getAppSyncableNamespace,
|
|
759
1160
|
initializeLocalSchema,
|
|
1161
|
+
libraryKeyColumns,
|
|
1162
|
+
libraryUserSegment,
|
|
760
1163
|
listAppSyncableNamespaces,
|
|
761
1164
|
sqliteCompiler,
|
|
762
1165
|
upsertAppSyncableNamespace
|