Open History to see the last commit
1import {Link} from '@tanstack/react-router'
2import {GHLink, HighLightComponent, OpenAPIRequestsPanelLink} from "/src/components/tutorial"
3
4## What are collections?
5
6### A quick demo
7
8Let's start with a quick demo and it'll all make sense. We have a `projects` table in our database. If you click the button below, you can see all the components that display data from this table:
9
10<HighLightComponent h_id="project">Highlight components</HighLightComponent>
11
12In a traditional React app, both of them would probably fetch the data they need from the server, managing their own cache. If you update something somewhere (e.g. rename a project), you need to manually update the cache, and after a while even knowing what to update becomes a challenge.
13
14TanStack DB provides you with a new option. You can store the projects' data in a collection, and use it as a single source of truth everywhere.
15
16In the `AppSidebar` component, we use this query to get the list of projects:
17
18```tsx
19 const { data: projects } = useLiveQuery((q) =>
20 q.from({
21 project: projectsCollection,
22 }),
23 );
24```
25<GHLink href="https://github.com/fulopkovacs/trytanstackdb.com/blob/5551f19041a217fd70ef406e9c96d09dc62d5eb1/src/components/AppSidebar.tsx#L31-L35" />
26
27<HighLightComponent h_id="project_sidebar">Highlight `AppSidebar`</HighLightComponent>
28
29On the project's page, the `EditableProjectDetails` component loads only the data relevant for the currently viewed project:
30
31```tsx
32 const {
33 data: [project],
34 } = useLiveQuery(
35 (q) => q
36 .from({ project: projectsCollection })
37 .where(({ project }) => eq(project.id, projectId)),
38 // This hook has a dependency array as seen below.
39 // Every time the `projectId` changes (i.e. we navigate
40 // to a new page), the query is executed again
41 [projectId],
42 );
43```
44<GHLink href="https://github.com/fulopkovacs/trytanstackdb.com/blob/5551f19041a217fd70ef406e9c96d09dc62d5eb1/src/components/EditableProjectDetails.tsx#L93-L104" />
45
46<HighLightComponent h_id="project_projectPage">Highlight `RouteComponent`</HighLightComponent>
47
48### "So many queries... Isn't that ...slow?"
49
50Thankfully, no, and there are two reasons why.
51
521. Multiple queries using the same collection will sync the collection with the backend only once, and use the cache the rest of the times.
53 - To check it, follow these steps:
54 1. open the _"API Requests"_ panel by clicking <OpenAPIRequestsPanelLink>here</OpenAPIRequestsPanelLink>
55 2. reload the page (<a href="">click here</a>)
56 3. see that there is only one `GET` request to the `/api/projects` endpoint, despite having 2 components using data from the `projectsCollection`
57 4. If you navigate to any other project page, you shouldn't see more requests made
58
59 > Unless the cache needs to be revalidated (because the state has been mutated or went stale).
60
61
622. Live queries are updated incrementally (they use <a href="https://github.com/electric-sql/d2ts" target="_blank">`d2ts`</a>, a differential dataflow library)
63 - this results in _sub-millisecond_ execution times (even for complex queries)
64 - which means that it's BLAZING FAST!!! ⚡⚡⚡
65
66### Querying multiple collections
67
68Yes, you can even use `innerJoin()` and other `join` operations to query multiple collections. This demo doesn't have an example for that yet, but you can <a href="https://tanstack.com/db/latest/docs/guides/live-queries#joins" target="_blank">check the documentation</a> if you're interested.