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: packages/powersync-db-collection/sync-stream-examples.md
+62-14Lines changed: 62 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,31 @@
1
-
## Calling sync streams automatically
1
+
## Incorporating Sync Streams
2
2
3
+
Ideally we would be able to map TanstackDB queries to sync streams automatically, if we can optimise the amount of data sync to
4
+
the sqlite database from the service we have smaller set of data that needs to be considered when syncing from the sqlite database to TanstackDB collections.
5
+
6
+
As a stepping stone towards that, we now expose data loading hooks for both eager and on-demand sync modes that allow a user to call sync streams when a collection is defined (eager mode) or when a collection's data boundary changes based on the live queries predicates (on-demand).
3
7
For the these examples we assuming the follow sync stream exists:
4
8
5
9
```
10
+
config:
11
+
edition: 3
12
+
6
13
streams:
7
14
lists:
8
15
query: SELECT * FROM lists WHERE owner_id = auth.user_id()
9
16
auto_subscribe: true
10
17
todos:
11
18
query: SELECT * FROM todos WHERE list_id = subscription.parameter('list') AND list_id IN (SELECT id FROM lists WHERE owner_id = auth.user_id())
12
-
13
-
config:
14
-
edition: 2
15
19
```
16
20
17
21
### Example 1: Eager mode basic usage
18
22
23
+
If you want an eager collection to subscribe to a sync stream when a collection loads, you can use the `onLoad` hook.
24
+
The hook may return a cleanup function.
25
+
26
+
Consider the diagram as an example.
27
+
We start with 4 todos in the PS service, only 2 todos get synced via the sync stream to the SQLite database. Because it's eager mode, both get synced from the SQLite database to the collection. Finally the TanstackDB query only returns the single todo that matches the live query predicate.
If you want to on-demand collection to subscribe to a sync stream whenever a subset of data is loaded (when the list of live queries against the collection change), you can use the `onLoadSubset` hook.
70
+
The hook may return a cleanup function.
71
+
72
+
Consider the diagram as an example.
73
+
We start with 4 todos in the PS service, only 2 todos get synced via the sync stream to the SQLite database. Because it's on-demand mode, only 1 todo matches gets synced from the SQLite database to the collection. Finally the TanstackDB query only returns the single todo that matches the live query predicate.
### Example 3: Extract a single filter value using `extractSimpleComparisons`
68
114
69
115
Given a live query like:
@@ -72,9 +118,12 @@ Given a live query like:
72
118
.where(({ todo }) => eq(todo.list_id, selectedListId))
73
119
```
74
120
75
-
`onLoadSubset` receives options.where as an expression tree for eq(list_id, '<uuid>').
121
+
`onLoadSubset` receives options.where as an expression tree `for eq(list_id, '<uuid>')`.
76
122
We parse it to get the `list_id` value and pass it to `syncStream`.
77
123
124
+
Consider the diagram as an example. Note it differs from example 1 and 2 as it aims to illustrate `extractSimpleComparisons`.
125
+
We start with 4 todos in the PS service, the sync stream subscription criteria (`list_id = "list_1"`) is derived from the live query registered against the collection. Only 2 todos get synced via the sync stream to the SQLite database. Two todos get synced from the SQLite database to the collection. Finally the TanstackDB query returns both todos as they both match `eq(todo.list_id, 'list_id')`.
({ todo }) =>eq(todo.list_id, '368b41f1-72fd-4a81-92ad-190711d72435'), // or some listId variable
177
+
({ todo }) =>eq(todo.list_id, 'list_id'), // or some listId variable
129
178
)
130
179
.select(({ todo }) => ({
131
180
id: todo.id,
132
-
description: todo.description,
133
181
completed: todo.completed,
134
182
})),
135
183
})
@@ -149,6 +197,9 @@ todos:
149
197
150
198
Note: We keep the `list` parameter name as is (consistent with most of our examples), but to correctly work with the following example we need to map it to `list_id`. You may opt to name it as `list_id` in the sync stream definition and skip the mapping process.
151
199
200
+
Consider the diagram as an example.
201
+
We start with 4 todos in the PS service, the sync stream subscription criteria (`list_id = "list_1" and completed = 1`) is derived from the live query registered against the collection. Only 1 todo gets synced via the sync stream to the SQLite database. One todos gets synced from the SQLite database to the collection. Finally the TanstackDB query returns 1 todo that matches `eq(todo.list_id, 'list_id') and eq(todo.completed, 1)`.
0 commit comments