@starkeep/storage-sqlite 0.8.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.
@@ -63,6 +63,13 @@ declare class SqliteDatabaseAdapter implements DatabaseAdapter {
63
63
  private getRow;
64
64
  private allRows;
65
65
  put(record: DataRecord): Promise<void>;
66
+ putSnapshot(record: DataRecord): Promise<boolean>;
67
+ /**
68
+ * One upsert statement. With `onlyIfNewer` an existing row is replaced only
69
+ * by a strictly later `updated_at` (see `DatabaseAdapter.putSnapshot`), and
70
+ * the result says whether a row was written.
71
+ */
72
+ private upsertRecord;
66
73
  get(id: StarkeepId): Promise<DataRecord | null>;
67
74
  delete(id: StarkeepId, hlc: HLCTimestamp): Promise<void>;
68
75
  getNodeWatermarks(): Promise<Record<string, HLCTimestamp>>;
@@ -102,7 +109,7 @@ declare class SqliteDatabaseAdapter implements DatabaseAdapter {
102
109
  * query draws.
103
110
  */
104
111
  findByLabel(query: FindByLabelQuery): Promise<FindByLabelResult>;
105
- putLabel(label: RecordLabel): Promise<void>;
112
+ putLabel(label: RecordLabel): Promise<boolean>;
106
113
  getLabel(recordId: StarkeepId, appId: string, key: string, value: string): Promise<RecordLabel | null>;
107
114
  queryLabels(query: {
108
115
  limit?: number;
@@ -63,6 +63,13 @@ declare class SqliteDatabaseAdapter implements DatabaseAdapter {
63
63
  private getRow;
64
64
  private allRows;
65
65
  put(record: DataRecord): Promise<void>;
66
+ putSnapshot(record: DataRecord): Promise<boolean>;
67
+ /**
68
+ * One upsert statement. With `onlyIfNewer` an existing row is replaced only
69
+ * by a strictly later `updated_at` (see `DatabaseAdapter.putSnapshot`), and
70
+ * the result says whether a row was written.
71
+ */
72
+ private upsertRecord;
66
73
  get(id: StarkeepId): Promise<DataRecord | null>;
67
74
  delete(id: StarkeepId, hlc: HLCTimestamp): Promise<void>;
68
75
  getNodeWatermarks(): Promise<Record<string, HLCTimestamp>>;
@@ -102,7 +109,7 @@ declare class SqliteDatabaseAdapter implements DatabaseAdapter {
102
109
  * query draws.
103
110
  */
104
111
  findByLabel(query: FindByLabelQuery): Promise<FindByLabelResult>;
105
- putLabel(label: RecordLabel): Promise<void>;
112
+ putLabel(label: RecordLabel): Promise<boolean>;
106
113
  getLabel(recordId: StarkeepId, appId: string, key: string, value: string): Promise<RecordLabel | null>;
107
114
  queryLabels(query: {
108
115
  limit?: number;
package/dist/index.cjs CHANGED
@@ -364,16 +364,32 @@ var SqliteDatabaseAdapter = class {
364
364
  return this.getDatabase().prepare(sql5).all(...params.map(toSqliteParam));
365
365
  }
366
366
  async put(record) {
367
+ this.upsertRecord(record, false);
368
+ }
369
+ async putSnapshot(record) {
370
+ return this.upsertRecord(record, true);
371
+ }
372
+ /**
373
+ * One upsert statement. With `onlyIfNewer` an existing row is replaced only
374
+ * by a strictly later `updated_at` (see `DatabaseAdapter.putSnapshot`), and
375
+ * the result says whether a row was written.
376
+ */
377
+ upsertRecord(record, onlyIfNewer) {
367
378
  const row = recordToRow(record);
368
379
  const updateColumns = Object.keys(row).filter((column) => column !== "id");
369
- const query = compiler.insertInto("shared_records").values({ ...row }).onConflict(
370
- (oc) => oc.column("id").doUpdateSet(
380
+ const query = compiler.insertInto("shared_records").values({ ...row }).onConflict((oc) => {
381
+ const update = oc.column("id").doUpdateSet(
371
382
  (eb) => Object.fromEntries(
372
383
  updateColumns.map((column) => [column, eb.ref(`excluded.${column}`)])
373
384
  )
374
- )
375
- ).compile();
376
- this.runStmt(query.sql, ...query.parameters);
385
+ );
386
+ return onlyIfNewer ? update.where(
387
+ import_kysely3.sql.ref("excluded.updated_at"),
388
+ ">",
389
+ import_kysely3.sql.ref("shared_records.updated_at")
390
+ ) : update;
391
+ }).returning("id").compile();
392
+ return this.allRows(query.sql, ...query.parameters).length > 0;
377
393
  }
378
394
  async get(id) {
379
395
  const query = compiler.selectFrom("shared_records").selectAll().where("id", "=", id).compile();
@@ -637,7 +653,7 @@ var SqliteDatabaseAdapter = class {
637
653
  // ---- Label sync ---------------------------------------------------------
638
654
  async putLabel(label) {
639
655
  const query = (0, import_storage_adapter4.buildLabelSnapshotUpsert)(compiler, LABELS, label);
640
- this.runStmt(query.sql, ...query.parameters);
656
+ return this.allRows(query.sql, ...query.parameters).length > 0;
641
657
  }
642
658
  async getLabel(recordId, appId, key, value) {
643
659
  const query = (0, import_storage_adapter4.buildGetLabel)(compiler, LABELS, recordId, appId, key, value);