You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/data/deep-dives/query-driven-sync.mdx
+36-16Lines changed: 36 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,14 +8,11 @@ import {
8
8
9
9
## Query-driven sync
10
10
11
-
Sometimes you don't want to load all your data into your collection, only the subset that is required by the live queries you're running.
11
+
Sometimes you don't want to load all your data into your collection (e.g. when you have a lot of data), only the subset required by the live queries you're running.
12
12
13
-
For example:
13
+
In this demo app, some projects have many more todo items than others. Currently, we load all todo items regardless of which project page the user visits. But what if we could load only items from the currently viewed project?
14
14
15
-
- instead of loading all the todo items a user has when they visit a project's page,
16
-
- we can just load the ones that belong to that project.
17
-
18
-
This can be achieved by using the `on-demand` sync mode.
15
+
Well, this is what the `on-demand` sync mode is for.
19
16
20
17
> FYI: the docs recommend it for large datasets (>50k rows), which means that using it is not really warranted in our case (we have a bit more than 1k rows). <atarget="_blank"href="https://tanstack.com/db/latest/docs/overview#sync-modes">(source)</a>
Using this `queryFn` for the `"on-demand"``syncMode` would defeat the very purpose of it, because it would fetch every single todo item in the database for each query.
56
53
57
-
Instead of that, we want the collection to load all the todo items that are required for the queries we run. Right now, we have only one query for the `todoItemsCollection`:
54
+
Instead, we want the collection to load all the todo items that are required for the queries we run. Right now, we have only one query for the `todoItemsCollection`:
The highlighted line in the code block above is the filter we need to use in the database query on the server. Luckily for us, when this live query runs, TanStack DB automatically passes down all the predicates coming from this live query to the `queryFn`.
70
+
The highlighted line in the code block above is the filter we need to use in the database query on the server. Fortunately, when this live query runs, TanStack DB automatically passes all query predicates to the `queryFn`.
In this app we stop here, but the predicates (`where` clauses, `orderBy`, `limit`, and `offset`) coming from the queries can cover far more advanced use cases.
134
+
In this app, we'll stop here, but the predicates (`where` clauses, `orderBy`, `limit`, and `offset`) coming from the queries can cover far more advanced use cases.
115
135
116
-
We could use `orderBy` to order elements on the server, `offset` to support pagination and more.
136
+
We could use `orderBy` to order elements on the server, `offset` to support pagination, and more.
117
137
118
138
Check out the <a target="_blank" href="https://tanstack.com/db/latest/docs/collections/query-collection#queryfn-and-predicate-push-down">documentation</a> for more info about them.
119
139
120
140
### Transitioning from `"eager"` to `"on-demand"`: some pitfalls
121
141
122
-
When I first refactor the`todoItemsCollection` to use `on-demand` sync, I ran into some issues.
142
+
When I first refactored`todoItemsCollection` to use `on-demand` sync, I ran into some issues.
123
143
124
144
#### `.toArrayWhenReady()` vs `.toArray`
125
145
126
146
For example, this line of code calling the <a target="_blank" href="https://tanstack.com/db/latest/docs/reference/interfaces/Collection#toarraywhenready">`.toArrayWhenReady()` method</a> caused the collection to fetch every single todo item from the remote source:
127
147
128
148
```ts
129
-
// ❌ Get the current state of the collection from the server,
149
+
// ❌ Get the current state of the collection from the server
130
150
awaittodoItemsCollection.toArrayWhenReady()
131
151
```
132
152
133
-
I had to change it to <a target="_blank" href="https://tanstack.com/db/latest/docs/reference/interfaces/Collection#toarray">`toArray`</a>, to use only the data that we already have in the client-side cache:
153
+
I had to change it to <a target="_blank" href="https://tanstack.com/db/latest/docs/reference/interfaces/Collection#toarray">`toArray`</a>, to use only existing data in the client-side cache:
134
154
135
155
```ts
136
156
// ✅ Get the current state of the collection from the cache
@@ -146,11 +166,11 @@ Another line that resulted in loading all the collection data instead of the sub
146
166
todoItemsCollection.utils.refetch();
147
167
```
148
168
149
-
The moral of the story is that you have to watch out and make sure you don't accidentally sync all the data in the collection when you use `"on-demand"` mode.
169
+
The key takeaway is that you have to watch out and ensure you don't accidentally sync all the data in the collection when you use `"on-demand"` mode.
150
170
151
171
### Progressive mode
152
172
153
-
There is a third sync mode, that I haven't mentioned before called `"progressive"`. Here's how it relates to the other two:
173
+
There is a third sync mode that I haven't previously mentioned called `"progressive"`. Here's how it relates to the other two:
0 commit comments