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:
I think I need something like this:
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.
JT3 Replies
stormy-gold•4d 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-coralOP•4d 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•3d ago
https://tanstack.com/blog/tanstack-db-0.5-query-driven-sync & https://tanstack.com/db/latest/docs/collections/query-collection#direct-writes
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...