@starkeep/storage-sqlite 0.7.0 → 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 +50 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +54 -22
- 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"]),
|
|
@@ -797,9 +807,10 @@ var import_kysely4 = require("kysely");
|
|
|
797
807
|
var import_protocol_primitives5 = require("@starkeep/protocol-primitives");
|
|
798
808
|
var import_shared_space_api2 = require("@starkeep/shared-space-api");
|
|
799
809
|
var import_storage_adapter5 = require("@starkeep/storage-adapter");
|
|
800
|
-
function
|
|
801
|
-
db
|
|
810
|
+
function countCompiled(db, compiled) {
|
|
811
|
+
return allCompiled(db, compiled).length;
|
|
802
812
|
}
|
|
813
|
+
var WRITTEN = import_kysely4.sql`1`.as("written");
|
|
803
814
|
function allCompiled(db, compiled) {
|
|
804
815
|
return db.prepare(compiled.sql).all(...compiled.parameters.map(toSqliteParam));
|
|
805
816
|
}
|
|
@@ -810,7 +821,7 @@ var SqliteAppSyncableApplier = class {
|
|
|
810
821
|
}
|
|
811
822
|
db;
|
|
812
823
|
namespace;
|
|
813
|
-
apply(entry) {
|
|
824
|
+
apply(entry, conditions) {
|
|
814
825
|
const ns = this.namespace.get(entry.appId);
|
|
815
826
|
if (!ns) {
|
|
816
827
|
throw new Error(`SqliteAppSyncableApplier: app "${entry.appId}" not installed`);
|
|
@@ -824,42 +835,52 @@ var SqliteAppSyncableApplier = class {
|
|
|
824
835
|
(0, import_shared_space_api2.requireWellFormedFileIndexRow)(entry);
|
|
825
836
|
const fullName = appSyncableTableName(entry.appId, entry.table);
|
|
826
837
|
const { pkColumns } = tableInfo;
|
|
838
|
+
let changes;
|
|
827
839
|
if (entry.op === "insert") {
|
|
828
|
-
this.applyInsert(fullName, pkColumns, entry);
|
|
840
|
+
changes = this.applyInsert(fullName, pkColumns, entry, conditions);
|
|
829
841
|
} else if (entry.op === "update") {
|
|
830
|
-
this.applyUpdate(fullName, pkColumns, entry);
|
|
842
|
+
changes = this.applyUpdate(fullName, pkColumns, entry, conditions);
|
|
831
843
|
} else {
|
|
832
|
-
this.applyDelete(fullName, pkColumns, entry);
|
|
844
|
+
changes = this.applyDelete(fullName, pkColumns, entry, conditions);
|
|
833
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) };
|
|
834
849
|
}
|
|
835
|
-
applyInsert(fullName, pkColumns, entry) {
|
|
850
|
+
applyInsert(fullName, pkColumns, entry, conditions) {
|
|
836
851
|
const row = withNodeId(entry.row ?? {}, entry);
|
|
837
852
|
const cols = Object.keys(row);
|
|
838
|
-
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
|
+
}
|
|
839
861
|
const updateCols = cols.filter((c) => !pkColumns.includes(c));
|
|
840
862
|
if (pkColumns.length === 0 || updateCols.length === 0) {
|
|
841
|
-
|
|
863
|
+
return countCompiled(
|
|
842
864
|
this.db,
|
|
843
|
-
compiler.insertInto(fullName).orIgnore().values({ ...row }).compile()
|
|
865
|
+
compiler.insertInto(fullName).orIgnore().values({ ...row }).returning(WRITTEN).compile()
|
|
844
866
|
);
|
|
845
|
-
return;
|
|
846
867
|
}
|
|
847
|
-
|
|
868
|
+
return countCompiled(
|
|
848
869
|
this.db,
|
|
849
870
|
compiler.insertInto(fullName).values({ ...row }).onConflict(
|
|
850
871
|
(oc) => oc.columns(pkColumns).doUpdateSet(
|
|
851
872
|
(eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
|
|
852
873
|
).where(import_kysely4.sql.ref("excluded.updated_at"), ">", import_kysely4.sql.ref(`${fullName}.updated_at`))
|
|
853
|
-
).compile()
|
|
874
|
+
).returning(WRITTEN).compile()
|
|
854
875
|
);
|
|
855
876
|
}
|
|
856
|
-
applyUpdate(fullName, pkColumns, entry) {
|
|
877
|
+
applyUpdate(fullName, pkColumns, entry, conditions) {
|
|
857
878
|
const rawPatch = entry.row ?? {};
|
|
858
879
|
const patch = rawPatch["updated_at"] ? withNodeId(rawPatch, entry) : rawPatch;
|
|
859
880
|
const where = (0, import_storage_adapter5.requireKeyedWhere)(entry, "update", pkColumns);
|
|
860
881
|
const patchCols = Object.keys(patch);
|
|
861
882
|
const whereCols = Object.keys(where);
|
|
862
|
-
if (patchCols.length === 0) return;
|
|
883
|
+
if (patchCols.length === 0) return 0;
|
|
863
884
|
const incomingUpdatedAt = patch["updated_at"];
|
|
864
885
|
let query = compiler.updateTable(fullName).set({ ...patch });
|
|
865
886
|
for (const c of whereCols) {
|
|
@@ -870,9 +891,13 @@ var SqliteAppSyncableApplier = class {
|
|
|
870
891
|
(eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", incomingUpdatedAt)])
|
|
871
892
|
);
|
|
872
893
|
}
|
|
873
|
-
|
|
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());
|
|
874
899
|
}
|
|
875
|
-
applyDelete(fullName, pkColumns, entry) {
|
|
900
|
+
applyDelete(fullName, pkColumns, entry, conditions) {
|
|
876
901
|
const where = (0, import_storage_adapter5.requireKeyedWhere)(entry, "delete", pkColumns);
|
|
877
902
|
const whereCols = Object.keys(where);
|
|
878
903
|
const incomingUpdatedAt = entry.row?.["updated_at"];
|
|
@@ -881,22 +906,25 @@ var SqliteAppSyncableApplier = class {
|
|
|
881
906
|
if (carriesFullRow) {
|
|
882
907
|
const row = withNodeId({ ...entry.row, updated_at: ts, deleted_at: ts }, entry);
|
|
883
908
|
const updateCols = Object.keys(row).filter((c) => !pkColumns.includes(c));
|
|
884
|
-
|
|
909
|
+
return countCompiled(
|
|
885
910
|
this.db,
|
|
886
911
|
compiler.insertInto(fullName).values({ ...row }).onConflict(
|
|
887
912
|
(oc) => oc.columns(pkColumns).doUpdateSet(
|
|
888
913
|
(eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
|
|
889
914
|
).where(import_kysely4.sql.ref("excluded.updated_at"), ">", import_kysely4.sql.ref(`${fullName}.updated_at`))
|
|
890
|
-
).compile()
|
|
915
|
+
).returning(WRITTEN).compile()
|
|
891
916
|
);
|
|
892
|
-
return;
|
|
893
917
|
}
|
|
894
918
|
let query = compiler.updateTable(fullName).set({ deleted_at: ts, updated_at: ts, node_id: nodeIdOf(ts, entry) });
|
|
895
919
|
for (const c of whereCols) {
|
|
896
920
|
query = query.where(c, "=", where[c]);
|
|
897
921
|
}
|
|
898
922
|
query = query.where((eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)]));
|
|
899
|
-
|
|
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());
|
|
900
928
|
}
|
|
901
929
|
/**
|
|
902
930
|
* Whether this table exists in *this* database.
|