|
1 | 1 | # PowerSync Drizzle Driver |
2 | 2 |
|
3 | | -``` |
4 | | -import { wrapPowerSyncWithDrizzle} from "@powersync/drizzle-driver"; |
| 3 | +This package (`@powersync/drizzle-driver`) brings the benefits of an ORM through our maintained [Drizzle](https://orm.drizzle.team/) driver to PowerSync. |
| 4 | + |
| 5 | +## Alpha Release |
| 6 | + |
| 7 | +The `drizzle-driver` package is currently in an Alpha release. |
| 8 | + |
| 9 | +## Getting Started |
| 10 | + |
| 11 | +Set up the PowerSync Database and wrap it with Drizzle. |
| 12 | + |
| 13 | +Currently, you need to create the Drizzle schema manually, and it should match the table definitions of your PowerSync AppSchema. |
5 | 14 |
|
6 | | -export const lists = sqliteTable("lists", { |
7 | | - id: text("id"), |
8 | | - name: text("name"), |
| 15 | +```js |
| 16 | +import { wrapPowerSyncWithDrizzle } from '@powersync/drizzle-driver'; |
| 17 | +import { PowerSyncDatabase } from '@powersync/web'; |
| 18 | +import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
| 19 | +import { appSchema } from './schema'; |
| 20 | + |
| 21 | +import { wrapPowerSyncWithDrizzle } from '@powersync/drizzle-driver'; |
| 22 | + |
| 23 | +export const lists = sqliteTable('lists', { |
| 24 | + id: text('id'), |
| 25 | + name: text('name') |
9 | 26 | }); |
10 | 27 |
|
11 | | -export const todos = sqliteTable("todos", { |
12 | | - id: text("id"), |
13 | | - description: text("description"), |
14 | | - list_id: text("list_id"), |
15 | | - created_at: text("created_at"), |
| 28 | +export const todos = sqliteTable('todos', { |
| 29 | + id: text('id'), |
| 30 | + description: text('description'), |
| 31 | + list_id: text('list_id'), |
| 32 | + created_at: text('created_at') |
16 | 33 | }); |
17 | 34 |
|
18 | 35 | export const listsRelations = relations(lists, ({ one, many }) => ({ |
19 | | - todos: many(todos), |
| 36 | + todos: many(todos) |
20 | 37 | })); |
21 | 38 |
|
22 | 39 | export const todosRelations = relations(todos, ({ one, many }) => ({ |
23 | 40 | list: one(lists, { |
24 | 41 | fields: [todos.list_id], |
25 | | - references: [lists.id], |
26 | | - }), |
| 42 | + references: [lists.id] |
| 43 | + }) |
27 | 44 | })); |
28 | 45 |
|
29 | | -export const db = wrapPowerSyncWithDrizzle(PowerSync, { |
30 | | - schema: { |
31 | | - lists, |
32 | | - todos, |
33 | | - listsRelations, |
34 | | - todosRelations, |
| 46 | +export const drizzleSchema = { |
| 47 | + lists, |
| 48 | + todos, |
| 49 | + listsRelations, |
| 50 | + todosRelations |
| 51 | +}; |
| 52 | + |
| 53 | +export const powerSyncDb = new PowerSyncDatabase({ |
| 54 | + database: { |
| 55 | + dbFilename: 'test.sqlite' |
35 | 56 | }, |
| 57 | + schema: appSchema |
36 | 58 | }); |
37 | 59 |
|
| 60 | +export const db = wrapPowerSyncWithDrizzle(powerSyncDb, { |
| 61 | + schema: drizzleSchema |
| 62 | +}); |
38 | 63 | ``` |
39 | 64 |
|
40 | 65 | ## Compilable queries |
41 | 66 |
|
42 | | -To use drizzle queries in our hooks and composables they currently need to be converted. |
| 67 | +To use Drizzle queries in your hooks and composables, they currently need to be converted using `toCompilableQuery`. |
43 | 68 |
|
44 | | -``` |
45 | | -import { toCompilableQuery } from "@powersync/drizzle-driver"; |
| 69 | +```js |
| 70 | +import { toCompilableQuery } from '@powersync/drizzle-driver'; |
46 | 71 |
|
47 | | -const query = drizzleDb.select().from(lists); |
| 72 | +const query = db.select().from(lists); |
48 | 73 | const { data: listRecords, isLoading } = useQuery(toCompilableQuery(query)); |
49 | 74 | ``` |
| 75 | + |
| 76 | +For more information on how to use Drizzle queries in PowerSync, see [here](https://docs.powersync.com/client-sdk-references/javascript-web/javascript-orm/drizzle#usage-examples). |
0 commit comments