T
TanStack4d ago
typical-coral

Partial collection updates

Hi, I would like to do partial collection updates for a subset of records. For example, I only want to refetch from the server the records with a foreignKeyId of 'xyz' which would subsequently update/insert the corresponding records in the collection. Of the 100 records in the collection, this might only fetch a handful of records. For example, I want something like myCollection.utils.refetch({ foreignKeyId: 'xyz' }); My current query function is like this:
queryFn: async () => getUsersForSessionsOwnedByUserServerFn(),
queryFn: async () => getUsersForSessionsOwnedByUserServerFn(),
I think I need something like this:
// foreignKeyId might be null or a string
queryFn: async (foreignKeyId) => getUsersForSessionsOwnedByUserServerFn({ foreignKeyId }),
// foreignKeyId might be null or a string
queryFn: async (foreignKeyId) => getUsersForSessionsOwnedByUserServerFn({ foreignKeyId }),
This would then update the records where the id matches. Does this make sense? I know this is not the correct way but I think it's possible. What is the correct way to do this with tanstack DB? Any help would be appreciated. Thank you. JT
3 Replies
stormy-gold
stormy-gold4d ago
Your options basically are the direct writes API where you can load data out of your queryFn and then write it in. Or with the new query-driven sync, you can just create a query and call .preload() on it and it'll ensure the needed data is loaded
typical-coral
typical-coralOP4d ago
Thanks Kyle, Is there a reference to this? Maybe an example? Maybe the direct write is what I am going to have to use.
stormy-gold
stormy-gold3d ago
TanStack DB 0.5 — Query-Driven Sync | TanStack Blog
You don't need a new API for every component. With 0.5, the component's query is the API call. tsx // Your component's query... const { data: projectTodos } = useLiveQuery((q) = q .from({ todos }) .jo...
Query Collection | TanStack DB Docs
Query Collection Query collections provide seamless integration between TanStack DB and TanStack Query, enabling automatic synchronization between your local database and remote data sources. Overview...

Did you find this page helpful?