Skip to content

Commit fea2fb9

Browse files
committed
chore: add changeset for object field update rollback fix
1 parent 073c114 commit fea2fb9

2 files changed

Lines changed: 9 additions & 14 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@tanstack/query-db-collection": patch
3+
"@tanstack/db": patch
4+
---
5+
6+
fix(query-db-collection): use deep equality for object field comparison in query observer
7+
8+
Fixed an issue where updating object fields (non-primitives) with `refetch: false` in `onUpdate` handlers would cause the value to rollback to the previous state every other update. The query observer was using shallow equality (`===`) to compare items, which compares object properties by reference rather than by value. This caused the observer to incorrectly detect differences and write stale data back to syncedData. Now uses `deepEquals` for proper value comparison.

packages/query-db-collection/src/query.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -800,27 +800,14 @@ export function queryCollectionOptions(
800800

801801
begin()
802802

803-
// Helper function for deep equality check of objects
804-
const itemsEqual = (
805-
obj1: Record<string, any>,
806-
obj2: Record<string, any>
807-
): boolean => {
808-
return deepEquals(obj1, obj2)
809-
}
810-
811803
currentSyncedItems.forEach((oldItem, key) => {
812804
const newItem = newItemsMap.get(key)
813805
if (!newItem) {
814806
const needToRemove = removeRow(key, hashedQueryKey) // returns true if the row is no longer referenced by any queries
815807
if (needToRemove) {
816808
write({ type: `delete`, value: oldItem })
817809
}
818-
} else if (
819-
!itemsEqual(
820-
oldItem as Record<string, any>,
821-
newItem as Record<string, any>
822-
)
823-
) {
810+
} else if (!deepEquals(oldItem, newItem)) {
824811
// Only update if there are actual differences in the properties
825812
write({ type: `update`, value: newItem })
826813
}

0 commit comments

Comments
 (0)