Skip to content

Commit 5aebbac

Browse files
KyleAMathewsclaude
andauthored
Make clear in mutation doc that using other ways of writing to backend is ok (#750)
* docs: Add callout about bypassing mutation system Users can completely bypass the TanStack DB mutation system and use their existing mutation logic by calling backend APIs directly and either waiting for sync or manually refetching collections. * docs: Improve bypass mutations callout - Clarify this is about not rewriting existing logic, not avoiding optimistic updates - Add Electric's awaitTxId as a sync option - Show collection-specific approaches (QueryCollection refetch vs Electric awaitTxId) - Provide examples for both patterns * docs: Move bypass mutations to Mutation Approaches section Present bypassing the mutation system as one of several valid approaches rather than an aggressive callout. Fits more naturally as the first option under Mutation Approaches. * docs: Move bypass mutations to last approach Present TanStack DB's mutation approaches first, with bypassing the mutation system as the final option for those who prefer their existing patterns. * docs: Remove createTransaction from Mutation Approaches Manual transactions is niche and covered in detail later in its own section. Keep Mutation Approaches focused on the main patterns. * docs: Clarify why to await sync in bypass approach Explain that awaiting refetch/sync lets you know when the server change is loaded in the collection, so you can render new data, hide loading indicators, navigate, etc. * docs: Add success messages to sync completion reasons Include showing success messages as a common reason to await sync completion, alongside rendering data, hiding loaders, and navigating. * docs: Explain write-then-sync pattern in bypass approach Clarify that you write to the server like normal, then use your collection's mechanism to await the server write and know when to update UI. --------- Co-authored-by: Claude <[email protected]>
1 parent 4309abd commit 5aebbac

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

docs/guides/mutations.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ onUpdate: async ({ transaction }) => {
160160

161161
### Intent-Based Mutations with Custom Actions
162162

163-
For more complex scenarios, use `createOptimisticAction` or `createTransaction` to create **intent-based mutations** that capture specific user actions.
163+
For more complex scenarios, use `createOptimisticAction` to create **intent-based mutations** that capture specific user actions.
164164

165165
```tsx
166166
// Intent: "like this post"
@@ -197,7 +197,45 @@ Custom actions provide the cleanest way to capture specific types of mutations a
197197

198198
- **Collection-level mutations** (`collection.update`): Simple CRUD operations on a single collection
199199
- **`createOptimisticAction`**: Intent-based operations, multi-collection mutations, immediately committed
200-
- **`createTransaction`**: Fully custom transactions, delayed commits, multi-step workflows
200+
- **Bypass the mutation system**: Use your existing mutation logic without rewriting
201+
202+
### Bypass the Mutation System
203+
204+
If you already have mutation logic in an existing system and don't want to rewrite it, you can **completely bypass** TanStack DB's mutation system and use your existing patterns.
205+
206+
With this approach, you write to the server like normal using your existing logic, then use your collection's mechanism for refetching or syncing data to await the server write. After the sync completes, the collection will have the updated server data and you can render the new state, hide loading indicators, show success messages, navigate to a new page, etc.
207+
208+
```tsx
209+
// Call your backend directly with your existing logic
210+
const handleUpdateTodo = async (todoId, changes) => {
211+
await api.todos.update(todoId, changes)
212+
213+
// Wait for the server change to load into the collection
214+
await todoCollection.utils.refetch()
215+
216+
// Now you know the new data is loaded and can render it or hide loaders
217+
}
218+
219+
// With Electric
220+
const handleUpdateTodo = async (todoId, changes) => {
221+
const { txid } = await api.todos.update(todoId, changes)
222+
223+
// Wait for this specific transaction to sync into the collection
224+
await todoCollection.utils.awaitTxId(txid)
225+
226+
// Now the server change is loaded and you can update UI accordingly
227+
}
228+
```
229+
230+
Use this approach when:
231+
- You have existing mutation logic you don't want to rewrite
232+
- You're comfortable with your current mutation patterns
233+
- You want to use TanStack DB only for queries and state management
234+
235+
How to sync changes back:
236+
- **QueryCollection**: Manually refetch with `collection.utils.refetch()` to reload data from the server
237+
- **ElectricCollection**: Use `collection.utils.awaitTxId(txid)` to wait for a specific transaction to sync
238+
- **Other sync systems**: Wait for your sync mechanism to update the collection
201239

202240
## Mutation Lifecycle
203241

0 commit comments

Comments
 (0)