From a21c05a80c4c3ccf30349568833e08b3116190f5 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Thu, 19 Nov 2026 11:42:59 -0400 Subject: [PATCH] fix(db): keep the first original on a delete that follows an update Co-Authored-By: Claude Sonnet 5.5 --- .changeset/keep-original-on-update-delete.md | 5 +++ packages/db/src/transactions.ts | 13 ++++--- ...mistic-transaction-oracle.property.test.ts | 2 +- packages/db/tests/transactions.test.ts | 36 +++++++++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 .changeset/keep-original-on-update-delete.md diff --git a/.changeset/keep-original-on-update-delete.md b/.changeset/keep-original-on-update-delete.md new file mode 100644 index 0000000000..60711d4ca9 --- /dev/null +++ b/.changeset/keep-original-on-update-delete.md @@ -0,0 +1,5 @@ +--- +'@tanstack/db': patch +--- + +A `delete` that follows an `update` of the same row in one transaction now keeps the row's first `original` (the state before the transaction) instead of the already-updated row. diff --git a/packages/db/src/transactions.ts b/packages/db/src/transactions.ts index 0e882a9525..653d5a8fea 100644 --- a/packages/db/src/transactions.ts +++ b/packages/db/src/transactions.ts @@ -180,7 +180,7 @@ function getTransactionAmbientScope(transaction: object): TransactionScope { * Merge behavior truth table: * - (insert, update) → insert (merge changes, keep empty original) * - (insert, delete) → null (cancel both mutations) - * - (update, delete) → delete (delete dominates) + * - (update, delete) → delete (keep the first original) * - (update, update) → update (replace with latest, union changes) * - (delete, delete) → delete (replace with latest) * - (insert, insert) → insert (replace with latest) @@ -220,8 +220,13 @@ function mergePendingMutations( } case `delete`: - // An insert followed by a delete cancels. Otherwise the delete dominates. - return existing.type === `insert` ? null : incoming + // An insert followed by a delete cancels. + if (existing.type === `insert`) return null + // A delete after an update keeps the row as it was before the transaction. + if (existing.type === `update`) { + return { ...incoming, original: existing.original } + } + return incoming case `insert`: { if (existing.type === `insert`) return incoming @@ -554,7 +559,7 @@ class Transaction> { * * - **insert + update** → insert (merge changes, keep empty original) * - **insert + delete** → removed (mutations cancel each other out) - * - **update + delete** → delete (delete dominates) + * - **update + delete** → delete (keep the first original) * - **update + update** → update (union changes, keep first original) * - **delete + insert** → removed if restored, otherwise update * - **same type** → replace with latest diff --git a/packages/db/tests/optimistic-transaction-oracle.property.test.ts b/packages/db/tests/optimistic-transaction-oracle.property.test.ts index 00d1b1d041..7550236e71 100644 --- a/packages/db/tests/optimistic-transaction-oracle.property.test.ts +++ b/packages/db/tests/optimistic-transaction-oracle.property.test.ts @@ -219,7 +219,7 @@ async function runSameKey( : [ { type: endsAbsent ? `delete` : startsAbsent ? `insert` : `update`, - original: startsAbsent ? {} : endsAbsent ? first : original, + original: startsAbsent ? {} : original, modified: endsAbsent ? first : final, changes: endsAbsent ? first diff --git a/packages/db/tests/transactions.test.ts b/packages/db/tests/transactions.test.ts index 14ca0f1a1f..9ca3f3ba89 100644 --- a/packages/db/tests/transactions.test.ts +++ b/packages/db/tests/transactions.test.ts @@ -939,6 +939,42 @@ describe(`Transactions`, () => { expect(transaction4.state).toBe(`failed`) }) + it(`keeps the first original when an update is followed by a delete`, () => { + type Row = { id: number; a: string; b: string } + const collection = createCollection({ + getKey: (row) => row.id, + startSync: true, + sync: { + sync: ({ begin, write, commit, markReady }) => { + begin() + write({ type: `insert`, value: { id: 1, a: `a`, b: `b` } }) + commit() + markReady() + }, + }, + }) + const tx = createTransaction({ + autoCommit: false, + mutationFn: () => Promise.resolve(), + }) + tx.mutate(() => { + collection.update(1, (draft) => { + draft.a = `a2` + }) + }) + tx.mutate(() => { + collection.delete(1) + }) + expect(tx.mutations).toHaveLength(1) + const [mutation] = tx.mutations + expect(mutation!.type).toBe(`delete`) + expect(mutation!.original).toEqual({ id: 1, a: `a`, b: `b` }) + expect(mutation!.modified).toEqual({ id: 1, a: `a2`, b: `b` }) + expect(mutation!.changes).toEqual({ id: 1, a: `a2`, b: `b` }) + tx.rollback() + tx.when(`settled`).catch(() => {}) + }) + describe(`duplicate instance detection`, () => { it(`sets a global marker in dev mode when in browser top window`, () => { // The duplicate instance marker is set when the module loads in dev mode