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/react/README.md
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -391,3 +391,28 @@ function MyWidget() {
391
391
)
392
392
}
393
393
```
394
+
395
+
## Query Subscriptions
396
+
397
+
The `useWatchedQuerySubscription` hook lets you access the state of an externally managed `WatchedQuery` instance. Managing a query outside of a component enables in-memory caching and sharing of results between multiple subscribers. This reduces async loading time during component mount (thanks to in-memory caching) and minimizes the number of SQLite queries (by sharing results between multiple components).
398
+
399
+
```jsx
400
+
// The lifecycle of this query is managed outside of any individual React component.
401
+
// The data is kept up-to-date in the background and can be shared by multiple subscribers.
402
+
const listsQuery = powerSync.query({sql: 'SELECT * FROM lists'}).differentialWatch();
403
+
404
+
export const ContentComponent = () => {
405
+
// Subscribes to the `listsQuery` instance. The subscription is automatically
406
+
// cleaned up when the component unmounts. The `data` value always reflects
Copy file name to clipboardExpand all lines: packages/vue/README.md
+73-2Lines changed: 73 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -101,7 +101,78 @@ const addList = () => {
101
101
</template>
102
102
```
103
103
104
-
### Static query
104
+
### Incremental Query
105
+
106
+
By default, the `useQuery` composable emits a new `data` reference whenever any change occurs in the query's dependent tables. With incremental queries, updates are only emitted when relevant changes are detected, and the internal `data` array preserves object references for unchanged rows between emissions. This helps prevent unnecessary re-renders in your components.
107
+
108
+
````Vue
109
+
// TodoListDisplayQuery.vue
110
+
<script setup>
111
+
import { usePowerSync, useQuery } from '@powersync/vue';
<li v-for="l in lists" :key="l.id">{{ l.name }}</li>
130
+
</ul>
131
+
</template>
132
+
133
+
### Query Subscriptions
134
+
135
+
The `useWatchedQuerySubscription` composable lets you access the state of an externally managed `WatchedQuery` instance. To enable in-memory caching and sharing of results between multiple subscribers, the query should be created outside of any component—such as in a module or singleton. This reduces async loading time when components mount (thanks to in-memory caching) and minimizes the number of SQLite queries (by sharing results between components).
136
+
137
+
```js
138
+
// listsQuery.js
139
+
import { usePowerSync } from '@powersync/vue';
140
+
141
+
// This module creates and exports a single shared query instance.
142
+
// It should be imported wherever you want to subscribe to the query.
143
+
let listsQuery;
144
+
export function getListsQuery() {
145
+
if (!listsQuery) {
146
+
// Note: usePowerSync() must be called in a Vue setup context, so you may need to
147
+
// pass the PowerSync instance here if you are not using the default context.
148
+
const powerSync = usePowerSync();
149
+
listsQuery = powerSync.value.query({ sql: 'SELECT * FROM lists' }).differentialWatch();
150
+
}
151
+
return listsQuery;
152
+
}
153
+
```
154
+
155
+
```vue
156
+
<!-- ContentComponent.vue -->
157
+
<script setup>
158
+
import { useWatchedQuerySubscription } from '@powersync/vue';
159
+
import { getListsQuery } from './listsQuery';
160
+
161
+
// Subscribe to the shared `listsQuery` instance. The subscription is automatically
162
+
// cleaned up when the component unmounts. The `data` value always reflects
<li v-for="l in lists" :key="l.id">{{ l.name }} ({{ l.id }})</li>
170
+
</ul>
171
+
</template>
172
+
```
173
+
174
+
175
+
### Static Query
105
176
106
177
The `useQuery` composable can be configured to only execute initially and not every time changes to dependent tables are detected. The query can be manually re-executed by using the returned `refresh` function.
107
178
@@ -122,7 +193,7 @@ const { data: list, refresh } = useQuery('SELECT id, name FROM lists', [], {
0 commit comments