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
Implement useReactiveQuery hooks for React, Vue, and Angular (#93)
* Add useReactiveQuery for Angular
* Add useReactiveQuery for Vue
* Add useReactiveQuery for React
* Fix React version of useReactiveQuery
* Use SqlTag type in client too
* Add status to useReactiveQuery
* Document useReactiveQuery
* Set allowSignalWrites for Angular 17 and 18 compatibility
* Reset pending state when DB or query is changed
* Add new headline features
The query can be any SQL statement that reads one or more tables. It can be passed using a `sql` tag function available in the `reactiveQuery` callback that works similarly to the [`sql` tag function used for single queries](sql.md). It can also be a query built with Drizzle or Kysely; see the "Query Builders" section below.
32
+
The query can be any SQL statement that reads one or more tables. It can be passed using a `sql` tag function available in the `reactiveQuery` callback that works similarly to the [`sql` tag function used for single queries](sql.md). It can also be a query built with Drizzle or Kysely; see the "Query Builders" section [below](#query-builders).
33
33
34
34
You can then call `subscribe` on the object returned from `reactiveQuery` to register a callback that gets called an initial time and then again whenever the one or more of the queried tables are changed. The latest result data from the query will be passed as the first argument to your callback.
We also provide `useReactiveQuery` hook implementations to make it easier to integrate reactive queries with the reactivity systems of UI frameworks. The hook handles subscribing, returns reactive data, and automatically unsubscribes from the query when the component it's used in is destroyed.
92
+
93
+
`useReactiveQuery` takes your `SQLocal` instance and a SQL query as arguments. The query can be passed using the `sql` tag function or using a query builder as described [above](#query-builders). It returns an object containing the following reactive values:
94
+
95
+
-**`data`** (`Result[]`) - The result data from your SQL query.
96
+
-**`error`** (`Error | undefined`) - An `Error` object if the SQL query fails.
97
+
-**`status`** (`'pending' | 'error' | 'ok'`) - The string `'pending'` if the SQL query has not completed for the first time yet, `'error'` if the SQL query failed, or `'ok'` if the SQL query returned successfully.
98
+
99
+
### React
100
+
101
+
Import the React version of `useReactiveQuery` from `sqlocal/react`. It requires React 18 or higher.
102
+
103
+
In addition to `data`, `error`, and `status`, the object returned from this version of `useReactiveQuery` also contains `setDb` and `setQuery` functions which allow you to dynamically change the arguments from their initial values and automatically resubscribe.
Import the Vue version of `useReactiveQuery` from `sqlocal/vue`. It requires Vue 3 or higher.
122
+
123
+
This version of `useReactiveQuery` returns `data`, `error`, and `status` as read-only Vue refs. It can also accept its arguments as refs, which allows you to dynamically change them from their initial values and automatically resubscribe.
124
+
125
+
```vue
126
+
<script setup>
127
+
import { SQLocal } from 'sqlocal';
128
+
import { useReactiveQuery } from 'sqlocal/vue';
129
+
130
+
const db = new SQLocal({
131
+
databasePath: 'database.sqlite3',
132
+
reactive: true,
133
+
});
134
+
const groceries = useReactiveQuery(db, (sql) => sql`SELECT * FROM groceries`);
135
+
</script>
136
+
```
137
+
138
+
### Angular
139
+
140
+
Import the Angular version of `useReactiveQuery` from `sqlocal/angular`. It requires Angular 17 or higher.
141
+
142
+
This version of `useReactiveQuery` returns `data`, `error`, and `status` as read-only Angular signals. It can also accept its arguments as signals, which allows you to dynamically change them from their initial values and automatically resubscribe.
0 commit comments