@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/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,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
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,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"]),
@@ -823,17 +833,22 @@ import {
823
833
  SQLITE_APP_QUERY_DIALECT as SQLITE_APP_QUERY_DIALECT2,
824
834
  buildBucketDigest as buildBucketDigest2,
825
835
  buildNodeWatermarks as buildNodeWatermarks2,
836
+ buildRowProbe,
837
+ classifySkippedWrite,
826
838
  buildScanSinceForNode as buildScanSinceForNode2,
827
839
  collectSince as collectSince2,
828
840
  planNodeScans as planNodeScans2,
841
+ probeKeyFor,
842
+ requireInsertKey,
829
843
  requireKeyedWhere,
830
844
  rowToWireEntry,
831
845
  toDigestBuckets as toDigestBuckets2,
832
846
  DEFAULT_BUCKET_PREFIX_LENGTH as DEFAULT_BUCKET_PREFIX_LENGTH2
833
847
  } from "@starkeep/storage-adapter";
834
- function runCompiled(db, compiled) {
835
- db.prepare(compiled.sql).run(...compiled.parameters.map(toSqliteParam));
848
+ function countCompiled(db, compiled) {
849
+ return allCompiled(db, compiled).length;
836
850
  }
851
+ var WRITTEN = sql2`1`.as("written");
837
852
  function allCompiled(db, compiled) {
838
853
  return db.prepare(compiled.sql).all(...compiled.parameters.map(toSqliteParam));
839
854
  }
@@ -844,7 +859,7 @@ var SqliteAppSyncableApplier = class {
844
859
  }
845
860
  db;
846
861
  namespace;
847
- apply(entry) {
862
+ apply(entry, conditions) {
848
863
  const ns = this.namespace.get(entry.appId);
849
864
  if (!ns) {
850
865
  throw new Error(`SqliteAppSyncableApplier: app "${entry.appId}" not installed`);
@@ -858,42 +873,52 @@ var SqliteAppSyncableApplier = class {
858
873
  requireWellFormedFileIndexRow(entry);
859
874
  const fullName = appSyncableTableName(entry.appId, entry.table);
860
875
  const { pkColumns } = tableInfo;
876
+ let changes;
861
877
  if (entry.op === "insert") {
862
- this.applyInsert(fullName, pkColumns, entry);
878
+ changes = this.applyInsert(fullName, pkColumns, entry, conditions);
863
879
  } else if (entry.op === "update") {
864
- this.applyUpdate(fullName, pkColumns, entry);
880
+ changes = this.applyUpdate(fullName, pkColumns, entry, conditions);
865
881
  } else {
866
- this.applyDelete(fullName, pkColumns, entry);
882
+ changes = this.applyDelete(fullName, pkColumns, entry, conditions);
867
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) };
868
887
  }
869
- applyInsert(fullName, pkColumns, entry) {
888
+ applyInsert(fullName, pkColumns, entry, conditions) {
870
889
  const row = withNodeId(entry.row ?? {}, entry);
871
890
  const cols = Object.keys(row);
872
- 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
+ }
873
899
  const updateCols = cols.filter((c) => !pkColumns.includes(c));
874
900
  if (pkColumns.length === 0 || updateCols.length === 0) {
875
- runCompiled(
901
+ return countCompiled(
876
902
  this.db,
877
- compiler.insertInto(fullName).orIgnore().values({ ...row }).compile()
903
+ compiler.insertInto(fullName).orIgnore().values({ ...row }).returning(WRITTEN).compile()
878
904
  );
879
- return;
880
905
  }
881
- runCompiled(
906
+ return countCompiled(
882
907
  this.db,
883
908
  compiler.insertInto(fullName).values({ ...row }).onConflict(
884
909
  (oc) => oc.columns(pkColumns).doUpdateSet(
885
910
  (eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
886
911
  ).where(sql2.ref("excluded.updated_at"), ">", sql2.ref(`${fullName}.updated_at`))
887
- ).compile()
912
+ ).returning(WRITTEN).compile()
888
913
  );
889
914
  }
890
- applyUpdate(fullName, pkColumns, entry) {
915
+ applyUpdate(fullName, pkColumns, entry, conditions) {
891
916
  const rawPatch = entry.row ?? {};
892
917
  const patch = rawPatch["updated_at"] ? withNodeId(rawPatch, entry) : rawPatch;
893
918
  const where = requireKeyedWhere(entry, "update", pkColumns);
894
919
  const patchCols = Object.keys(patch);
895
920
  const whereCols = Object.keys(where);
896
- if (patchCols.length === 0) return;
921
+ if (patchCols.length === 0) return 0;
897
922
  const incomingUpdatedAt = patch["updated_at"];
898
923
  let query = compiler.updateTable(fullName).set({ ...patch });
899
924
  for (const c of whereCols) {
@@ -904,9 +929,13 @@ var SqliteAppSyncableApplier = class {
904
929
  (eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", incomingUpdatedAt)])
905
930
  );
906
931
  }
907
- runCompiled(this.db, query.compile());
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());
908
937
  }
909
- applyDelete(fullName, pkColumns, entry) {
938
+ applyDelete(fullName, pkColumns, entry, conditions) {
910
939
  const where = requireKeyedWhere(entry, "delete", pkColumns);
911
940
  const whereCols = Object.keys(where);
912
941
  const incomingUpdatedAt = entry.row?.["updated_at"];
@@ -915,22 +944,25 @@ var SqliteAppSyncableApplier = class {
915
944
  if (carriesFullRow) {
916
945
  const row = withNodeId({ ...entry.row, updated_at: ts, deleted_at: ts }, entry);
917
946
  const updateCols = Object.keys(row).filter((c) => !pkColumns.includes(c));
918
- runCompiled(
947
+ return countCompiled(
919
948
  this.db,
920
949
  compiler.insertInto(fullName).values({ ...row }).onConflict(
921
950
  (oc) => oc.columns(pkColumns).doUpdateSet(
922
951
  (eb) => Object.fromEntries(updateCols.map((c) => [c, eb.ref(`excluded.${c}`)]))
923
952
  ).where(sql2.ref("excluded.updated_at"), ">", sql2.ref(`${fullName}.updated_at`))
924
- ).compile()
953
+ ).returning(WRITTEN).compile()
925
954
  );
926
- return;
927
955
  }
928
956
  let query = compiler.updateTable(fullName).set({ deleted_at: ts, updated_at: ts, node_id: nodeIdOf(ts, entry) });
929
957
  for (const c of whereCols) {
930
958
  query = query.where(c, "=", where[c]);
931
959
  }
932
960
  query = query.where((eb) => eb.or([eb("updated_at", "is", null), eb("updated_at", "<", ts)]));
933
- runCompiled(this.db, query.compile());
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());
934
966
  }
935
967
  /**
936
968
  * Whether this table exists in *this* database.