T
TanStack2mo ago
provincial-silver

Multiple mutations to the same collection

Does TanStack DB automatically handle multiple mutations like, for instance, an auto-save input field that sends a mutation onChange? Is it able to cancel previous mutations or at least only keep the latest successful result, or do we have to handle that in application code? If so, are there any examples or best practices? EDIT: Just saw this which looks promising https://github.com/TanStack/db/issues/35 - and makes me think the answer to my question is “no”. But that then raises the question, out of curiosity: how do people using the default behavior handle this seemingly ubiquitous case? It seems like any mutation, unless you disable the UI while it’s running (though that wouldn’t really happen if you use optimistic updates), would face race conditions, no? Take the following examples: 1. Adding a to-do: what if you add 3 to-dos very quickly, and the 2nd request completes faster for whatever reason (maybe the request ended up on a bad edge node?) - wouldn’t the to-dos in the UI jump around if you were sorting them by createdAt? 2. Incrementing an item’s count in your shopping cart: if you’re sending the request as current+1, you sent the request 3 times, and the 2nd request won the race, you’d end up with a bad result, no?
3 Replies
adverse-sapphire
adverse-sapphire2mo ago
There's a PR up for that issue that should be merged soon https://github.com/TanStack/db/pull/704
GitHub
feat: Add paced mutations with timing strategies by KyleAMathews ·...
Summary This PR introduces a new paced mutations system for TanStack DB, enabling optimistic mutations with pluggable timing strategies. This provides a powerful way to control when and how mutatio...
adverse-sapphire
adverse-sapphire2mo ago
For increment, you'd call api/increment. It's not safe to send specific state updates for stuff like this. You have the db do the increment
provincial-silver
provincial-silverOP2mo ago
heh good timing, thanks! that's exactly what i was looking for yep that increment was a poorly-thought out example, spent a whole day reasoning about distributed concurrent queries/mutations and my brain was fried. for some reason thought i'd want idempotency in that endpoint

Did you find this page helpful?