Skip to content
Source
Sign inSign up
TanStack/db

fix(db): compose optimistic updates from changes instead of whole-row snapshots #1701

Closedmarbemac(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/1701/headopened

Showing 3 changed files with 47 additions and 2 deletions (loading 1).

x
0 of 3 viewed, 3 left
9 additions and 0 deletions.changeset/soft-donkeys-shave.mdAdded
View file
@@ -0,0 +1,9 @@
+---
+'@tanstack/db': patch
+---
+​
+Fix concurrent optimistic updates to the same row overwriting each other's fields.
+​
+Each pending update was stored as a whole-row snapshot taken at `mutate()` time, so whichever transaction applied last set every field — including fields it never touched. The clearest symptom: rolling back one of several in-flight transactions left its change visible, restored from a sibling transaction's snapshot.
+​
+Updates now apply only the top-level fields they actually changed. Inserts are unaffected — they still apply the whole row, because an insert's `changes` omit schema defaults.
38 additions and 2 deletionspackages/db/src/collection/state.tsModified
View file
@@ -13,6 +13,7 @@
ChangeMessage,
CollectionConfig,
OptimisticChangeMessage,
+ PendingMutation,
} from '../types'
import type { CollectionImpl } from './index.js'
import type { CollectionLifecycleManager } from './lifecycle'
@@ -103,6 +104,11 @@
public pendingLocalChanges = new Set<TKey>()
public pendingLocalOrigins = new Set<TKey>()