@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/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @starkeep/storage-sqlite
|
|
2
2
|
|
|
3
|
-
SQLite implementation of `DatabaseAdapter` using the built-in `node:sqlite` module
|
|
3
|
+
SQLite implementation of `DatabaseAdapter` using the built-in `node:sqlite` module. No native compilation dependencies required.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -8,7 +8,7 @@ SQLite implementation of `DatabaseAdapter` using the built-in `node:sqlite` modu
|
|
|
8
8
|
pnpm add @starkeep/storage-sqlite
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
Requires Node.js
|
|
11
|
+
Requires Node.js 24 or later, the version the rest of Starkeep requires.
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
package/dist/index.cjs
CHANGED
|
@@ -67,6 +67,8 @@ function recordToRow(record) {
|
|
|
67
67
|
mime_type: record.mimeType,
|
|
68
68
|
size_bytes: record.sizeBytes,
|
|
69
69
|
original_filename: record.originalFilename,
|
|
70
|
+
original_created_at: record.originalCreatedAt ?? null,
|
|
71
|
+
original_updated_at: record.originalUpdatedAt ?? null,
|
|
70
72
|
origin_app_id: record.originAppId,
|
|
71
73
|
parent_id: record.parentId,
|
|
72
74
|
stand_in_role: record.standInRole,
|
|
@@ -90,6 +92,8 @@ function rowToRecord(row) {
|
|
|
90
92
|
mimeType: row.mime_type,
|
|
91
93
|
sizeBytes: row.size_bytes,
|
|
92
94
|
originalFilename: row.original_filename,
|
|
95
|
+
originalCreatedAt: row.original_created_at ?? null,
|
|
96
|
+
originalUpdatedAt: row.original_updated_at ?? null,
|
|
93
97
|
originAppId: row.origin_app_id,
|
|
94
98
|
parentId: row.parent_id ? (0, import_protocol_primitives.createStarkeepId)(row.parent_id) : null,
|
|
95
99
|
standInRole: (0, import_protocol_primitives.isStandInRole)(row.stand_in_role) ? row.stand_in_role : null,
|
|
@@ -142,12 +146,18 @@ function applyLocalSchemaDdl(db) {
|
|
|
142
146
|
db.exec("PRAGMA foreign_keys = ON");
|
|
143
147
|
applyConnectionPragmas(db);
|
|
144
148
|
db.exec(
|
|
145
|
-
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(
|
|
149
|
+
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(
|
|
146
150
|
"stand_in_role",
|
|
147
151
|
"text",
|
|
148
152
|
(c) => c.check(import_kysely2.sql`stand_in_role IN ('canonical', 'smaller')`)
|
|
149
153
|
).addColumn("fidelity", "integer").addColumn("canonical_threshold", "integer").addColumn("self_canonical", "integer").addColumn("stand_in_slot", "text").compile().sql
|
|
150
154
|
);
|
|
155
|
+
const recordColumns = db.prepare("SELECT name FROM pragma_table_info('shared_records')").all();
|
|
156
|
+
for (const column of ["original_created_at", "original_updated_at"]) {
|
|
157
|
+
if (!recordColumns.some((c) => c.name === column)) {
|
|
158
|
+
db.exec(compiler.schema.alterTable("shared_records").addColumn(column, "text").compile().sql);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
151
161
|
const sharedRecordsIndexes = [
|
|
152
162
|
compiler.schema.createIndex("idx_shared_records_type").ifNotExists().on("shared_records").column("type"),
|
|
153
163
|
compiler.schema.createIndex("idx_shared_records_node_watermark").ifNotExists().on("shared_records").columns(["node_id", "updated_at"]),
|
|
@@ -206,8 +216,14 @@ function applyLocalSchemaDdl(db) {
|
|
|
206
216
|
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
|
|
207
217
|
);
|
|
208
218
|
db.exec(
|
|
209
|
-
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(import_kysely2.sql`(datetime('now'))`)).addColumn("updated_at", "text", (c) => c.notNull().defaultTo(import_kysely2.sql`(datetime('now'))`)).compile().sql
|
|
219
|
+
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(import_kysely2.sql`(datetime('now'))`)).addColumn("updated_at", "text", (c) => c.notNull().defaultTo(import_kysely2.sql`(datetime('now'))`)).addColumn("source_dir", "text").compile().sql
|
|
210
220
|
);
|
|
221
|
+
const registryColumns = db.prepare("SELECT name FROM pragma_table_info('shared_app_registry')").all();
|
|
222
|
+
if (!registryColumns.some((c) => c.name === "source_dir")) {
|
|
223
|
+
db.exec(
|
|
224
|
+
compiler.schema.alterTable("shared_app_registry").addColumn("source_dir", "text").compile().sql
|
|
225
|
+
);
|
|
226
|
+
}
|
|
211
227
|
db.exec(
|
|
212
228
|
compiler.schema.createTable("shared_app_install_steps").ifNotExists().addColumn("app_id", "text", (c) => c.notNull()).addColumn(
|
|
213
229
|
"operation",
|
|
@@ -791,9 +807,10 @@ var import_kysely4 = require("kysely");
|
|
|
791
807
|
var import_protocol_primitives5 = require("@starkeep/protocol-primitives");
|
|
792
808
|
var import_shared_space_api2 = require("@starkeep/shared-space-api");
|
|
793
809
|
var import_storage_adapter5 = require("@starkeep/storage-adapter");
|
|
794
|
-
function
|
|
795
|
-
db
|
|
810
|
+
function countCompiled(db, compiled) {
|
|
811
|
+
return allCompiled(db, compiled).length;
|
|
796
812
|
}
|
|
813
|
+
var WRITTEN = import_kysely4.sql`1`.as("written");
|
|
797
814
|
function allCompiled(db, compiled) {
|
|
798
815
|
return db.prepare(compiled.sql).all(...compiled.parameters.map(toSqliteParam));
|
|
799
816
|
}
|
|
@@ -804,7 +821,7 @@ var SqliteAppSyncableApplier = class {
|
|
|
804
821
|
}
|
|
805
822
|
db;
|
|
806
823
|
namespace;
|
|
807
|
-
apply(entry) {
|
|
824
|
+
apply(entry, conditions) {
|
|
808
825
|
const ns = this.namespace.get(entry.appId);
|
|
809
826
|
if (!ns) {
|
|
810
827
|
throw new Error(`SqliteAppSyncableApplier: app "${entry.appId}" not installed`);
|
|
@@ -818,42 +835,52 @@ var SqliteAppSyncableApplier = class {
|
|
|
818
835
|
(0, import_shared_space_api2.requireWellFormedFileIndexRow)(entry);
|
|
819
836
|
const fullName = appSyncableTableName(entry.appId, entry.table);
|
|
820
837
|
const { pkColumns } = tableInfo;
|
|
838
|
+
let changes;
|
|
821
839
|
if (entry.op === "insert") {
|
|
822
|
-
this.applyInsert(fullName, pkColumns, entry);
|
|
840
|
+
changes = this.applyInsert(fullName, pkColumns, entry, conditions);
|
|
823
841
|
} else if (entry.op === "update") {
|
|
824
|
-
this.applyUpdate(fullName, pkColumns, entry);
|
|
842
|
+
changes = this.applyUpdate(fullName, pkColumns, entry, conditions);
|
|
825
843
|
} else {
|
|
826
|
-
this.applyDelete(fullName, pkColumns, entry);
|
|
844
|
+
changes = this.applyDelete(fullName, pkColumns, entry, conditions);
|
|
827
845
|
}
|
|
846
|
+
if (changes > 0 || conditions === void 0) return { changes };
|
|
847
|
+
const probe = this.selectRows((0, import_storage_adapter5.buildRowProbe)(compiler, fullName, (0, import_storage_adapter5.probeKeyFor)(entry, pkColumns)))[0];
|
|
848
|
+
return { changes: 0, skipped: (0, import_storage_adapter5.classifySkippedWrite)(entry.op, probe, conditions) };
|
|
828
849
|
}
|
|
829
|
-
applyInsert(fullName, pkColumns, entry) {
|
|
850
|
+
applyInsert(fullName, pkColumns, entry, conditions) {
|
|
830
851
|
const row = withNodeId(entry.row ?? {}, entry);
|
|
831
852
|
const cols = Object.keys(row);
|
|
832
|
-
if (cols.length === 0) return;
|
|
853
|
+
if (cols.length === 0) return 0;
|
|
854
|
+
if (conditions?.ifAbsent) {
|
|
855
|
+
(0, import_storage_adapter5.requireInsertKey)(entry, pkColumns);
|
|
856
|
+
return countCompiled(
|
|
857
|
+
this.db,
|
|
858
|
+
compiler.insertInto(fullName).values({ ...row }).onConflict((oc) => oc.columns(pkColumns).doNothing()).returning(WRITTEN).compile()
|
|
859
|
+
);
|
|
860
|
+
}
|
|
833
861
|
const updateCols = cols.filter((c) => !pkColumns.includes(c));
|
|
834
862
|
if (pkColumns.length === 0 || updateCols.length === 0) {
|
|
835
|
-
|
|
863
|
+
return countCompiled(
|
|
836
864
|
this.db,
|
|
837
|
-
compiler.insertInto(fullName).orIgnore().values({ ...row }).compile()
|
|
865
|
+
compiler.insertInto(fullName).orIgnore().values({ ...row }).returning(WRITTEN).compile()
|
|
838
866
|
);
|
|
839
|
-
return;
|
|
840
867
|
}
|
|
841
|
-
|
|
868
|
+
return countCompiled(
|
|
842
869
|
this.db,
|
|
843
870
|
compiler.insertInto(fullName).values({ ...row }).onConflict(
|
|
844
871
|
(oc) => oc.columns(pkColumns).doUpdateSet(
|
|
845
872
|
(eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
|
|
846
873
|
).where(import_kysely4.sql.ref("excluded.updated_at"), ">", import_kysely4.sql.ref(`${fullName}.updated_at`))
|
|
847
|
-
).compile()
|
|
874
|
+
).returning(WRITTEN).compile()
|
|
848
875
|
);
|
|
849
876
|
}
|
|
850
|
-
applyUpdate(fullName, pkColumns, entry) {
|
|
877
|
+
applyUpdate(fullName, pkColumns, entry, conditions) {
|
|
851
878
|
const rawPatch = entry.row ?? {};
|
|
852
879
|
const patch = rawPatch["updated_at"] ? withNodeId(rawPatch, entry) : rawPatch;
|
|
853
880
|
const where = (0, import_storage_adapter5.requireKeyedWhere)(entry, "update", pkColumns);
|
|
854
881
|
const patchCols = Object.keys(patch);
|
|
855
882
|
const whereCols = Object.keys(where);
|
|
856
|
-
if (patchCols.length === 0) return;
|
|
883
|
+
if (patchCols.length === 0) return 0;
|
|
857
884
|
const incomingUpdatedAt = patch["updated_at"];
|
|
858
885
|
let query = compiler.updateTable(fullName).set({ ...patch });
|
|
859
886
|
for (const c of whereCols) {
|
|
@@ -864,9 +891,13 @@ var SqliteAppSyncableApplier = class {
|
|
|
864
891
|
(eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", incomingUpdatedAt)])
|
|
865
892
|
);
|
|
866
893
|
}
|
|
867
|
-
|
|
894
|
+
query = query.where("deleted_at", "is", null);
|
|
895
|
+
if (conditions?.ifUpdatedAt !== void 0) {
|
|
896
|
+
query = query.where("updated_at", "=", conditions.ifUpdatedAt);
|
|
897
|
+
}
|
|
898
|
+
return countCompiled(this.db, query.returning(WRITTEN).compile());
|
|
868
899
|
}
|
|
869
|
-
applyDelete(fullName, pkColumns, entry) {
|
|
900
|
+
applyDelete(fullName, pkColumns, entry, conditions) {
|
|
870
901
|
const where = (0, import_storage_adapter5.requireKeyedWhere)(entry, "delete", pkColumns);
|
|
871
902
|
const whereCols = Object.keys(where);
|
|
872
903
|
const incomingUpdatedAt = entry.row?.["updated_at"];
|
|
@@ -875,22 +906,25 @@ var SqliteAppSyncableApplier = class {
|
|
|
875
906
|
if (carriesFullRow) {
|
|
876
907
|
const row = withNodeId({ ...entry.row, updated_at: ts, deleted_at: ts }, entry);
|
|
877
908
|
const updateCols = Object.keys(row).filter((c) => !pkColumns.includes(c));
|
|
878
|
-
|
|
909
|
+
return countCompiled(
|
|
879
910
|
this.db,
|
|
880
911
|
compiler.insertInto(fullName).values({ ...row }).onConflict(
|
|
881
912
|
(oc) => oc.columns(pkColumns).doUpdateSet(
|
|
882
913
|
(eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
|
|
883
914
|
).where(import_kysely4.sql.ref("excluded.updated_at"), ">", import_kysely4.sql.ref(`${fullName}.updated_at`))
|
|
884
|
-
).compile()
|
|
915
|
+
).returning(WRITTEN).compile()
|
|
885
916
|
);
|
|
886
|
-
return;
|
|
887
917
|
}
|
|
888
918
|
let query = compiler.updateTable(fullName).set({ deleted_at: ts, updated_at: ts, node_id: nodeIdOf(ts, entry) });
|
|
889
919
|
for (const c of whereCols) {
|
|
890
920
|
query = query.where(c, "=", where[c]);
|
|
891
921
|
}
|
|
892
922
|
query = query.where((eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)]));
|
|
893
|
-
|
|
923
|
+
query = query.where("deleted_at", "is", null);
|
|
924
|
+
if (conditions?.ifUpdatedAt !== void 0) {
|
|
925
|
+
query = query.where("updated_at", "=", conditions.ifUpdatedAt);
|
|
926
|
+
}
|
|
927
|
+
return countCompiled(this.db, query.returning(WRITTEN).compile());
|
|
894
928
|
}
|
|
895
929
|
/**
|
|
896
930
|
* Whether this table exists in *this* database.
|