@starkeep/storage-sqlite 0.7.0 → 0.9.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/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
- export { S as SqliteDatabaseAdapter, a as SqliteDatabaseAdapterOptions, b as SqliteDriver } from './adapter-DoVOV6K-.cjs';
1
+ export { S as SqliteDatabaseAdapter, a as SqliteDatabaseAdapterOptions, b as SqliteDriver } from './adapter-Cm4K6kmH.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,8 +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. One temporary guard is the exception: it adds
31
- * `shared_app_registry.source_dir` to a registry created before the column.
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`.
32
33
  */
33
34
  /**
34
35
  * Connection settings the query grammar's portability depends on.
@@ -80,7 +81,7 @@ declare class SqliteAppSyncableApplier implements AppSyncableApplier, ScanCapabl
80
81
  private readonly db;
81
82
  private readonly namespace;
82
83
  constructor(db: RawDatabase, namespace: AppSyncableNamespaceStore);
83
- apply(entry: AppSyncableRowEntry): void;
84
+ apply(entry: AppSyncableRowEntry, conditions?: AppSyncableWriteConditions): AppSyncableApplyOutcome;
84
85
  private applyInsert;
85
86
  private applyUpdate;
86
87
  private applyDelete;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- export { S as SqliteDatabaseAdapter, a as SqliteDatabaseAdapterOptions, b as SqliteDriver } from './adapter-DoVOV6K-.js';
1
+ export { S as SqliteDatabaseAdapter, a as SqliteDatabaseAdapterOptions, b as SqliteDriver } from './adapter-Cm4K6kmH.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,8 +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. One temporary guard is the exception: it adds
31
- * `shared_app_registry.source_dir` to a registry created before the column.
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`.
32
33
  */
33
34
  /**
34
35
  * Connection settings the query grammar's portability depends on.
@@ -80,7 +81,7 @@ declare class SqliteAppSyncableApplier implements AppSyncableApplier, ScanCapabl
80
81
  private readonly db;
81
82
  private readonly namespace;
82
83
  constructor(db: RawDatabase, namespace: AppSyncableNamespaceStore);
83
- apply(entry: AppSyncableRowEntry): void;
84
+ apply(entry: AppSyncableRowEntry, conditions?: AppSyncableWriteConditions): AppSyncableApplyOutcome;
84
85
  private applyInsert;
85
86
  private applyUpdate;
86
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"]),
@@ -373,16 +383,32 @@ var SqliteDatabaseAdapter = class {
373
383
  return this.getDatabase().prepare(sql5).all(...params.map(toSqliteParam));
374
384
  }
375
385
  async put(record) {
386
+ this.upsertRecord(record, false);
387
+ }
388
+ async putSnapshot(record) {
389
+ return this.upsertRecord(record, true);
390
+ }
391
+ /**
392
+ * One upsert statement. With `onlyIfNewer` an existing row is replaced only
393
+ * by a strictly later `updated_at` (see `DatabaseAdapter.putSnapshot`), and
394
+ * the result says whether a row was written.
395
+ */
396
+ upsertRecord(record, onlyIfNewer) {
376
397
  const row = recordToRow(record);
377
398
  const updateColumns = Object.keys(row).filter((column) => column !== "id");
378
- const query = compiler.insertInto("shared_records").values({ ...row }).onConflict(
379
- (oc) => oc.column("id").doUpdateSet(
399
+ const query = compiler.insertInto("shared_records").values({ ...row }).onConflict((oc) => {
400
+ const update = oc.column("id").doUpdateSet(
380
401
  (eb) => Object.fromEntries(
381
402
  updateColumns.map((column) => [column, eb.ref(`excluded.${column}`)])
382
403
  )
383
- )
384
- ).compile();
385
- this.runStmt(query.sql, ...query.parameters);
404
+ );
405
+ return onlyIfNewer ? update.where(
406
+ kSql.ref("excluded.updated_at"),
407
+ ">",
408
+ kSql.ref("shared_records.updated_at")
409
+ ) : update;
410
+ }).returning("id").compile();
411
+ return this.allRows(query.sql, ...query.parameters).length > 0;
386
412
  }
