unforgit 0.11.12 → 0.11.14

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.
@@ -2083,6 +2083,27 @@ var RemoteClient = class {
2083
2083
  syncedAt: tombstone.syncedAt ? new Date(tombstone.syncedAt) : void 0
2084
2084
  }));
2085
2085
  }
2086
+ async pushTombstone(tombstone) {
2087
+ const res = await this.fetchWithRetry(
2088
+ `${this.baseUrl}/v1/sync/tombstones`,
2089
+ {
2090
+ method: "POST",
2091
+ headers: this.getHeaders(),
2092
+ body: JSON.stringify({
2093
+ memoryId: tombstone.memoryId,
2094
+ orgId: tombstone.orgId,
2095
+ repoId: tombstone.repoId,
2096
+ deletedAt: tombstone.deletedAt.toISOString(),
2097
+ deletedBy: tombstone.deletedBy
2098
+ })
2099
+ },
2100
+ "pushTombstone"
2101
+ );
2102
+ if (!res.ok) {
2103
+ this.handleError(res, "pushTombstone", await res.text());
2104
+ }
2105
+ return res.json();
2106
+ }
2086
2107
  async deprecate(id, reason) {
2087
2108
  const res = await this.fetchWithRetry(
2088
2109
  `${this.baseUrl}/v1/memory/${pathSegment(id)}/deprecate`,
@@ -3387,14 +3408,34 @@ var LocalStore = class {
3387
3408
  ).all(orgId, repoId);
3388
3409
  return rows.map(rowToTombstone);
3389
3410
  }
3390
- markTombstoneSynced(memoryId) {
3411
+ markTombstoneSynced(memoryId, expectedTombstoneId) {
3391
3412
  const now = (/* @__PURE__ */ new Date()).toISOString();
3392
- const result = this.db.prepare("UPDATE tombstones SET synced_at = ? WHERE memory_id = ?").run(now, memoryId);
3413
+ const result = expectedTombstoneId ? this.db.prepare(
3414
+ "UPDATE tombstones SET synced_at = ? WHERE memory_id = ? AND id = ? AND synced_at IS NULL"
3415
+ ).run(now, memoryId, expectedTombstoneId) : this.db.prepare("UPDATE tombstones SET synced_at = ? WHERE memory_id = ?").run(now, memoryId);
3393
3416
  return result.changes > 0;
3394
3417
  }
3395
3418
  applyTombstone(tombstone) {
3396
- const memory = this.getById(tombstone.memoryId);
3397
- if (!memory) {
3419
+ const now = (/* @__PURE__ */ new Date()).toISOString();
3420
+ const transaction = this.db.transaction(() => {
3421
+ const existingRow = this.db.prepare("SELECT * FROM tombstones WHERE memory_id = ?").get(tombstone.memoryId);
3422
+ const existingTombstone = existingRow ? rowToTombstone(existingRow) : void 0;
3423
+ if (existingTombstone && !existingTombstone.syncedAt && existingTombstone.deletedAt > tombstone.deletedAt) {
3424
+ return false;
3425
+ }
3426
+ const memory = this.getById(tombstone.memoryId);
3427
+ if (memory && memory.status !== "deleted") {
3428
+ this.db.prepare(
3429
+ `UPDATE memories
3430
+ SET status = 'deleted', deleted_at = ?, deleted_by = ?, version = version + 1, updated_at = ?
3431
+ WHERE id = ?`
3432
+ ).run(
3433
+ tombstone.deletedAt.toISOString(),
3434
+ tombstone.deletedBy ?? null,
3435
+ now,
3436
+ tombstone.memoryId
3437
+ );
3438
+ }
3398
3439
  this.db.prepare(
3399
3440
  `INSERT OR REPLACE INTO tombstones (id, memory_id, org_id, repo_id, deleted_at, deleted_by, synced_at, created_at)
3400
3441
  VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
@@ -3405,15 +3446,12 @@ var LocalStore = class {
3405
3446
  tombstone.repoId,
3406
3447
  tombstone.deletedAt.toISOString(),
3407
3448
  tombstone.deletedBy ?? null,
3408
- (/* @__PURE__ */ new Date()).toISOString(),
3409
- (/* @__PURE__ */ new Date()).toISOString()
3449
+ now,
3450
+ now
3410
3451
  );
3411
3452
  return true;
3412
- }
3413
- return this.softDelete({
3414
- id: tombstone.memoryId,
3415
- deletedBy: tombstone.deletedBy
3416
3453
  });
3454
+ return transaction.immediate();
3417
3455
  }
3418
3456
  incrementVersion(id) {
3419
3457
  const now = (/* @__PURE__ */ new Date()).toISOString();
@@ -4467,4 +4505,4 @@ export {
4467
4505
  RemoteClient,
4468
4506
  LocalStore
4469
4507
  };
4470
- //# sourceMappingURL=chunk-B3ZM62FV.js.map
4508
+ //# sourceMappingURL=chunk-IOHA5JJV.js.map