Skip to content

Commit 2f6b22c

Browse files
committed
Added watched query example for Drizzle.
1 parent 7749abc commit 2f6b22c

File tree

1 file changed

+35
-1
lines changed
  • client-sdk-references/javascript-web/javascript-orm

1 file changed

+35
-1
lines changed

client-sdk-references/javascript-web/javascript-orm/drizzle.mdx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This package enables using [Drizzle](https://orm.drizzle.team/) with the PowerSy
1616

1717
Set up the PowerSync Database and wrap it with Drizzle.
1818

19-
Currently, you need to create the Drizzle schema manually, and it should match the table definitions of your PowerSync AppSchema.
19+
Currently, you need to create the Drizzle schema manually, and it should match the table definitions of your PowerSync [client-side schema](/installation/client-side-setup/define-your-schema).
2020

2121
```js
2222
import { wrapPowerSyncWithDrizzle } from "@powersync/drizzle-driver";
@@ -200,3 +200,37 @@ const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['
200200
```
201201
202202
</CodeGroup>
203+
204+
### Watched Queries
205+
206+
<CodeGroup>
207+
208+
```js Drizzle
209+
import { toCompilableQuery } from "@powersync/drizzle-driver";
210+
211+
// `compile()` is automatically called internally in the hooks, but not for `watch()`
212+
const compiledQuery = toCompilableQuery(db.select().from(users)).compile();
213+
214+
powerSyncDb.watch(compiledQuery.sql, compiledQuery.parameters, {
215+
onResult(results) {
216+
console.log(results.rows?._array);
217+
218+
// With Typescript typing
219+
// console.log((results.rows?._array as (typeof users.$inferSelect)[]));
220+
},
221+
});
222+
223+
// [{ id: '1', name: 'John' }]
224+
```
225+
226+
```js PowerSync
227+
powerSyncDb.watch("select * from users", [], {
228+
onResult(results) {
229+
console.log(results.rows?._array);
230+
},
231+
});
232+
233+
// [{ id: '1', name: 'John' }]
234+
```
235+
236+
</CodeGroup>

0 commit comments

Comments
 (0)