387
413
  async get(id) {
388
414
  const query = compiler.selectFrom("shared_records").selectAll().where("id", "=", id).compile();
@@ -646,7 +672,7 @@ var SqliteDatabaseAdapter = class {
646
672
  // ---- Label sync ---------------------------------------------------------
647
673
  async putLabel(label) {
648
674
  const query = buildLabelSnapshotUpsert(compiler, LABELS, label);
649
- this.runStmt(query.sql, ...query.parameters);
675
+ return this.allRows(query.sql, ...query.parameters).length > 0;
650
676
  }
651
677
  async getLabel(recordId, appId, key, value) {
652
678
  const query = buildGetLabel(compiler, LABELS, recordId, appId, key, value);
@@ -823,17 +849,22 @@ import {
823
849
  SQLITE_APP_QUERY_DIALECT as SQLITE_APP_QUERY_DIALECT2,
824
850
  buildBucketDigest as buildBucketDigest2,
825
851
  buildNodeWatermarks as buildNodeWatermarks2,
852
+ buildRowProbe,
853
+ classifySkippedWrite,
826
854
  buildScanSinceForNode as buildScanSinceForNode2,
827
855
  collectSince as collectSince2,
828
856
  planNodeScans as planNodeScans2,
857
+ probeKeyFor,
858
+ requireInsertKey,
829
859
  requireKeyedWhere,
830
860
  rowToWireEntry,
831
861
  toDigestBuckets as toDigestBuckets2,
832
862
  DEFAULT_BUCKET_PREFIX_LENGTH as DEFAULT_BUCKET_PREFIX_LENGTH2
833
863
  } from "@starkeep/storage-adapter";
834
- function runCompiled(db, compiled) {
835
- db.prepare(compiled.sql).run(...compiled.parameters.map(toSqliteParam));
864
+ function countCompiled(db, compiled) {
865
+ return allCompiled(db, compiled).length;
836
866
  }
867
+ var WRITTEN = sql2`1`.as("written");
837
868
  function allCompiled(db, compiled) {
838
869
  return db.prepare(compiled.sql).all(...compiled.parameters.map(toSqliteParam));
839
870
  }
@@ -844,7 +875,7 @@ var SqliteAppSyncableApplier = class {
844
875
  }
845
876
  db;
846
877
  namespace;
847
- apply(entry) {
878
+ apply(entry, conditions) {
848
879
  const ns = this.namespace.get(entry.appId);
849
880
  if (!ns) {
850
881
  throw new Error(`SqliteAppSyncableApplier: app "${entry.appId}" not installed`);
@@ -858,42 +889,52 @@ var SqliteAppSyncableApplier = class {
858
889
  requireWellFormedFileIndexRow(entry);
859
890
  const fullName = appSyncableTableName(entry.appId, entry.table);
860
891
  const { pkColumns } = tableInfo;
892
+ let changes;
861
893
  if (entry.op === "insert") {
862
- this.applyInsert(fullName, pkColumns, entry);
894
+ changes = this.applyInsert(fullName, pkColumns, entry, conditions);
863
895
  } else if (entry.op === "update") {
864
- this.applyUpdate(fullName, pkColumns, entry);
896
+ changes = this.applyUpdate(fullName, pkColumns, entry, conditions);
865
897
  } else {
866
- this.applyDelete(fullName, pkColumns, entry);
898
+ changes = this.applyDelete(fullName, pkColumns, entry, conditions);
867
899
  }
900
+ if (changes > 0 || conditions === void 0) return { changes };
901
+ const probe = this.selectRows(buildRowProbe(compiler, fullName, probeKeyFor(entry, pkColumns)))[0];
902
+ return { changes: 0, skipped: classifySkippedWrite(entry.op, probe, conditions) };
868
903
  }
869
- applyInsert(fullName, pkColumns, entry) {
904
+ applyInsert(fullName, pkColumns, entry, conditions) {
870
905
  const row = withNodeId(entry.row ?? {}, entry);
871
906
  const cols = Object.keys(row);
872
- if (cols.length === 0) return;
907
+ if (cols.length === 0) return 0;
908
+ if (conditions?.ifAbsent) {
909
+ requireInsertKey(entry, pkColumns);
910
+ return countCompiled(
911
+ this.db,
912
+ compiler.insertInto(fullName).values({ ...row }).onConflict((oc) => oc.columns(pkColumns).doNothing()).returning(WRITTEN).compile()
913
+ );
914
+ }
873
915
  const updateCols = cols.filter((c) => !pkColumns.includes(c));
874
916
  if (pkColumns.length === 0 || updateCols.length === 0) {
875
- runCompiled(
917
+ return countCompiled(
876
918
  this.db,
877
- compiler.insertInto(fullName).orIgnore().values({ ...row }).compile()
919
+ compiler.insertInto(fullName).orIgnore().values({ ...row }).returning(WRITTEN).compile()
878
920
  );
879
- return;
880
921
  }
881
- runCompiled(
922
+ return countCompiled(
882
923
  this.db,
883
924
  compiler.insertInto(fullName).values({ ...row }).onConflict(
884
925
  (oc) => oc.columns(pkColumns).doUpdateSet(
885
926
  (eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
886
927
  ).where(sql2.ref("excluded.updated_at"), ">", sql2.ref(`${fullName}.updated_at`))
887
- ).compile()
928
+ ).returning(WRITTEN).compile()
888
929
  );
889
930
  }
890
- applyUpdate(fullName, pkColumns, entry) {
931
+ applyUpdate(fullName, pkColumns, entry, conditions) {
891
932
  const rawPatch = entry.row ?? {};
892
933
  const patch = rawPatch["updated_at"] ? withNodeId(rawPatch, entry) : rawPatch;
893
934
  const where = requireKeyedWhere(entry, "update", pkColumns);
894
935
  const patchCols = Object.keys(patch);
895
936
  const whereCols = Object.keys(where);
896
- if (patchCols.length === 0) return;
937
+ if (patchCols.length === 0) return 0;
897
938
  const incomingUpdatedAt = patch["updated_at"];
898
939
  let query = compiler.updateTable(fullName).set({ ...patch });
899
940
  for (const c of whereCols) {
@@ -904,9 +945,13 @@ var SqliteAppSyncableApplier = class {
904
945
  (eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", incomingUpdatedAt)])
905
946
  );
906
947
  }
907
- runCompiled(this.db, query.compile());
948
+ query = query.where("deleted_at", "is", null);
949
+ if (conditions?.ifUpdatedAt !== void 0) {
950
+ query = query.where("updated_at", "=", conditions.ifUpdatedAt);
951
+ }
952
+ return countCompiled(this.db, query.returning(WRITTEN).compile());
908
953
  }
909
- applyDelete(fullName, pkColumns, entry) {
954
+ applyDelete(fullName, pkColumns, entry, conditions) {
910
955
  const where = requireKeyedWhere(entry, "delete", pkColumns);
911
956
  const whereCols = Object.keys(where);
912
957
  const incomingUpdatedAt = entry.row?.["updated_at"];
@@ -915,22 +960,25 @@ var SqliteAppSyncableApplier = class {
915
960
  if (carriesFullRow) {
916
961
  const row = withNodeId({ ...entry.row, updated_at: ts, deleted_at: ts }, entry);
917
962
  const updateCols = Object.keys(row).filter((c) => !pkColumns.includes(c));
918
- runCompiled(
963
+ return countCompiled(
919
964
  this.db,
920
965
  compiler.insertInto(fullName).values({ ...row }).onConflict(
921
966
  (oc) => oc.columns(pkColumns).doUpdateSet(
922
967
  (eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
923
968
  ).where(sql2.ref("excluded.updated_at"), ">", sql2.ref(`${fullName}.updated_at`))
924
- ).compile()
969
+ ).returning(WRITTEN).compile()
925
970
  );
926
- return;
927
971
  }
928
972
  let query = compiler.updateTable(fullName).set({ deleted_at: ts, updated_at: ts, node_id: nodeIdOf(ts, entry) });
929
973
  for (const c of whereCols) {
930
974
  query = query.where(c, "=", where[c]);
931
975
  }
932
976
  query = query.where((eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)]));
933
- runCompiled(this.db, query.compile());
977
+ query = query.where("deleted_at", "is", null);
978
+ if (conditions?.ifUpdatedAt !== void 0) {
979
+ query = query.where("updated_at", "=", conditions.ifUpdatedAt);
980
+ }
981
+ return countCompiled(this.db, query.returning(WRITTEN).compile());
934
982
  }
935
983
  /**
936
984
  * Whether this table exists in *this* database.