Using Tanstack DB without fetching the entire collection #469
Replies: 3 comments 10 replies
-
You would need apply filtering in the query function to ensure that the data that is fetched/synced from the server is only what is needed to display/work with for the use case. In some cases where data may only be hundreds of records, you can perhaps always fetch everything. In other cases, consider the logical boundaries the user will be working within for a particular use case. This could be the scope of a page, aggregate root, etc. |
Beta Was this translation helpful? Give feedback.
-
Right now it's as @craigsmitham says, you need your collections to fetch subsets of data — as large as makes sense. The plan is fairly soon to have APIs where this is fairly automatic — see #315 & #343 |
Beta Was this translation helpful? Give feedback.
-
Hello! I recently faced the same task. In the docs, all the examples show queryFn loading an entire collection, so it feels like TanStack DB is only meant for “load everything at once” scenarios. But how you get your data is completely up to you. In the current version, the DB doesn’t support parameterized queries yet and can run a live query only on the whole collection. I solved it in a different way: I combined React Query and TanStack DB. React Query handles network requests and parameters (page, number of items, filters), and the DB only stores and returns normalized records. For each new set of parameters I call useQuery, get the required portion of data from the server, and within one collection I clear the store and insert new items —“wipe & insert”, using writeBatch to delete old keys and insert new data. The collection is synchronized not through queryFn but directly from the React Query cache (queryClient.getQueryData). This approach lets you store data by ID, perform optimistic updates, and you don’t need to use a factory to create collections for every filter. And the DB stores only the current set of data, and React Query handles parameterized loading. Not every app needs to keep dozens of pages in memory at once, and separate collections for each filter can be an overhead. A rewritable collection, which you clear and refill when the parameters change, can be enough. You can find a more detailed explanation with code and optimistic updates here. I hope this pattern will be helpful! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! I really like the look of TanStack DB, but I'm pretty puzzled as to how you work with a collection where it's not feasible to fetch the entire collection at once. In working with an app like Notion, it isn't performant or feasible to sync all the data the user might access when the user loads the page. I can't figure out how that would work in TanStackDB, given the
queryFn
is designed to fetch the entire collectionHow would I use TanStackDB in a Notion-like app where the data the user has access vastly exceeds what's possible to download into their browser?
Beta Was this translation helpful? Give feedback.
All reactions