Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

collections-intro.mdx

69 lines3.3 KB

1import {Link} from '@tanstack/react-router'
2import {GHLink, HighLightComponent, ClearHighlightsButton} from "/src/components/tutorial"
3​
4## What are collections?
5​
6### A quick demo
7​
8We have a `projects` table in our database. If you click the button below, you can see all the components that to 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```ts
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/013849f87b6ce7ab5dee3873424d8e1012b810fc/src/components/AppSidebar.tsx#L45-L49" />
26​
27<HighLightComponent h_id="project_sidebar">Highlight `AppSidebar`</HighLightComponent>
28​
29On the project's page, the `RouteComponent` loads only the data relevant for the currently viewed project:
30​
31```ts
32 const {
33 data: [project],
34 } = useLiveQuery(
35 (q) =>
36 q
37 .from({ project: projectsCollection })
38 .where(({ project }) => eq(project.id, projectId)),
39 // This hook has a dependency array as seen below.
40 // Every time the `projectId` changes (i.e. we navigate
41 // to a new page), the query is executed again
42 [projectId],
43 );
44```
45<GHLink href="https://github.com/fulopkovacs/trytanstackdb.com/blob/013849f87b6ce7ab5dee3873424d8e1012b810fc/src/routes/_tempDbRequired.projects.$projectId.tsx#L19-L27" />
46​
47<HighLightComponent h_id="project_projectPage">Highlight `RouteComponent`</HighLightComponent>
48​
49### "So many queries... Isn't that ...slow?"
50​
51Thankfully, no, and there are two reasons why.
52​
531. Multiple queries using the same collection will sync the collection with the backend only once, and use the cache the rest of the times.
54 - To check it, follow these steps:
55 1. go to the `Network` tab in your browser's dev tools
56 2. reload the page (<a href="">click here</a>)
57 3. see that there is only one `GET` request to the `/api/projects` endpoint, despite having 2 components using data from the `projectCollection`
58 4. if you navigate to any other project page, you shouldn't see more requests made
59​
60 > Unless the cache needs to be revalidated (because the state has been mutated or went stale).
61​
62​
632. Live queries updated incrementally, (they use <a href="https://github.com/electric-sql/d2ts" target="_blank">`d2ts`</a>, a differential dataflow library)
64 - this results in _sub-millisecond_ execution times (even for complex queries)
65 - which means that it's BLAZING FAST!!! ⚡⚡⚡
66​
67### Querying multiple collections
68​
69Yes, 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.