|
1 | 1 | --- |
2 | | -title: "Drizzle (coming soon)" |
3 | | -icon: "person-digging" |
4 | | -iconType: "solid" |
| 2 | +title: "Drizzle" |
5 | 3 | mode: wide |
6 | 4 | --- |
7 | 5 |
|
8 | | -We are currently working on adding support for Drizzle. |
| 6 | +<Card |
| 7 | + title="npm: @powersync/drizzle-driver" |
| 8 | + icon="npm" |
| 9 | + href="https://www.npmjs.com/package/@powersync/drizzle-driver" |
| 10 | + horizontal |
| 11 | +/> |
| 12 | + |
| 13 | +This package enables using [Drizzle](https://orm.drizzle.team/) with PowerSync React Native and web SDKs. |
| 14 | + |
| 15 | +## Setup |
| 16 | + |
| 17 | +Set up the PowerSync Database and wrap it with Drizzle. |
| 18 | + |
| 19 | +Currently, you need to create the Drizzle schema manually, and it should match the table definitions of your PowerSync AppSchema. |
| 20 | + |
| 21 | +```js |
| 22 | +import { wrapPowerSyncWithDrizzle } from "@powersync/drizzle-driver"; |
| 23 | +import { PowerSyncDatabase } from "@powersync/web"; |
| 24 | +import { index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core"; |
| 25 | +import { appSchema } from "./schema"; |
| 26 | + |
| 27 | +import { wrapPowerSyncWithDrizzle } from "@powersync/drizzle-driver"; |
| 28 | + |
| 29 | +export const lists = sqliteTable("lists", { |
| 30 | + id: text("id"), |
| 31 | + name: text("name"), |
| 32 | +}); |
| 33 | + |
| 34 | +export const todos = sqliteTable("todos", { |
| 35 | + id: text("id"), |
| 36 | + description: text("description"), |
| 37 | + list_id: text("list_id"), |
| 38 | + created_at: text("created_at"), |
| 39 | +}); |
| 40 | + |
| 41 | +export const listsRelations = relations(lists, ({ one, many }) => ({ |
| 42 | + todos: many(todos), |
| 43 | +})); |
| 44 | + |
| 45 | +export const todosRelations = relations(todos, ({ one, many }) => ({ |
| 46 | + list: one(lists, { |
| 47 | + fields: [todos.list_id], |
| 48 | + references: [lists.id], |
| 49 | + }), |
| 50 | +})); |
| 51 | + |
| 52 | +export const drizzleSchema = { |
| 53 | + lists, |
| 54 | + todos, |
| 55 | + listsRelations, |
| 56 | + todosRelations, |
| 57 | +}; |
| 58 | + |
| 59 | +export const powerSyncDb = new PowerSyncDatabase({ |
| 60 | + database: { |
| 61 | + dbFilename: "test.sqlite", |
| 62 | + }, |
| 63 | + schema: appSchema, |
| 64 | +}); |
| 65 | + |
| 66 | +export const db = wrapPowerSyncWithDrizzle(powerSyncDb, { |
| 67 | + schema: drizzleSchema, |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +## Compilable queries |
| 72 | + |
| 73 | +To use Drizzle queries in your hooks and composables, they currently need to be converted using `toCompilableQuery`. |
| 74 | + |
| 75 | +```js |
| 76 | +import { toCompilableQuery } from "@powersync/drizzle-driver"; |
| 77 | + |
| 78 | +const query = db.select().from(users); |
| 79 | +const { data: listRecords, isLoading } = useQuery(toCompilableQuery(query)); |
| 80 | +``` |
| 81 | + |
| 82 | +## Usage Examples |
| 83 | + |
| 84 | +Below are examples comparing Drizzle and PowerSync syntax for common database operations. |
| 85 | + |
| 86 | +### Select Operations |
| 87 | + |
| 88 | +<CodeGroup> |
| 89 | + ```js Drizzle |
| 90 | + const result = await db.select().from(users); |
| 91 | + |
| 92 | +// [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }] |
| 93 | + |
| 94 | +```` |
| 95 | + |
| 96 | +```js PowerSync |
| 97 | +const result = await powerSyncDb.getAll('SELECT * from users'); |
| 98 | +
|
| 99 | +// [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }] |
| 100 | +```` |
| 101 | +
|
| 102 | +</CodeGroup> |
| 103 | +
|
| 104 | +### Insert Operations |
| 105 | +
|
| 106 | +<CodeGroup> |
| 107 | + ```js Drizzle |
| 108 | + await db.insert(users).values({ id: '1', name: 'John' }); |
| 109 | + const result = await db.select().from(users); |
| 110 | + |
| 111 | +// [{ id: '1', name: 'John' }] |
| 112 | + |
| 113 | +```` |
| 114 | + |
| 115 | +```js PowerSync |
| 116 | +await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(1, ?)', ['John']); |
| 117 | +const result = await powerSyncDb.getAll('SELECT * from users'); |
| 118 | +
|
| 119 | +// [{ id: '1', name: 'John' }] |
| 120 | +```` |
| 121 | +
|
| 122 | +</CodeGroup> |
| 123 | +
|
| 124 | +### Delete Operations |
| 125 | +
|
| 126 | +<CodeGroup> |
| 127 | + ```js Drizzle |
| 128 | + await db.insert(users).values({ id: '2', name: 'Ben' }); |
| 129 | + await db.delete(users).where(eq(users.name, 'Ben')); |
| 130 | + const result = await db.select().from(users); |
| 131 | + |
| 132 | +// [] |
| 133 | + |
| 134 | +```` |
| 135 | + |
| 136 | +```js PowerSync |
| 137 | +await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(2, ?)', ['Ben']); |
| 138 | +await powerSyncDb.execute(`DELETE FROM users WHERE name = ?`, ['Ben']); |
| 139 | +const result = await powerSyncDb.getAll('SELECT * from users'); |
| 140 | +
|
| 141 | +// [] |
| 142 | +```` |
| 143 | +
|
| 144 | +</CodeGroup> |
| 145 | +
|
| 146 | +### Update Operations |
| 147 | +
|
| 148 | +<CodeGroup> |
| 149 | + ```js Drizzle |
| 150 | +await db.insert(users).values({ id: '3', name: 'Lucy' }); |
| 151 | +await db.update(users).set({ name: 'Lucy Smith' }).where(eq(users.name, 'Lucy')); |
| 152 | +const result = await db.select({ name: users.name }).from(users).get(); |
| 153 | + |
| 154 | +// 'Lucy Smith' |
| 155 | + |
| 156 | +```` |
| 157 | + |
| 158 | +```js PowerSync |
| 159 | +await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(3, ?)', ['Lucy']); |
| 160 | +await powerSyncDb.execute('UPDATE users SET name = ? WHERE name = ?', ['Lucy Smith', 'Lucy']); |
| 161 | +const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['Lucy Smith']) |
| 162 | +
|
| 163 | +// 'Lucy Smith' |
| 164 | +```` |
| 165 | +
|
| 166 | +</CodeGroup> |
| 167 | +
|
| 168 | +### Transactions |
| 169 | +
|
| 170 | +<CodeGroup> |
| 171 | +
|
| 172 | +```js Drizzle |
| 173 | +await db.transaction(async (transaction) => { |
| 174 | + await db.insert(users).values({ id: "4", name: "James" }); |
| 175 | + await db |
| 176 | + .update(users) |
| 177 | + .set({ name: "Lucy James Smith" }) |
| 178 | + .where(eq(users.name, "James")); |
| 179 | +}); |
| 180 | + |
| 181 | +const result = await db.select({ name: users.name }).from(users).get(); |
| 182 | + |
| 183 | +// 'James Smith' |
| 184 | +``` |
| 185 | +
|
| 186 | +```js PowerSync |
| 187 | +await powerSyncDb.writeTransaction((transaction) => { |
| 188 | + await transaction.execute('INSERT INTO users (id, name) VALUES(4, ?)', ['James']); |
| 189 | + await transaction.execute("UPDATE users SET name = ? WHERE name = ?", ['James Smith', 'James']); |
| 190 | +}) |
| 191 | +const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['James Smith']) |
| 192 | + |
| 193 | +// 'James Smith' |
| 194 | +``` |
| 195 | +
|
| 196 | +</CodeGroup> |
| 197 | +```` |
0 commit comments