Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/keep-original-on-update-delete.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 9 additions & 4 deletions packages/db/src/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -220,8 +220,13 @@ function mergePendingMutations<T extends object>(
}

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
Expand Down Expand Up @@ -554,7 +559,7 @@ class Transaction<T extends object = Record<string, unknown>> {
*
* - **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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions packages/db/tests/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Row, number>({
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
Expand Down