@starkeep/storage-sqlite 0.5.3 → 0.6.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/LICENSE +1 -1
- package/dist/index.cjs +110 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +47 -2
- package/dist/index.d.ts +47 -2
- package/dist/index.js +110 -24
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2026 Aaron Koller
|
|
3
|
+
Copyright (c) 2026 Aaron Koller and Project Contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/dist/index.cjs
CHANGED
|
@@ -24,17 +24,21 @@ __export(index_exports, {
|
|
|
24
24
|
SqliteAppSyncableApplier: () => SqliteAppSyncableApplier,
|
|
25
25
|
SqliteAppSyncableNamespaceStore: () => SqliteAppSyncableNamespaceStore,
|
|
26
26
|
SqliteDatabaseAdapter: () => SqliteDatabaseAdapter,
|
|
27
|
+
appSyncableNamespaceTables: () => appSyncableNamespaceTables,
|
|
27
28
|
appSyncableTableName: () => appSyncableTableName,
|
|
28
29
|
applyConnectionPragmas: () => applyConnectionPragmas,
|
|
29
30
|
boundUserId: () => boundUserId,
|
|
30
31
|
checkLibraryBindable: () => checkLibraryBindable,
|
|
31
32
|
commitLibraryBinding: () => commitLibraryBinding,
|
|
33
|
+
createAppSyncableTables: () => createAppSyncableTables,
|
|
34
|
+
createReservedFileRecordsTable: () => createReservedFileRecordsTable,
|
|
32
35
|
deleteAppSyncableNamespace: () => deleteAppSyncableNamespace,
|
|
33
36
|
getAppSyncableNamespace: () => getAppSyncableNamespace,
|
|
34
37
|
initializeLocalSchema: () => initializeLocalSchema,
|
|
35
38
|
libraryKeyColumns: () => libraryKeyColumns,
|
|
36
39
|
libraryUserSegment: () => libraryUserSegment,
|
|
37
40
|
listAppSyncableNamespaces: () => listAppSyncableNamespaces,
|
|
41
|
+
registerAppSyncableNamespace: () => registerAppSyncableNamespace,
|
|
38
42
|
sqliteCompiler: () => compiler,
|
|
39
43
|
upsertAppSyncableNamespace: () => upsertAppSyncableNamespace
|
|
40
44
|
});
|
|
@@ -334,14 +338,14 @@ var SqliteDatabaseAdapter = class {
|
|
|
334
338
|
// JavaScript boolean and threw "Provided value cannot be bound to SQLite
|
|
335
339
|
// parameter". Nothing had exercised it because no metadata column was a
|
|
336
340
|
// boolean until `exif_present`.
|
|
337
|
-
runStmt(
|
|
338
|
-
this.getDatabase().prepare(
|
|
341
|
+
runStmt(sql5, ...params) {
|
|
342
|
+
this.getDatabase().prepare(sql5).run(...params.map(toSqliteParam));
|
|
339
343
|
}
|
|
340
|
-
getRow(
|
|
341
|
-
return this.getDatabase().prepare(
|
|
344
|
+
getRow(sql5, ...params) {
|
|
345
|
+
return this.getDatabase().prepare(sql5).get(...params.map(toSqliteParam));
|
|
342
346
|
}
|
|
343
|
-
allRows(
|
|
344
|
-
return this.getDatabase().prepare(
|
|
347
|
+
allRows(sql5, ...params) {
|
|
348
|
+
return this.getDatabase().prepare(sql5).all(...params.map(toSqliteParam));
|
|
345
349
|
}
|
|
346
350
|
async put(record) {
|
|
347
351
|
const row = recordToRow(record);
|
|
@@ -413,8 +417,8 @@ var SqliteDatabaseAdapter = class {
|
|
|
413
417
|
return (0, import_storage_adapter4.mergeDigestBuckets)(out);
|
|
414
418
|
}
|
|
415
419
|
async query(query) {
|
|
416
|
-
const { sql:
|
|
417
|
-
const rows = this.allRows(
|
|
420
|
+
const { sql: sql5, params } = buildSelectQuery(query);
|
|
421
|
+
const rows = this.allRows(sql5, ...params);
|
|
418
422
|
const limit = query.limit;
|
|
419
423
|
const hasMore = limit ? rows.length > limit : false;
|
|
420
424
|
const resultRows = hasMore ? rows.slice(0, limit) : rows;
|
|
@@ -428,13 +432,13 @@ var SqliteDatabaseAdapter = class {
|
|
|
428
432
|
};
|
|
429
433
|
}
|
|
430
434
|
async countRecords(query) {
|
|
431
|
-
const { sql:
|
|
432
|
-
const row = this.getRow(
|
|
435
|
+
const { sql: sql5, params } = buildCountQuery(query);
|
|
436
|
+
const row = this.getRow(sql5, ...params);
|
|
433
437
|
return row?.total ?? 0;
|
|
434
438
|
}
|
|
435
439
|
async countRecordsByType(query) {
|
|
436
|
-
const { sql:
|
|
437
|
-
const rows = this.allRows(
|
|
440
|
+
const { sql: sql5, params } = buildTypeCountsQuery(query);
|
|
441
|
+
const rows = this.allRows(sql5, ...params);
|
|
438
442
|
return rows.map((row) => ({
|
|
439
443
|
type: row.type,
|
|
440
444
|
count: Number(row.count),
|
|
@@ -1028,9 +1032,85 @@ function nodeIdOf(updatedAt, entry) {
|
|
|
1028
1032
|
return entry.timestamp.nodeId;
|
|
1029
1033
|
}
|
|
1030
1034
|
|
|
1031
|
-
// src/
|
|
1035
|
+
// src/app-syncable/tables.ts
|
|
1032
1036
|
var import_kysely5 = require("kysely");
|
|
1037
|
+
var import_shared_space_api2 = require("@starkeep/shared-space-api");
|
|
1033
1038
|
var import_protocol_primitives6 = require("@starkeep/protocol-primitives");
|
|
1039
|
+
function createSyncableTable(db, fullName, columns, indexes = []) {
|
|
1040
|
+
let tb = compiler.schema.createTable(fullName).ifNotExists();
|
|
1041
|
+
for (const c of columns) {
|
|
1042
|
+
tb = tb.addColumn(c.name, import_kysely5.sql.raw(c.type), (col) => {
|
|
1043
|
+
const withNull = c.notNull || c.primaryKey ? col.notNull() : col;
|
|
1044
|
+
return c.boolean ? withNull.check(import_kysely5.sql`${import_kysely5.sql.ref(c.name)} in (0, 1)`) : withNull;
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
tb = tb.addColumn("updated_at", "text", (col) => col.notNull()).addColumn("node_id", "text", (col) => col.notNull()).addColumn("deleted_at", "text");
|
|
1048
|
+
const pks = columns.filter((c) => c.primaryKey).map((c) => c.name);
|
|
1049
|
+
if (pks.length > 0) {
|
|
1050
|
+
tb = tb.addPrimaryKeyConstraint(`pk_${fullName}`, pks);
|
|
1051
|
+
}
|
|
1052
|
+
db.exec(tb.compile().sql);
|
|
1053
|
+
db.exec(
|
|
1054
|
+
compiler.schema.createIndex(`idx_${fullName}_updated_at`).ifNotExists().on(fullName).column("updated_at").compile().sql
|
|
1055
|
+
);
|
|
1056
|
+
db.exec(
|
|
1057
|
+
compiler.schema.createIndex(`idx_${fullName}_node_watermark`).ifNotExists().on(fullName).columns(["node_id", "updated_at"]).compile().sql
|
|
1058
|
+
);
|
|
1059
|
+
for (const index of indexes) {
|
|
1060
|
+
db.exec(
|
|
1061
|
+
compiler.schema.createIndex((0, import_shared_space_api2.syncableIndexName)(fullName, [...index.columns])).ifNotExists().on(fullName).columns([...index.columns]).compile().sql
|
|
1062
|
+
);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
function createAppSyncableTables(db, appId, tables) {
|
|
1066
|
+
for (const table of tables) {
|
|
1067
|
+
createSyncableTable(
|
|
1068
|
+
db,
|
|
1069
|
+
appSyncableTableName(appId, table.name),
|
|
1070
|
+
table.columns.map((c) => ({
|
|
1071
|
+
name: c.name,
|
|
1072
|
+
type: (0, import_protocol_primitives6.sqliteColumnType)(c.type),
|
|
1073
|
+
notNull: Boolean(c.notNull),
|
|
1074
|
+
primaryKey: Boolean(c.primaryKey),
|
|
1075
|
+
boolean: c.type === "boolean"
|
|
1076
|
+
})),
|
|
1077
|
+
table.indexes
|
|
1078
|
+
);
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
function createReservedFileRecordsTable(db, appId) {
|
|
1082
|
+
createSyncableTable(
|
|
1083
|
+
db,
|
|
1084
|
+
appSyncableTableName(appId, import_shared_space_api2.FILE_RECORDS_TABLE),
|
|
1085
|
+
// Through the one mapping, like `createAppSyncableTables` above. This was a
|
|
1086
|
+
// hand-written ternary whose Postgres twin in `dsql-ddl.ts` answered
|
|
1087
|
+
// `bigint` where this one answered `integer`, for one declared type.
|
|
1088
|
+
import_shared_space_api2.FILE_RECORDS_COLUMNS.map((c) => ({
|
|
1089
|
+
name: c.name,
|
|
1090
|
+
type: (0, import_protocol_primitives6.sqliteColumnType)(c.type),
|
|
1091
|
+
notNull: Boolean(c.notNull),
|
|
1092
|
+
primaryKey: Boolean(c.primaryKey)
|
|
1093
|
+
}))
|
|
1094
|
+
);
|
|
1095
|
+
}
|
|
1096
|
+
function appSyncableNamespaceTables(syncable) {
|
|
1097
|
+
const declared = syncable.tables.map((t) => (0, import_shared_space_api2.appSyncableTableInfo)(t.name, t.columns));
|
|
1098
|
+
return syncable.files ? [...declared, import_shared_space_api2.FILE_RECORDS_TABLE_INFO] : declared;
|
|
1099
|
+
}
|
|
1100
|
+
function registerAppSyncableNamespace(db, appId, syncable) {
|
|
1101
|
+
createAppSyncableTables(db, appId, syncable.tables);
|
|
1102
|
+
if (syncable.files) createReservedFileRecordsTable(db, appId);
|
|
1103
|
+
upsertAppSyncableNamespace(
|
|
1104
|
+
db,
|
|
1105
|
+
appId,
|
|
1106
|
+
appSyncableNamespaceTables(syncable),
|
|
1107
|
+
syncable.files === true
|
|
1108
|
+
);
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
// src/library-owner.ts
|
|
1112
|
+
var import_kysely6 = require("kysely");
|
|
1113
|
+
var import_protocol_primitives7 = require("@starkeep/protocol-primitives");
|
|
1034
1114
|
var OWNER_TABLE = "library_owner";
|
|
1035
1115
|
function ensureOwnerTable(db) {
|
|
1036
1116
|
db.exec(
|
|
@@ -1040,12 +1120,12 @@ function ensureOwnerTable(db) {
|
|
|
1040
1120
|
function boundUserId(db) {
|
|
1041
1121
|
ensureOwnerTable(db);
|
|
1042
1122
|
const row = db.prepare(
|
|
1043
|
-
compiler.selectFrom(OWNER_TABLE).select("user_id").where("id", "=",
|
|
1123
|
+
compiler.selectFrom(OWNER_TABLE).select("user_id").where("id", "=", import_kysely6.sql.lit(0)).compile().sql
|
|
1044
1124
|
).get();
|
|
1045
1125
|
return row?.user_id ?? null;
|
|
1046
1126
|
}
|
|
1047
1127
|
function libraryUserSegment(db) {
|
|
1048
|
-
return boundUserId(db) ??
|
|
1128
|
+
return boundUserId(db) ?? import_protocol_primitives7.LOCAL_USER_SEGMENT;
|
|
1049
1129
|
}
|
|
1050
1130
|
function libraryKeyColumns(db) {
|
|
1051
1131
|
return coreKeyColumns(db).filter(({ table }) => tableExists(db, table));
|
|
@@ -1058,7 +1138,7 @@ function coreKeyColumns(db) {
|
|
|
1058
1138
|
{ table: "resident_keys", column: "record_id", primaryKey: false }
|
|
1059
1139
|
];
|
|
1060
1140
|
const tables = db.prepare(
|
|
1061
|
-
compiler.selectFrom("sqlite_master").select("name").where("type", "=",
|
|
1141
|
+
compiler.selectFrom("sqlite_master").select("name").where("type", "=", import_kysely6.sql.lit("table")).compile().sql
|
|
1062
1142
|
).all();
|
|
1063
1143
|
for (const { name } of tables) {
|
|
1064
1144
|
if (name.endsWith("__starkeep_sync_records")) {
|
|
@@ -1072,20 +1152,20 @@ function coreKeyColumns(db) {
|
|
|
1072
1152
|
}
|
|
1073
1153
|
function tableExists(db, table) {
|
|
1074
1154
|
return db.prepare(
|
|
1075
|
-
compiler.selectFrom("sqlite_master").select("name").where("type", "=",
|
|
1155
|
+
compiler.selectFrom("sqlite_master").select("name").where("type", "=", import_kysely6.sql.lit("table")).where("name", "=", import_kysely6.sql.lit(table)).compile().sql
|
|
1076
1156
|
).get() !== void 0;
|
|
1077
1157
|
}
|
|
1078
1158
|
function namespaces(column, userId) {
|
|
1079
1159
|
return [
|
|
1080
1160
|
{
|
|
1081
|
-
local:
|
|
1161
|
+
local: import_kysely6.sql.lit(`shared/${import_protocol_primitives7.LOCAL_USER_SEGMENT}/%`),
|
|
1082
1162
|
// Keeps the slash after `local`, so `shared/local/x` becomes `shared/<userId>/x`.
|
|
1083
|
-
rebound:
|
|
1163
|
+
rebound: import_kysely6.sql`'shared/' || ${import_kysely6.sql.lit(userId)} || substr(${import_kysely6.sql.ref(column)}, ${import_kysely6.sql.lit(`shared/${import_protocol_primitives7.LOCAL_USER_SEGMENT}`.length + 1)})`
|
|
1084
1164
|
},
|
|
1085
1165
|
// apps/<appId>/local/syncable/... — the app id is the segment before `local`.
|
|
1086
1166
|
{
|
|
1087
|
-
local:
|
|
1088
|
-
rebound:
|
|
1167
|
+
local: import_kysely6.sql.lit(`apps/%/${import_protocol_primitives7.LOCAL_USER_SEGMENT}/syncable/%`),
|
|
1168
|
+
rebound: import_kysely6.sql`replace(${import_kysely6.sql.ref(column)}, ${import_kysely6.sql.lit(`/${import_protocol_primitives7.LOCAL_USER_SEGMENT}/syncable/`)}, ${import_kysely6.sql.lit(`/${userId}/syncable/`)})`
|
|
1089
1169
|
}
|
|
1090
1170
|
];
|
|
1091
1171
|
}
|
|
@@ -1093,12 +1173,12 @@ function rebindColumn(db, { table, column, primaryKey }, userId) {
|
|
|
1093
1173
|
let changed = 0;
|
|
1094
1174
|
for (const { local, rebound } of namespaces(column, userId)) {
|
|
1095
1175
|
if (primaryKey) {
|
|
1096
|
-
const duplicates = compiler.deleteFrom(table).where(
|
|
1097
|
-
|
|
1176
|
+
const duplicates = compiler.deleteFrom(table).where(import_kysely6.sql.ref(column), "like", local).where(
|
|
1177
|
+
import_kysely6.sql`${rebound} in (${compiler.selectFrom(table).select(import_kysely6.sql.ref(column).as("k"))})`
|
|
1098
1178
|
).compile();
|
|
1099
1179
|
db.prepare(duplicates.sql).run(...duplicates.parameters);
|
|
1100
1180
|
}
|
|
1101
|
-
const update = compiler.updateTable(table).set({ [column]: rebound }).where(
|
|
1181
|
+
const update = compiler.updateTable(table).set({ [column]: rebound }).where(import_kysely6.sql.ref(column), "like", local).compile();
|
|
1102
1182
|
const result = db.prepare(update.sql).run(...update.parameters);
|
|
1103
1183
|
changed += Number(result.changes ?? 0);
|
|
1104
1184
|
}
|
|
@@ -1117,7 +1197,7 @@ var LibraryBoundElsewhereError = class extends Error {
|
|
|
1117
1197
|
cloudSays;
|
|
1118
1198
|
};
|
|
1119
1199
|
function checkLibraryBindable(db, userId) {
|
|
1120
|
-
if (!
|
|
1200
|
+
if (!import_protocol_primitives7.USER_SEGMENT_PATTERN.test(userId) || userId === import_protocol_primitives7.LOCAL_USER_SEGMENT) {
|
|
1121
1201
|
throw new Error(`cannot bind the library to ${JSON.stringify(userId)}`);
|
|
1122
1202
|
}
|
|
1123
1203
|
const current = boundUserId(db);
|
|
@@ -1133,7 +1213,7 @@ function commitLibraryBinding(db, userId, extraKeyColumns = []) {
|
|
|
1133
1213
|
try {
|
|
1134
1214
|
for (const column of columns) rewritten += rebindColumn(db, column, userId);
|
|
1135
1215
|
db.prepare(
|
|
1136
|
-
compiler.insertInto(OWNER_TABLE).values({ id:
|
|
1216
|
+
compiler.insertInto(OWNER_TABLE).values({ id: import_kysely6.sql.lit(0), user_id: import_kysely6.sql.raw("?") }).onConflict(
|
|
1137
1217
|
(oc) => oc.column("id").doUpdateSet((eb) => ({ user_id: eb.ref("excluded.user_id") }))
|
|
1138
1218
|
).compile().sql
|
|
1139
1219
|
).run(userId);
|
|
@@ -1150,17 +1230,21 @@ function commitLibraryBinding(db, userId, extraKeyColumns = []) {
|
|
|
1150
1230
|
SqliteAppSyncableApplier,
|
|
1151
1231
|
SqliteAppSyncableNamespaceStore,
|
|
1152
1232
|
SqliteDatabaseAdapter,
|
|
1233
|
+
appSyncableNamespaceTables,
|
|
1153
1234
|
appSyncableTableName,
|
|
1154
1235
|
applyConnectionPragmas,
|
|
1155
1236
|
boundUserId,
|
|
1156
1237
|
checkLibraryBindable,
|
|
1157
1238
|
commitLibraryBinding,
|
|
1239
|
+
createAppSyncableTables,
|
|
1240
|
+
createReservedFileRecordsTable,
|
|
1158
1241
|
deleteAppSyncableNamespace,
|
|
1159
1242
|
getAppSyncableNamespace,
|
|
1160
1243
|
initializeLocalSchema,
|
|
1161
1244
|
libraryKeyColumns,
|
|
1162
1245
|
libraryUserSegment,
|
|
1163
1246
|
listAppSyncableNamespaces,
|
|
1247
|
+
registerAppSyncableNamespace,
|
|
1164
1248
|
sqliteCompiler,
|
|
1165
1249
|
upsertAppSyncableNamespace
|
|
1166
1250
|
});
|