@starkeep/storage-sqlite 0.6.3 → 0.8.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 +2 -2
- package/dist/index.cjs +57 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +61 -23
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
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, DeclaredColumn } from '@starkeep/shared-space-api';
|
|
4
|
+
import { AppSyncableNamespaceStore, AppSyncableNamespace, AppSyncableTableInfo, AppSyncableApplier, ScanCapableApplier, AppSyncableRowEntry, AppSyncableWriteConditions, AppSyncableApplyOutcome, 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
|
|
|
@@ -27,7 +27,9 @@ declare const compiler: Kysely<DB>;
|
|
|
27
27
|
*
|
|
28
28
|
* No migration system: this is a fresh-start schema. The user removes
|
|
29
29
|
* ~/.starkeep/data.db (or the local-data-server's STARKEEP_DIR is fresh)
|
|
30
|
-
* before this code runs.
|
|
30
|
+
* before this code runs. Two temporary guards are the exception: one adds
|
|
31
|
+
* `shared_app_registry.source_dir` to a registry created before the column,
|
|
32
|
+
* and one adds the original-date columns to `shared_records`.
|
|
31
33
|
*/
|
|
32
34
|
/**
|
|
33
35
|
* Connection settings the query grammar's portability depends on.
|
|
@@ -79,7 +81,7 @@ declare class SqliteAppSyncableApplier implements AppSyncableApplier, ScanCapabl
|
|
|
79
81
|
private readonly db;
|
|
80
82
|
private readonly namespace;
|
|
81
83
|
constructor(db: RawDatabase, namespace: AppSyncableNamespaceStore);
|
|
82
|
-
apply(entry: AppSyncableRowEntry):
|
|
84
|
+
apply(entry: AppSyncableRowEntry, conditions?: AppSyncableWriteConditions): AppSyncableApplyOutcome;
|
|
83
85
|
private applyInsert;
|
|
84
86
|
private applyUpdate;
|
|
85
87
|
private applyDelete;
|
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, DeclaredColumn } from '@starkeep/shared-space-api';
|
|
4
|
+
import { AppSyncableNamespaceStore, AppSyncableNamespace, AppSyncableTableInfo, AppSyncableApplier, ScanCapableApplier, AppSyncableRowEntry, AppSyncableWriteConditions, AppSyncableApplyOutcome, 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
|
|
|
@@ -27,7 +27,9 @@ declare const compiler: Kysely<DB>;
|
|
|
27
27
|
*
|
|
28
28
|
* No migration system: this is a fresh-start schema. The user removes
|
|
29
29
|
* ~/.starkeep/data.db (or the local-data-server's STARKEEP_DIR is fresh)
|
|
30
|
-
* before this code runs.
|
|
30
|
+
* before this code runs. Two temporary guards are the exception: one adds
|
|
31
|
+
* `shared_app_registry.source_dir` to a registry created before the column,
|
|
32
|
+
* and one adds the original-date columns to `shared_records`.
|
|
31
33
|
*/
|
|
32
34
|
/**
|
|
33
35
|
* Connection settings the query grammar's portability depends on.
|
|
@@ -79,7 +81,7 @@ declare class SqliteAppSyncableApplier implements AppSyncableApplier, ScanCapabl
|
|
|
79
81
|
private readonly db;
|
|
80
82
|
private readonly namespace;
|
|
81
83
|
constructor(db: RawDatabase, namespace: AppSyncableNamespaceStore);
|
|
82
|
-
apply(entry: AppSyncableRowEntry):
|
|
84
|
+
apply(entry: AppSyncableRowEntry, conditions?: AppSyncableWriteConditions): AppSyncableApplyOutcome;
|
|
83
85
|
private applyInsert;
|
|
84
86
|
private applyUpdate;
|
|
85
87
|
private applyDelete;
|
package/dist/index.js
CHANGED
|
@@ -71,6 +71,8 @@ function recordToRow(record) {
|
|
|
71
71
|
mime_type: record.mimeType,
|
|
72
72
|
size_bytes: record.sizeBytes,
|
|
73
73
|
original_filename: record.originalFilename,
|
|
74
|
+
original_created_at: record.originalCreatedAt ?? null,
|
|
75
|
+
original_updated_at: record.originalUpdatedAt ?? null,
|
|
74
76
|
origin_app_id: record.originAppId,
|
|
75
77
|
parent_id: record.parentId,
|
|
76
78
|
stand_in_role: record.standInRole,
|
|
@@ -94,6 +96,8 @@ function rowToRecord(row) {
|
|
|
94
96
|
mimeType: row.mime_type,
|
|
95
97
|
sizeBytes: row.size_bytes,
|
|
96
98
|
originalFilename: row.original_filename,
|
|
99
|
+
originalCreatedAt: row.original_created_at ?? null,
|
|
100
|
+
originalUpdatedAt: row.original_updated_at ?? null,
|
|
97
101
|
originAppId: row.origin_app_id,
|
|
98
102
|
parentId: row.parent_id ? createStarkeepId(row.parent_id) : null,
|
|
99
103
|
standInRole: isStandInRole(row.stand_in_role) ? row.stand_in_role : null,
|
|
@@ -161,12 +165,18 @@ function applyLocalSchemaDdl(db) {
|
|
|
161
165
|
db.exec("PRAGMA foreign_keys = ON");
|
|
162
166
|
applyConnectionPragmas(db);
|
|
163
167
|
db.exec(
|
|
164
|
-
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(
|
|
168
|
+
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("original_created_at", "text").addColumn("original_updated_at", "text").addColumn("origin_app_id", "text", (c) => c.notNull()).addColumn("parent_id", "text").addColumn(
|
|
165
169
|
"stand_in_role",
|
|
166
170
|
"text",
|
|
167
171
|
(c) => c.check(sql`stand_in_role IN ('canonical', 'smaller')`)
|
|
168
172
|
).addColumn("fidelity", "integer").addColumn("canonical_threshold", "integer").addColumn("self_canonical", "integer").addColumn("stand_in_slot", "text").compile().sql
|
|
169
173
|
);
|
|
174
|
+
const recordColumns = db.prepare("SELECT name FROM pragma_table_info('shared_records')").all();
|
|
175
|
+
for (const column of ["original_created_at", "original_updated_at"]) {
|
|
176
|
+
if (!recordColumns.some((c) => c.name === column)) {
|
|
177
|
+
db.exec(compiler.schema.alterTable("shared_records").addColumn(column, "text").compile().sql);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
170
180
|
const sharedRecordsIndexes = [
|
|
171
181
|
compiler.schema.createIndex("idx_shared_records_type").ifNotExists().on("shared_records").column("type"),
|
|
172
182
|
compiler.schema.createIndex("idx_shared_records_node_watermark").ifNotExists().on("shared_records").columns(["node_id", "updated_at"]),
|
|
@@ -225,8 +235,14 @@ function applyLocalSchemaDdl(db) {
|
|
|
225
235
|
compiler.schema.createTable("shared_app_label_keys").ifNotExists().addColumn("app_id", "text", (c) => c.notNull()).addColumn("key", "text", (c) => c.notNull()).addColumn("description", "text").addColumn("created_at", "text", (c) => c.notNull().defaultTo(sql`(datetime('now'))`)).addPrimaryKeyConstraint("pk_shared_app_label_keys", ["app_id", "key"]).compile().sql
|
|
226
236
|
);
|
|
227
237
|
db.exec(
|
|
228
|
-
compiler.schema.createTable("shared_app_registry").ifNotExists().addColumn("app_id", "text", (c) => c.primaryKey()).addColumn("name", "text", (c) => c.notNull()).addColumn("version", "text", (c) => c.notNull()).addColumn("tier", "text", (c) => c.notNull().defaultTo("app")).addColumn("manifest", "text", (c) => c.notNull()).addColumn("status", "text", (c) => c.notNull().defaultTo("installing")).addColumn("hmac_secret", "text", (c) => c.notNull()).addColumn("installed_at", "text", (c) => c.notNull().defaultTo(sql`(datetime('now'))`)).addColumn("updated_at", "text", (c) => c.notNull().defaultTo(sql`(datetime('now'))`)).compile().sql
|
|
238
|
+
compiler.schema.createTable("shared_app_registry").ifNotExists().addColumn("app_id", "text", (c) => c.primaryKey()).addColumn("name", "text", (c) => c.notNull()).addColumn("version", "text", (c) => c.notNull()).addColumn("tier", "text", (c) => c.notNull().defaultTo("app")).addColumn("manifest", "text", (c) => c.notNull()).addColumn("status", "text", (c) => c.notNull().defaultTo("installing")).addColumn("hmac_secret", "text", (c) => c.notNull()).addColumn("installed_at", "text", (c) => c.notNull().defaultTo(sql`(datetime('now'))`)).addColumn("updated_at", "text", (c) => c.notNull().defaultTo(sql`(datetime('now'))`)).addColumn("source_dir", "text").compile().sql
|
|
229
239
|
);
|
|
240
|
+
const registryColumns = db.prepare("SELECT name FROM pragma_table_info('shared_app_registry')").all();
|
|
241
|
+
if (!registryColumns.some((c) => c.name === "source_dir")) {
|
|
242
|
+
db.exec(
|
|
243
|
+
compiler.schema.alterTable("shared_app_registry").addColumn("source_dir", "text").compile().sql
|
|
244
|
+
);
|
|
245
|
+
}
|
|
230
246
|
db.exec(
|
|
231
247
|
compiler.schema.createTable("shared_app_install_steps").ifNotExists().addColumn("app_id", "text", (c) => c.notNull()).addColumn(
|
|
232
248
|
"operation",
|
|
@@ -817,17 +833,22 @@ import {
|
|
|
817
833
|
SQLITE_APP_QUERY_DIALECT as SQLITE_APP_QUERY_DIALECT2,
|
|
818
834
|
buildBucketDigest as buildBucketDigest2,
|
|
819
835
|
buildNodeWatermarks as buildNodeWatermarks2,
|
|
836
|
+
buildRowProbe,
|
|
837
|
+
classifySkippedWrite,
|
|
820
838
|
buildScanSinceForNode as buildScanSinceForNode2,
|
|
821
839
|
collectSince as collectSince2,
|
|
822
840
|
planNodeScans as planNodeScans2,
|
|
841
|
+
probeKeyFor,
|
|
842
|
+
requireInsertKey,
|
|
823
843
|
requireKeyedWhere,
|
|
824
844
|
rowToWireEntry,
|
|
825
845
|
toDigestBuckets as toDigestBuckets2,
|
|
826
846
|
DEFAULT_BUCKET_PREFIX_LENGTH as DEFAULT_BUCKET_PREFIX_LENGTH2
|
|
827
847
|
} from "@starkeep/storage-adapter";
|
|
828
|
-
function
|
|
829
|
-
db
|
|
848
|
+
function countCompiled(db, compiled) {
|
|
849
|
+
return allCompiled(db, compiled).length;
|
|
830
850
|
}
|
|
851
|
+
var WRITTEN = sql2`1`.as("written");
|
|
831
852
|
function allCompiled(db, compiled) {
|
|
832
853
|
return db.prepare(compiled.sql).all(...compiled.parameters.map(toSqliteParam));
|
|
833
854
|
}
|
|
@@ -838,7 +859,7 @@ var SqliteAppSyncableApplier = class {
|
|
|
838
859
|
}
|
|
839
860
|
db;
|
|
840
861
|
namespace;
|
|
841
|
-
apply(entry) {
|
|
862
|
+
apply(entry, conditions) {
|
|
842
863
|
const ns = this.namespace.get(entry.appId);
|
|
843
864
|
if (!ns) {
|
|
844
865
|
throw new Error(`SqliteAppSyncableApplier: app "${entry.appId}" not installed`);
|
|
@@ -852,42 +873,52 @@ var SqliteAppSyncableApplier = class {
|
|
|
852
873
|
requireWellFormedFileIndexRow(entry);
|
|
853
874
|
const fullName = appSyncableTableName(entry.appId, entry.table);
|
|
854
875
|
const { pkColumns } = tableInfo;
|
|
876
|
+
let changes;
|
|
855
877
|
if (entry.op === "insert") {
|
|
856
|
-
this.applyInsert(fullName, pkColumns, entry);
|
|
878
|
+
changes = this.applyInsert(fullName, pkColumns, entry, conditions);
|
|
857
879
|
} else if (entry.op === "update") {
|
|
858
|
-
this.applyUpdate(fullName, pkColumns, entry);
|
|
880
|
+
changes = this.applyUpdate(fullName, pkColumns, entry, conditions);
|
|
859
881
|
} else {
|
|
860
|
-
this.applyDelete(fullName, pkColumns, entry);
|
|
882
|
+
changes = this.applyDelete(fullName, pkColumns, entry, conditions);
|
|
861
883
|
}
|
|
884
|
+
if (changes > 0 || conditions === void 0) return { changes };
|
|
885
|
+
const probe = this.selectRows(buildRowProbe(compiler, fullName, probeKeyFor(entry, pkColumns)))[0];
|
|
886
|
+
return { changes: 0, skipped: classifySkippedWrite(entry.op, probe, conditions) };
|
|
862
887
|
}
|
|
863
|
-
applyInsert(fullName, pkColumns, entry) {
|
|
888
|
+
applyInsert(fullName, pkColumns, entry, conditions) {
|
|
864
889
|
const row = withNodeId(entry.row ?? {}, entry);
|
|
865
890
|
const cols = Object.keys(row);
|
|
866
|
-
if (cols.length === 0) return;
|
|
891
|
+
if (cols.length === 0) return 0;
|
|
892
|
+
if (conditions?.ifAbsent) {
|
|
893
|
+
requireInsertKey(entry, pkColumns);
|
|
894
|
+
return countCompiled(
|
|
895
|
+
this.db,
|
|
896
|
+
compiler.insertInto(fullName).values({ ...row }).onConflict((oc) => oc.columns(pkColumns).doNothing()).returning(WRITTEN).compile()
|
|
897
|
+
);
|
|
898
|
+
}
|
|
867
899
|
const updateCols = cols.filter((c) => !pkColumns.includes(c));
|
|
868
900
|
if (pkColumns.length === 0 || updateCols.length === 0) {
|
|
869
|
-
|
|
901
|
+
return countCompiled(
|
|
870
902
|
this.db,
|
|
871
|
-
compiler.insertInto(fullName).orIgnore().values({ ...row }).compile()
|
|
903
|
+
compiler.insertInto(fullName).orIgnore().values({ ...row }).returning(WRITTEN).compile()
|
|
872
904
|
);
|
|
873
|
-
return;
|
|
874
905
|
}
|
|
875
|
-
|
|
906
|
+
return countCompiled(
|
|
876
907
|
this.db,
|
|
877
908
|
compiler.insertInto(fullName).values({ ...row }).onConflict(
|
|
878
909
|
(oc) => oc.columns(pkColumns).doUpdateSet(
|
|
879
910
|
(eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
|
|
880
911
|
).where(sql2.ref("excluded.updated_at"), ">", sql2.ref(`${fullName}.updated_at`))
|
|
881
|
-
).compile()
|
|
912
|
+
).returning(WRITTEN).compile()
|
|
882
913
|
);
|
|
883
914
|
}
|
|
884
|
-
applyUpdate(fullName, pkColumns, entry) {
|
|
915
|
+
applyUpdate(fullName, pkColumns, entry, conditions) {
|
|
885
916
|
const rawPatch = entry.row ?? {};
|
|
886
917
|
const patch = rawPatch["updated_at"] ? withNodeId(rawPatch, entry) : rawPatch;
|
|
887
918
|
const where = requireKeyedWhere(entry, "update", pkColumns);
|
|
888
919
|
const patchCols = Object.keys(patch);
|
|
889
920
|
const whereCols = Object.keys(where);
|
|
890
|
-
if (patchCols.length === 0) return;
|
|
921
|
+
if (patchCols.length === 0) return 0;
|
|
891
922
|
const incomingUpdatedAt = patch["updated_at"];
|
|
892
923
|
let query = compiler.updateTable(fullName).set({ ...patch });
|
|
893
924
|
for (const c of whereCols) {
|
|
@@ -898,9 +929,13 @@ var SqliteAppSyncableApplier = class {
|
|
|
898
929
|
(eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", incomingUpdatedAt)])
|
|
899
930
|
);
|
|
900
931
|
}
|
|
901
|
-
|
|
932
|
+
query = query.where("deleted_at", "is", null);
|
|
933
|
+
if (conditions?.ifUpdatedAt !== void 0) {
|
|
934
|
+
query = query.where("updated_at", "=", conditions.ifUpdatedAt);
|
|
935
|
+
}
|
|
936
|
+
return countCompiled(this.db, query.returning(WRITTEN).compile());
|
|
902
937
|
}
|
|
903
|
-
applyDelete(fullName, pkColumns, entry) {
|
|
938
|
+
applyDelete(fullName, pkColumns, entry, conditions) {
|
|
904
939
|
const where = requireKeyedWhere(entry, "delete", pkColumns);
|
|
905
940
|
const whereCols = Object.keys(where);
|
|
906
941
|
const incomingUpdatedAt = entry.row?.["updated_at"];
|
|
@@ -909,22 +944,25 @@ var SqliteAppSyncableApplier = class {
|
|
|
909
944
|
if (carriesFullRow) {
|
|
910
945
|
const row = withNodeId({ ...entry.row, updated_at: ts, deleted_at: ts }, entry);
|
|
911
946
|
const updateCols = Object.keys(row).filter((c) => !pkColumns.includes(c));
|
|
912
|
-
|
|
947
|
+
return countCompiled(
|
|
913
948
|
this.db,
|
|
914
949
|
compiler.insertInto(fullName).values({ ...row }).onConflict(
|
|
915
950
|
(oc) => oc.columns(pkColumns).doUpdateSet(
|
|
916
951
|
(eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
|
|
917
952
|
).where(sql2.ref("excluded.updated_at"), ">", sql2.ref(`${fullName}.updated_at`))
|
|
918
|
-
).compile()
|
|
953
|
+
).returning(WRITTEN).compile()
|
|
919
954
|
);
|
|
920
|
-
return;
|
|
921
955
|
}
|
|
922
956
|
let query = compiler.updateTable(fullName).set({ deleted_at: ts, updated_at: ts, node_id: nodeIdOf(ts, entry) });
|
|
923
957
|
for (const c of whereCols) {
|
|
924
958
|
query = query.where(c, "=", where[c]);
|
|
925
959
|
}
|
|
926
960
|
query = query.where((eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)]));
|
|
927
|
-
|
|
961
|
+
query = query.where("deleted_at", "is", null);
|
|
962
|
+
if (conditions?.ifUpdatedAt !== void 0) {
|
|
963
|
+
query = query.where("updated_at", "=", conditions.ifUpdatedAt);
|
|
964
|
+
}
|
|
965
|
+
return countCompiled(this.db, query.returning(WRITTEN).compile());
|
|
928
966
|
}
|
|
929
967
|
/**
|
|
930
968
|
* Whether this table exists in *this* database.
|