@starkeep/storage-sqlite 0.5.3 → 0.6.1
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 +113 -29
- 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 +115 -28
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { S as SqliteDatabaseAdapter, a as SqliteDatabaseAdapterOptions, b as SqliteDriver } from './adapter-DoVOV6K-.cjs';
|
|
2
2
|
import { Kysely } from 'kysely';
|
|
3
3
|
import { RawDatabase, DigestBucket, ParsedQuery, BuildOptions, ParsedQueryResult } from '@starkeep/storage-adapter';
|
|
4
|
-
import { AppSyncableNamespaceStore, AppSyncableNamespace, AppSyncableTableInfo, AppSyncableApplier, ScanCapableApplier, AppSyncableRowEntry, ScanSincePage } from '@starkeep/shared-space-api';
|
|
4
|
+
import { AppSyncableNamespaceStore, AppSyncableNamespace, AppSyncableTableInfo, AppSyncableApplier, ScanCapableApplier, AppSyncableRowEntry, ScanSincePage, DeclaredColumn } from '@starkeep/shared-space-api';
|
|
5
5
|
export { AppSyncableNamespace, AppSyncableTableInfo } from '@starkeep/shared-space-api';
|
|
6
6
|
import { HLCTimestamp } from '@starkeep/protocol-primitives';
|
|
7
7
|
|
|
@@ -143,6 +143,51 @@ declare class SqliteAppSyncableApplier implements AppSyncableApplier, ScanCapabl
|
|
|
143
143
|
private selectRows;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
/**
|
|
147
|
+
* One syncable table as an app's manifest declares it: the structural shape of
|
|
148
|
+
* admin-manifest's `SyncableTable`, so this package needs no manifest schema
|
|
149
|
+
* and a phone can pass the block straight from the manifest JSON.
|
|
150
|
+
*/
|
|
151
|
+
interface SyncableTableDeclaration {
|
|
152
|
+
readonly name: string;
|
|
153
|
+
readonly columns: readonly DeclaredColumn[];
|
|
154
|
+
readonly indexes?: readonly {
|
|
155
|
+
readonly columns: readonly string[];
|
|
156
|
+
}[];
|
|
157
|
+
}
|
|
158
|
+
/** A manifest's `infraRequirements.appSpecificSyncable` block. */
|
|
159
|
+
interface AppSyncableDeclaration {
|
|
160
|
+
readonly tables: readonly SyncableTableDeclaration[];
|
|
161
|
+
readonly files?: boolean;
|
|
162
|
+
}
|
|
163
|
+
declare function createAppSyncableTables(db: RawDatabase, appId: string, tables: readonly SyncableTableDeclaration[]): void;
|
|
164
|
+
/**
|
|
165
|
+
* Create the framework-owned `_starkeep_sync_records` table for an app that
|
|
166
|
+
* opted into `filesEnabled`. Same column shape as the manifest-declared
|
|
167
|
+
* syncable tables (plus the standard updated_at/deleted_at HLC columns) so
|
|
168
|
+
* the LWW applier can treat it uniformly.
|
|
169
|
+
*/
|
|
170
|
+
declare function createReservedFileRecordsTable(db: RawDatabase, appId: string): void;
|
|
171
|
+
/**
|
|
172
|
+
* The namespace registry's description of an app's tables: the declared ones,
|
|
173
|
+
* plus the reserved file-records table when files are enabled. Derived from
|
|
174
|
+
* the declaration rather than read back from SQLite, because a declared type
|
|
175
|
+
* is not recoverable from a physical column, and it runs in the data server,
|
|
176
|
+
* which never sees a manifest.
|
|
177
|
+
*/
|
|
178
|
+
declare function appSyncableNamespaceTables(syncable: AppSyncableDeclaration): AppSyncableTableInfo[];
|
|
179
|
+
/**
|
|
180
|
+
* Create an app's syncable tables and register its namespace, from its
|
|
181
|
+
* manifest's `appSpecificSyncable` block. Idempotent, so a node may run it on
|
|
182
|
+
* every start.
|
|
183
|
+
*
|
|
184
|
+
* The desktop's local installer runs the same two halves as separate ledger
|
|
185
|
+
* steps; a node that holds an app's private data without installing the app,
|
|
186
|
+
* as Photos Mobile does, calls this once at bring-up. Both therefore create
|
|
187
|
+
* identical tables.
|
|
188
|
+
*/
|
|
189
|
+
declare function registerAppSyncableNamespace(db: RawDatabase, appId: string, syncable: AppSyncableDeclaration): void;
|
|
190
|
+
|
|
146
191
|
/**
|
|
147
192
|
* Which user a node's library belongs to, and the database half of binding it.
|
|
148
193
|
*
|
|
@@ -211,4 +256,4 @@ declare function checkLibraryBindable(db: RawDatabase, userId: string): void;
|
|
|
211
256
|
*/
|
|
212
257
|
declare function commitLibraryBinding(db: RawDatabase, userId: string, extraKeyColumns?: readonly LibraryKeyColumn[]): number;
|
|
213
258
|
|
|
214
|
-
export { LibraryBoundElsewhereError, type LibraryKeyColumn, SqliteAppSyncableApplier, SqliteAppSyncableNamespaceStore, appSyncableTableName, applyConnectionPragmas, boundUserId, checkLibraryBindable, commitLibraryBinding, deleteAppSyncableNamespace, getAppSyncableNamespace, initializeLocalSchema, libraryKeyColumns, libraryUserSegment, listAppSyncableNamespaces, compiler as sqliteCompiler, upsertAppSyncableNamespace };
|
|
259
|
+
export { type AppSyncableDeclaration, LibraryBoundElsewhereError, type LibraryKeyColumn, SqliteAppSyncableApplier, SqliteAppSyncableNamespaceStore, type SyncableTableDeclaration, appSyncableNamespaceTables, appSyncableTableName, applyConnectionPragmas, boundUserId, checkLibraryBindable, commitLibraryBinding, createAppSyncableTables, createReservedFileRecordsTable, deleteAppSyncableNamespace, getAppSyncableNamespace, initializeLocalSchema, libraryKeyColumns, libraryUserSegment, listAppSyncableNamespaces, registerAppSyncableNamespace, compiler as sqliteCompiler, upsertAppSyncableNamespace };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { S as SqliteDatabaseAdapter, a as SqliteDatabaseAdapterOptions, b as SqliteDriver } from './adapter-DoVOV6K-.js';
|
|
2
2
|
import { Kysely } from 'kysely';
|
|
3
3
|
import { RawDatabase, DigestBucket, ParsedQuery, BuildOptions, ParsedQueryResult } from '@starkeep/storage-adapter';
|
|
4
|
-
import { AppSyncableNamespaceStore, AppSyncableNamespace, AppSyncableTableInfo, AppSyncableApplier, ScanCapableApplier, AppSyncableRowEntry, ScanSincePage } from '@starkeep/shared-space-api';
|
|
4
|
+
import { AppSyncableNamespaceStore, AppSyncableNamespace, AppSyncableTableInfo, AppSyncableApplier, ScanCapableApplier, AppSyncableRowEntry, ScanSincePage, DeclaredColumn } from '@starkeep/shared-space-api';
|
|
5
5
|
export { AppSyncableNamespace, AppSyncableTableInfo } from '@starkeep/shared-space-api';
|
|
6
6
|
import { HLCTimestamp } from '@starkeep/protocol-primitives';
|
|
7
7
|
|
|
@@ -143,6 +143,51 @@ declare class SqliteAppSyncableApplier implements AppSyncableApplier, ScanCapabl
|
|
|
143
143
|
private selectRows;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
/**
|
|
147
|
+
* One syncable table as an app's manifest declares it: the structural shape of
|
|
148
|
+
* admin-manifest's `SyncableTable`, so this package needs no manifest schema
|
|
149
|
+
* and a phone can pass the block straight from the manifest JSON.
|
|
150
|
+
*/
|
|
151
|
+
interface SyncableTableDeclaration {
|
|
152
|
+
readonly name: string;
|
|
153
|
+
readonly columns: readonly DeclaredColumn[];
|
|
154
|
+
readonly indexes?: readonly {
|
|
155
|
+
readonly columns: readonly string[];
|
|
156
|
+
}[];
|
|
157
|
+
}
|
|
158
|
+
/** A manifest's `infraRequirements.appSpecificSyncable` block. */
|
|
159
|
+
interface AppSyncableDeclaration {
|
|
160
|
+
readonly tables: readonly SyncableTableDeclaration[];
|
|
161
|
+
readonly files?: boolean;
|
|
162
|
+
}
|
|
163
|
+
declare function createAppSyncableTables(db: RawDatabase, appId: string, tables: readonly SyncableTableDeclaration[]): void;
|
|
164
|
+
/**
|
|
165
|
+
* Create the framework-owned `_starkeep_sync_records` table for an app that
|
|
166
|
+
* opted into `filesEnabled`. Same column shape as the manifest-declared
|
|
167
|
+
* syncable tables (plus the standard updated_at/deleted_at HLC columns) so
|
|
168
|
+
* the LWW applier can treat it uniformly.
|
|
169
|
+
*/
|
|
170
|
+
declare function createReservedFileRecordsTable(db: RawDatabase, appId: string): void;
|
|
171
|
+
/**
|
|
172
|
+
* The namespace registry's description of an app's tables: the declared ones,
|
|
173
|
+
* plus the reserved file-records table when files are enabled. Derived from
|
|
174
|
+
* the declaration rather than read back from SQLite, because a declared type
|
|
175
|
+
* is not recoverable from a physical column, and it runs in the data server,
|
|
176
|
+
* which never sees a manifest.
|
|
177
|
+
*/
|
|
178
|
+
declare function appSyncableNamespaceTables(syncable: AppSyncableDeclaration): AppSyncableTableInfo[];
|
|
179
|
+
/**
|
|
180
|
+
* Create an app's syncable tables and register its namespace, from its
|
|
181
|
+
* manifest's `appSpecificSyncable` block. Idempotent, so a node may run it on
|
|
182
|
+
* every start.
|
|
183
|
+
*
|
|
184
|
+
* The desktop's local installer runs the same two halves as separate ledger
|
|
185
|
+
* steps; a node that holds an app's private data without installing the app,
|
|
186
|
+
* as Photos Mobile does, calls this once at bring-up. Both therefore create
|
|
187
|
+
* identical tables.
|
|
188
|
+
*/
|
|
189
|
+
declare function registerAppSyncableNamespace(db: RawDatabase, appId: string, syncable: AppSyncableDeclaration): void;
|
|
190
|
+
|
|
146
191
|
/**
|
|
147
192
|
* Which user a node's library belongs to, and the database half of binding it.
|
|
148
193
|
*
|
|
@@ -211,4 +256,4 @@ declare function checkLibraryBindable(db: RawDatabase, userId: string): void;
|
|
|
211
256
|
*/
|
|
212
257
|
declare function commitLibraryBinding(db: RawDatabase, userId: string, extraKeyColumns?: readonly LibraryKeyColumn[]): number;
|
|
213
258
|
|
|
214
|
-
export { LibraryBoundElsewhereError, type LibraryKeyColumn, SqliteAppSyncableApplier, SqliteAppSyncableNamespaceStore, appSyncableTableName, applyConnectionPragmas, boundUserId, checkLibraryBindable, commitLibraryBinding, deleteAppSyncableNamespace, getAppSyncableNamespace, initializeLocalSchema, libraryKeyColumns, libraryUserSegment, listAppSyncableNamespaces, compiler as sqliteCompiler, upsertAppSyncableNamespace };
|
|
259
|
+
export { type AppSyncableDeclaration, LibraryBoundElsewhereError, type LibraryKeyColumn, SqliteAppSyncableApplier, SqliteAppSyncableNamespaceStore, type SyncableTableDeclaration, appSyncableNamespaceTables, appSyncableTableName, applyConnectionPragmas, boundUserId, checkLibraryBindable, commitLibraryBinding, createAppSyncableTables, createReservedFileRecordsTable, deleteAppSyncableNamespace, getAppSyncableNamespace, initializeLocalSchema, libraryKeyColumns, libraryUserSegment, listAppSyncableNamespaces, registerAppSyncableNamespace, compiler as sqliteCompiler, upsertAppSyncableNamespace };
|
package/dist/index.js
CHANGED
|
@@ -22,9 +22,9 @@ import {
|
|
|
22
22
|
sharedQueryTableName,
|
|
23
23
|
SQLITE_APP_QUERY_DIALECT,
|
|
24
24
|
buildGetLabel,
|
|
25
|
-
buildLabelNodeWatermarks,
|
|
26
25
|
buildBucketDigest,
|
|
27
26
|
buildScanSinceForNode,
|
|
27
|
+
buildNodeWatermarks,
|
|
28
28
|
collectSince,
|
|
29
29
|
mergeDigestBuckets,
|
|
30
30
|
planNodeScans,
|
|
@@ -357,14 +357,14 @@ var SqliteDatabaseAdapter = class {
|
|
|
357
357
|
// JavaScript boolean and threw "Provided value cannot be bound to SQLite
|
|
358
358
|
// parameter". Nothing had exercised it because no metadata column was a
|
|
359
359
|
// boolean until `exif_present`.
|
|
360
|
-
runStmt(
|
|
361
|
-
this.getDatabase().prepare(
|
|
360
|
+
runStmt(sql5, ...params) {
|
|
361
|
+
this.getDatabase().prepare(sql5).run(...params.map(toSqliteParam));
|
|
362
362
|
}
|
|
363
|
-
getRow(
|
|
364
|
-
return this.getDatabase().prepare(
|
|
363
|
+
getRow(sql5, ...params) {
|
|
364
|
+
return this.getDatabase().prepare(sql5).get(...params.map(toSqliteParam));
|
|
365
365
|
}
|
|
366
|
-
allRows(
|
|
367
|
-
return this.getDatabase().prepare(
|
|
366
|
+
allRows(sql5, ...params) {
|
|
367
|
+
return this.getDatabase().prepare(sql5).all(...params.map(toSqliteParam));
|
|
368
368
|
}
|
|
369
369
|
async put(record) {
|
|
370
370
|
const row = recordToRow(record);
|
|
@@ -389,7 +389,7 @@ var SqliteDatabaseAdapter = class {
|
|
|
389
389
|
this.runStmt(query.sql, ...query.parameters);
|
|
390
390
|
}
|
|
391
391
|
async getNodeWatermarks() {
|
|
392
|
-
const watermarksQuery = compiler
|
|
392
|
+
const watermarksQuery = buildNodeWatermarks(compiler, RECORDS);
|
|
393
393
|
const rows = this.allRows(watermarksQuery.sql);
|
|
394
394
|
const out = {};
|
|
395
395
|
for (const row of rows) {
|
|
@@ -436,8 +436,8 @@ var SqliteDatabaseAdapter = class {
|
|
|
436
436
|
return mergeDigestBuckets(out);
|
|
437
437
|
}
|
|
438
438
|
async query(query) {
|
|
439
|
-
const { sql:
|
|
440
|
-
const rows = this.allRows(
|
|
439
|
+
const { sql: sql5, params } = buildSelectQuery(query);
|
|
440
|
+
const rows = this.allRows(sql5, ...params);
|
|
441
441
|
const limit = query.limit;
|
|
442
442
|
const hasMore = limit ? rows.length > limit : false;
|
|
443
443
|
const resultRows = hasMore ? rows.slice(0, limit) : rows;
|
|
@@ -451,13 +451,13 @@ var SqliteDatabaseAdapter = class {
|
|
|
451
451
|
};
|
|
452
452
|
}
|
|
453
453
|
async countRecords(query) {
|
|
454
|
-
const { sql:
|
|
455
|
-
const row = this.getRow(
|
|
454
|
+
const { sql: sql5, params } = buildCountQuery(query);
|
|
455
|
+
const row = this.getRow(sql5, ...params);
|
|
456
456
|
return row?.total ?? 0;
|
|
457
457
|
}
|
|
458
458
|
async countRecordsByType(query) {
|
|
459
|
-
const { sql:
|
|
460
|
-
const rows = this.allRows(
|
|
459
|
+
const { sql: sql5, params } = buildTypeCountsQuery(query);
|
|
460
|
+
const rows = this.allRows(sql5, ...params);
|
|
461
461
|
return rows.map((row) => ({
|
|
462
462
|
type: row.type,
|
|
463
463
|
count: Number(row.count),
|
|
@@ -653,7 +653,7 @@ var SqliteDatabaseAdapter = class {
|
|
|
653
653
|
return paginateLabelScan(rows, query.limit);
|
|
654
654
|
}
|
|
655
655
|
async getLabelNodeWatermarks() {
|
|
656
|
-
const query =
|
|
656
|
+
const query = buildNodeWatermarks(compiler, LABELS.table);
|
|
657
657
|
const rows = this.allRows(query.sql);
|
|
658
658
|
const out = {};
|
|
659
659
|
for (const row of rows) out[row.node_id] = deserializeHLC2(row.max_updated_at);
|
|
@@ -815,6 +815,7 @@ import {
|
|
|
815
815
|
collectRowPage as collectRowPage2,
|
|
816
816
|
SQLITE_APP_QUERY_DIALECT as SQLITE_APP_QUERY_DIALECT2,
|
|
817
817
|
buildBucketDigest as buildBucketDigest2,
|
|
818
|
+
buildNodeWatermarks as buildNodeWatermarks2,
|
|
818
819
|
buildScanSinceForNode as buildScanSinceForNode2,
|
|
819
820
|
collectSince as collectSince2,
|
|
820
821
|
planNodeScans as planNodeScans2,
|
|
@@ -973,7 +974,7 @@ var SqliteAppSyncableApplier = class {
|
|
|
973
974
|
if (!this.tableExists(fullName)) return {};
|
|
974
975
|
const rows = allCompiled(
|
|
975
976
|
this.db,
|
|
976
|
-
compiler
|
|
977
|
+
buildNodeWatermarks2(compiler, fullName)
|
|
977
978
|
);
|
|
978
979
|
const out = {};
|
|
979
980
|
for (const row of rows) {
|
|
@@ -1065,8 +1066,90 @@ function nodeIdOf(updatedAt, entry) {
|
|
|
1065
1066
|
return entry.timestamp.nodeId;
|
|
1066
1067
|
}
|
|
1067
1068
|
|
|
1068
|
-
// src/
|
|
1069
|
+
// src/app-syncable/tables.ts
|
|
1069
1070
|
import { sql as sql3 } from "kysely";
|
|
1071
|
+
import {
|
|
1072
|
+
FILE_RECORDS_COLUMNS,
|
|
1073
|
+
FILE_RECORDS_TABLE,
|
|
1074
|
+
FILE_RECORDS_TABLE_INFO,
|
|
1075
|
+
appSyncableTableInfo,
|
|
1076
|
+
syncableIndexName
|
|
1077
|
+
} from "@starkeep/shared-space-api";
|
|
1078
|
+
import { sqliteColumnType } from "@starkeep/protocol-primitives";
|
|
1079
|
+
function createSyncableTable(db, fullName, columns, indexes = []) {
|
|
1080
|
+
let tb = compiler.schema.createTable(fullName).ifNotExists();
|
|
1081
|
+
for (const c of columns) {
|
|
1082
|
+
tb = tb.addColumn(c.name, sql3.raw(c.type), (col) => {
|
|
1083
|
+
const withNull = c.notNull || c.primaryKey ? col.notNull() : col;
|
|
1084
|
+
return c.boolean ? withNull.check(sql3`${sql3.ref(c.name)} in (0, 1)`) : withNull;
|
|
1085
|
+
});
|
|
1086
|
+
}
|
|
1087
|
+
tb = tb.addColumn("updated_at", "text", (col) => col.notNull()).addColumn("node_id", "text", (col) => col.notNull()).addColumn("deleted_at", "text");
|
|
1088
|
+
const pks = columns.filter((c) => c.primaryKey).map((c) => c.name);
|
|
1089
|
+
if (pks.length > 0) {
|
|
1090
|
+
tb = tb.addPrimaryKeyConstraint(`pk_${fullName}`, pks);
|
|
1091
|
+
}
|
|
1092
|
+
db.exec(tb.compile().sql);
|
|
1093
|
+
db.exec(
|
|
1094
|
+
compiler.schema.createIndex(`idx_${fullName}_updated_at`).ifNotExists().on(fullName).column("updated_at").compile().sql
|
|
1095
|
+
);
|
|
1096
|
+
db.exec(
|
|
1097
|
+
compiler.schema.createIndex(`idx_${fullName}_node_watermark`).ifNotExists().on(fullName).columns(["node_id", "updated_at"]).compile().sql
|
|
1098
|
+
);
|
|
1099
|
+
for (const index of indexes) {
|
|
1100
|
+
db.exec(
|
|
1101
|
+
compiler.schema.createIndex(syncableIndexName(fullName, [...index.columns])).ifNotExists().on(fullName).columns([...index.columns]).compile().sql
|
|
1102
|
+
);
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
function createAppSyncableTables(db, appId, tables) {
|
|
1106
|
+
for (const table of tables) {
|
|
1107
|
+
createSyncableTable(
|
|
1108
|
+
db,
|
|
1109
|
+
appSyncableTableName(appId, table.name),
|
|
1110
|
+
table.columns.map((c) => ({
|
|
1111
|
+
name: c.name,
|
|
1112
|
+
type: sqliteColumnType(c.type),
|
|
1113
|
+
notNull: Boolean(c.notNull),
|
|
1114
|
+
primaryKey: Boolean(c.primaryKey),
|
|
1115
|
+
boolean: c.type === "boolean"
|
|
1116
|
+
})),
|
|
1117
|
+
table.indexes
|
|
1118
|
+
);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
function createReservedFileRecordsTable(db, appId) {
|
|
1122
|
+
createSyncableTable(
|
|
1123
|
+
db,
|
|
1124
|
+
appSyncableTableName(appId, FILE_RECORDS_TABLE),
|
|
1125
|
+
// Through the one mapping, like `createAppSyncableTables` above. This was a
|
|
1126
|
+
// hand-written ternary whose Postgres twin in `dsql-ddl.ts` answered
|
|
1127
|
+
// `bigint` where this one answered `integer`, for one declared type.
|
|
1128
|
+
FILE_RECORDS_COLUMNS.map((c) => ({
|
|
1129
|
+
name: c.name,
|
|
1130
|
+
type: sqliteColumnType(c.type),
|
|
1131
|
+
notNull: Boolean(c.notNull),
|
|
1132
|
+
primaryKey: Boolean(c.primaryKey)
|
|
1133
|
+
}))
|
|
1134
|
+
);
|
|
1135
|
+
}
|
|
1136
|
+
function appSyncableNamespaceTables(syncable) {
|
|
1137
|
+
const declared = syncable.tables.map((t) => appSyncableTableInfo(t.name, t.columns));
|
|
1138
|
+
return syncable.files ? [...declared, FILE_RECORDS_TABLE_INFO] : declared;
|
|
1139
|
+
}
|
|
1140
|
+
function registerAppSyncableNamespace(db, appId, syncable) {
|
|
1141
|
+
createAppSyncableTables(db, appId, syncable.tables);
|
|
1142
|
+
if (syncable.files) createReservedFileRecordsTable(db, appId);
|
|
1143
|
+
upsertAppSyncableNamespace(
|
|
1144
|
+
db,
|
|
1145
|
+
appId,
|
|
1146
|
+
appSyncableNamespaceTables(syncable),
|
|
1147
|
+
syncable.files === true
|
|
1148
|
+
);
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
// src/library-owner.ts
|
|
1152
|
+
import { sql as sql4 } from "kysely";
|
|
1070
1153
|
import { LOCAL_USER_SEGMENT, USER_SEGMENT_PATTERN } from "@starkeep/protocol-primitives";
|
|
1071
1154
|
var OWNER_TABLE = "library_owner";
|
|
1072
1155
|
function ensureOwnerTable(db) {
|
|
@@ -1077,7 +1160,7 @@ function ensureOwnerTable(db) {
|
|
|
1077
1160
|
function boundUserId(db) {
|
|
1078
1161
|
ensureOwnerTable(db);
|
|
1079
1162
|
const row = db.prepare(
|
|
1080
|
-
compiler.selectFrom(OWNER_TABLE).select("user_id").where("id", "=",
|
|
1163
|
+
compiler.selectFrom(OWNER_TABLE).select("user_id").where("id", "=", sql4.lit(0)).compile().sql
|
|
1081
1164
|
).get();
|
|
1082
1165
|
return row?.user_id ?? null;
|
|
1083
1166
|
}
|
|
@@ -1095,7 +1178,7 @@ function coreKeyColumns(db) {
|
|
|
1095
1178
|
{ table: "resident_keys", column: "record_id", primaryKey: false }
|
|
1096
1179
|
];
|
|
1097
1180
|
const tables = db.prepare(
|
|
1098
|
-
compiler.selectFrom("sqlite_master").select("name").where("type", "=",
|
|
1181
|
+
compiler.selectFrom("sqlite_master").select("name").where("type", "=", sql4.lit("table")).compile().sql
|
|
1099
1182
|
).all();
|
|
1100
1183
|
for (const { name } of tables) {
|
|
1101
1184
|
if (name.endsWith("__starkeep_sync_records")) {
|
|
@@ -1109,20 +1192,20 @@ function coreKeyColumns(db) {
|
|
|
1109
1192
|
}
|
|
1110
1193
|
function tableExists(db, table) {
|
|
1111
1194
|
return db.prepare(
|
|
1112
|
-
compiler.selectFrom("sqlite_master").select("name").where("type", "=",
|
|
1195
|
+
compiler.selectFrom("sqlite_master").select("name").where("type", "=", sql4.lit("table")).where("name", "=", sql4.lit(table)).compile().sql
|
|
1113
1196
|
).get() !== void 0;
|
|
1114
1197
|
}
|
|
1115
1198
|
function namespaces(column, userId) {
|
|
1116
1199
|
return [
|
|
1117
1200
|
{
|
|
1118
|
-
local:
|
|
1201
|
+
local: sql4.lit(`shared/${LOCAL_USER_SEGMENT}/%`),
|
|
1119
1202
|
// Keeps the slash after `local`, so `shared/local/x` becomes `shared/<userId>/x`.
|
|
1120
|
-
rebound:
|
|
1203
|
+
rebound: sql4`'shared/' || ${sql4.lit(userId)} || substr(${sql4.ref(column)}, ${sql4.lit(`shared/${LOCAL_USER_SEGMENT}`.length + 1)})`
|
|
1121
1204
|
},
|
|
1122
1205
|
// apps/<appId>/local/syncable/... — the app id is the segment before `local`.
|
|
1123
1206
|
{
|
|
1124
|
-
local:
|
|
1125
|
-
rebound:
|
|
1207
|
+
local: sql4.lit(`apps/%/${LOCAL_USER_SEGMENT}/syncable/%`),
|
|
1208
|
+
rebound: sql4`replace(${sql4.ref(column)}, ${sql4.lit(`/${LOCAL_USER_SEGMENT}/syncable/`)}, ${sql4.lit(`/${userId}/syncable/`)})`
|
|
1126
1209
|
}
|
|
1127
1210
|
];
|
|
1128
1211
|
}
|
|
@@ -1130,12 +1213,12 @@ function rebindColumn(db, { table, column, primaryKey }, userId) {
|
|
|
1130
1213
|
let changed = 0;
|
|
1131
1214
|
for (const { local, rebound } of namespaces(column, userId)) {
|
|
1132
1215
|
if (primaryKey) {
|
|
1133
|
-
const duplicates = compiler.deleteFrom(table).where(
|
|
1134
|
-
|
|
1216
|
+
const duplicates = compiler.deleteFrom(table).where(sql4.ref(column), "like", local).where(
|
|
1217
|
+
sql4`${rebound} in (${compiler.selectFrom(table).select(sql4.ref(column).as("k"))})`
|
|
1135
1218
|
).compile();
|
|
1136
1219
|
db.prepare(duplicates.sql).run(...duplicates.parameters);
|
|
1137
1220
|
}
|
|
1138
|
-
const update = compiler.updateTable(table).set({ [column]: rebound }).where(
|
|
1221
|
+
const update = compiler.updateTable(table).set({ [column]: rebound }).where(sql4.ref(column), "like", local).compile();
|
|
1139
1222
|
const result = db.prepare(update.sql).run(...update.parameters);
|
|
1140
1223
|
changed += Number(result.changes ?? 0);
|
|
1141
1224
|
}
|
|
@@ -1170,7 +1253,7 @@ function commitLibraryBinding(db, userId, extraKeyColumns = []) {
|
|
|
1170
1253
|
try {
|
|
1171
1254
|
for (const column of columns) rewritten += rebindColumn(db, column, userId);
|
|
1172
1255
|
db.prepare(
|
|
1173
|
-
compiler.insertInto(OWNER_TABLE).values({ id:
|
|
1256
|
+
compiler.insertInto(OWNER_TABLE).values({ id: sql4.lit(0), user_id: sql4.raw("?") }).onConflict(
|
|
1174
1257
|
(oc) => oc.column("id").doUpdateSet((eb) => ({ user_id: eb.ref("excluded.user_id") }))
|
|
1175
1258
|
).compile().sql
|
|
1176
1259
|
).run(userId);
|
|
@@ -1186,17 +1269,21 @@ export {
|
|
|
1186
1269
|
SqliteAppSyncableApplier,
|
|
1187
1270
|
SqliteAppSyncableNamespaceStore,
|
|
1188
1271
|
SqliteDatabaseAdapter,
|
|
1272
|
+
appSyncableNamespaceTables,
|
|
1189
1273
|
appSyncableTableName,
|
|
1190
1274
|
applyConnectionPragmas,
|
|
1191
1275
|
boundUserId,
|
|
1192
1276
|
checkLibraryBindable,
|
|
1193
1277
|
commitLibraryBinding,
|
|
1278
|
+
createAppSyncableTables,
|
|
1279
|
+
createReservedFileRecordsTable,
|
|
1194
1280
|
deleteAppSyncableNamespace,
|
|
1195
1281
|
getAppSyncableNamespace,
|
|
1196
1282
|
initializeLocalSchema,
|
|
1197
1283
|
libraryKeyColumns,
|
|
1198
1284
|
libraryUserSegment,
|
|
1199
1285
|
listAppSyncableNamespaces,
|
|
1286
|
+
registerAppSyncableNamespace,
|
|
1200
1287
|
compiler as sqliteCompiler,
|
|
1201
1288
|
upsertAppSyncableNamespace
|
|
1202
1289
|
};
|