Pattern: Populating a collection "as needed" with multiple Queries throughout app - no "base" query.
This thread explores using various
useQuery
hooks throughout the app to "feed" our collections, only adding records as they're called for throughout the app.
We don't want to force the user to load entire db tables since 95% of the time they will only use a small % of them. We do want to progressively build up a store and use it's querying methods to performantly select from the data that has been fetched.
As of 7/31 we have "manual sync update methods" per this issue: https://github.com/TanStack/db/issues/294
Though I'm not certain that quite addresses our goal, as it still seems to require the collection to be build on a default query. Within our app, we can't be certain which query will be called upon first to begin populating a given collection.
In theory I'd like to do this:
...with the hope that it would update any records with a matching key (unique ID), and insert any new records.
I'm already seeing a few related questions/issues/discord messages around a similar topic, so hopefully this thread can be a good centralized place to discuss the optimal approach for this. Pinging @Kyle Mathews since he helpfully pointed out the manual sync update.GitHub
Add Manual Sync Updates API to @tanstack/query-db-collection · Iss...
Background The @tanstack/query-db-collection package integrates TanStack Query with TanStack DB collections, providing automatic synchronization between query results and collection state. Currentl...
24 Replies
skilled-lime•4w ago
if you want to lazily load data as needed — check out and comment on these proposals https://github.com/TanStack/db/issues/343 & https://github.com/TanStack/db/issues/315
GitHub
Paginated / Infinite Collections · Issue #343 · TanStack/db
A common request we are receiving is to lazily load data into a "query collection" using the infinite query pattern. We need to consider how to support this in a way that is then useable ...
GitHub
Partitioned collections · Issue #315 · TanStack/db
A very common use case, and question, is how to handle collections where you don't want to download all of it. Such as issues in an issue tracker, downloading by project/status/createdData etc....
ambitious-aquaOP•4w ago
Reading them now. Also you mentioned a "derived" collection that joins the query cache of multiple queries. That sounds like it could work, but I'm not seeing anything in the docs about derived collections or how to build them?
skilled-lime•4w ago
also you don't want to manually dispatch stuff like that as a normal course
queries are derived collections
skilled-lime•4w ago
Overview | TanStack DB Docs
TanStack DB Documentation Welcome to the TanStack DB documentation. TanStack DB is a reactive client store for building super fast apps on sync. It extends TanStack Query with collections, live querie...
ambitious-aquaOP•4w ago
Oh, I was thinking react query queries, not live queries.
skilled-lime•4w ago
so
ambitious-aquaOP•4w ago
So that shows deriving from imported collection instances, not react query queries - does that mean that I should replace my useQuery hooks with their own
createCollection( queryCollectionOptions({}) )
and then use the pattern you just shared to extract the relevant data types from each of them?skilled-lime•4w ago
yup! Just convert the response into a flat array & w/ a type field of some sort and then you can easily spilt them into their own collections
ambitious-aquaOP•4w ago
So when multiple collections have a given ID, presumably I'll need to write custom logic so it knows how to reconcile which one should win? (probably just comparing "modifiedAt" in most cases)
skilled-lime•4w ago
what collections are you talking about?
ambitious-aquaOP•4w ago
Let's say I have three queries throughout the app that all fetch datasets that include Dispatch records, and though their filtersets are unique, they likely all contain some overlap - same database rows, but fetched at different times. The derived collection would be trying to combine all Dispatch records from all three query collections, and would see the same unique ID up to three times.
skilled-lime•4w ago
ah ok, then you could do a full outer join then to merge the overlapping collections
ambitious-aquaOP•4w ago
Hmm, is there a way to create a queryCollectionOptions with a dynamic param like date? Right now both
enabled
and queryKey
can't access date.
skilled-lime•4w ago
Make a factory function?
ambitious-aquaOP•4w ago
Wouldn't that mean that the collections are destroyed when the component that calls them for a given date are un-mounted? Edit: Apparently not. AI studio has informed me that I am dumb.
So is this the right idea? (aside from the goofy short gcTime)
Then use it like this:
Then to create a unified live query, we'd need to manage the generated collections in global state, right? eg.
and THEN unify with a live query:
Then finally create/use it:
Though that's a lot of hoops to jump through just to get to use the querying/selector capability of db.
To avoid the factories, I'm creating a "default" query for each table that gets the most common recent records - they'll all run at launch. Then I'll use the new utils.syncUpsert function to add the "as needed" results from my various useQuery hooks.
skilled-lime•4w ago
Not at computer still hard to give this a close read -- but yeah just directly writing out could definitely be easier
Using a weakmap is probably easier than doing manual cleanup
Once nothing is using it it'd get GCed
ambitious-aquaOP•4w ago
I commented with an attempt at implementing the WeakMap on the partitioned collections issue: https://github.com/TanStack/db/issues/315#issuecomment-3145541811
GitHub
Partitioned collections · Issue #315 · TanStack/db
A very common use case, and question, is how to handle collections where you don't want to download all of it. Such as issues in an issue tracker, downloading by project/status/createdData etc....
skilled-lime•4w ago
Nice! Yeah that looks right the pattern you want
ambitious-aquaOP•4w ago
Is it correct to interpret the lack of examples in the docs as "we really want to nudge you in the direction of downloading it all to the client and selecting client-side"?
skilled-lime•4w ago
no just working on official patterns — see https://github.com/TanStack/db/issues/315 & https://github.com/TanStack/db/issues/343
GitHub
Partitioned collections · Issue #315 · TanStack/db
A very common use case, and question, is how to handle collections where you don't want to download all of it. Such as issues in an issue tracker, downloading by project/status/createdData etc....
GitHub
Paginated / Infinite Collections · Issue #343 · TanStack/db
A common request we are receiving is to lazily load data into a "query collection" using the infinite query pattern. We need to consider how to support this in a way that is then useable ...
ambitious-aquaOP•4w ago
Yeah, I see that the desire for the functionality is acknowledged in those. I'm more trying to put my brain in the frame of mind as the devs/creators to fully grasp the intent of how and why it's been designed the way that it has.
skilled-lime•4w ago
well it's just do one thing at a time 😆
ambitious-aquaOP•4w ago
in terms of expanding the library's features?
skilled-lime•4w ago
right
nothing is born fully formed