Skip to content

Commit 3b0b7eb

Browse files
committed
Documentation edits made through Mintlify web editor
1 parent b59f96a commit 3b0b7eb

File tree

1 file changed

+92
-93
lines changed
  • client-sdk-references/javascript-web/javascript-orm

1 file changed

+92
-93
lines changed

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

Lines changed: 92 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ title: "Drizzle"
33
mode: wide
44
---
55

6-
<Card
7-
title="npm: @powersync/drizzle-driver"
8-
icon="npm"
9-
href="https://www.npmjs.com/package/@powersync/drizzle-driver"
10-
horizontal
11-
/>
6+
***
7+
8+
title: "Drizzle"
9+
mode: wide
10+
----------
11+
12+
<Card title="npm: @powersync/drizzle-driver" icon="npm" href="https://www.npmjs.com/package/@powersync/drizzle-driver" horizontal />
1213

1314
This package enables using [Drizzle](https://orm.drizzle.team/) with the PowerSync [React Native](/client-sdk-references/react-native-and-expo) and [JavaScript Web](/client-sdk-references/javascript-web) SDKs.
1415

@@ -25,6 +26,7 @@ import { relations } from "drizzle-orm";
2526
import { index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
2627
import { appSchema } from "./schema";
2728

29+
// Define Drizzle schema
2830
export const lists = sqliteTable("lists", {
2931
id: text("id"),
3032
name: text("name"),
@@ -55,13 +57,16 @@ export const drizzleSchema = {
5557
todosRelations,
5658
};
5759

60+
// Existing PowerSync database constructor
5861
export const powerSyncDb = new PowerSyncDatabase({
5962
database: {
6063
dbFilename: "test.sqlite",
6164
},
6265
schema: appSchema,
6366
});
6467

68+
// Wrap the database with Drizzle
69+
// Provide the Drizzle schema you created above
6570
export const db = wrapPowerSyncWithDrizzle(powerSyncDb, {
6671
schema: drizzleSchema,
6772
});
@@ -88,17 +93,16 @@ Below are examples comparing Drizzle and PowerSync syntax for common database op
8893
```js Drizzle
8994
const result = await db.select().from(users);
9095

91-
// [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }]
92-
93-
````
96+
// [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }]
9497

95-
```js PowerSync
96-
const result = await powerSyncDb.getAll('SELECT * from users');
98+
```
9799

98-
// [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }]
100+
```js PowerSync
101+
const result = await powerSyncDb.getAll('SELECT * from users');
99102

100-
````
103+
// [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }]
101104

105+
```
102106
</CodeGroup>
103107

104108
### Insert Operations
@@ -108,18 +112,17 @@ const result = await powerSyncDb.getAll('SELECT * from users');
108112
await db.insert(users).values({ id: '1', name: 'John' });
109113
const result = await db.select().from(users);
110114

111-
// [{ id: '1', name: 'John' }]
115+
// [{ id: '1', name: 'John' }]
112116

113-
````
117+
```
114118

115-
```js PowerSync
116-
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(1, ?)', ['John']);
117-
const result = await powerSyncDb.getAll('SELECT * from users');
119+
```js PowerSync
120+
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(1, ?)', ['John']);
121+
const result = await powerSyncDb.getAll('SELECT * from users');
118122

119-
// [{ id: '1', name: 'John' }]
120-
121-
````
123+
// [{ id: '1', name: 'John' }]
122124

125+
```
123126
</CodeGroup>
124127

125128
### Delete Operations
@@ -130,105 +133,101 @@ const result = await powerSyncDb.getAll('SELECT * from users');
130133
await db.delete(users).where(eq(users.name, 'Ben'));
131134
const result = await db.select().from(users);
132135

133-
// []
134-
135-
````
136+
// []
136137

137-
```js PowerSync
138-
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(2, ?)', ['Ben']);
139-
await powerSyncDb.execute(`DELETE FROM users WHERE name = ?`, ['Ben']);
140-
const result = await powerSyncDb.getAll('SELECT * from users');
138+
```
141139

142-
// []
140+
```js PowerSync
141+
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(2, ?)', ['Ben']);
142+
await powerSyncDb.execute(`DELETE FROM users WHERE name = ?`, ['Ben']);
143+
const result = await powerSyncDb.getAll('SELECT * from users');
143144

144-
````
145+
// []
145146

147+
```
146148
</CodeGroup>
147149

148150
### Update Operations
149151

150152
<CodeGroup>
151153
```js Drizzle
152-
await db.insert(users).values({ id: '3', name: 'Lucy' });
153-
await db.update(users).set({ name: 'Lucy Smith' }).where(eq(users.name, 'Lucy'));
154-
const result = await db.select({ name: users.name }).from(users).get();
154+
await db.insert(users).values({ id: '3', name: 'Lucy' });
155+
await db.update(users).set({ name: 'Lucy Smith' }).where(eq(users.name, 'Lucy'));
156+
const result = await db.select({ name: users.name }).from(users).get();
155157

156-
// 'Lucy Smith'
158+
// 'Lucy Smith'
157159

158-
````
160+
```
159161

160-
```js PowerSync
161-
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(3, ?)', ['Lucy']);
162-
await powerSyncDb.execute('UPDATE users SET name = ? WHERE name = ?', ['Lucy Smith', 'Lucy']);
163-
const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['Lucy Smith'])
162+
```js PowerSync
163+
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(3, ?)', ['Lucy']);
164+
await powerSyncDb.execute('UPDATE users SET name = ? WHERE name = ?', ['Lucy Smith', 'Lucy']);
165+
const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['Lucy Smith'])
164166

165-
// 'Lucy Smith'
166-
167-
````
167+
// 'Lucy Smith'
168168

169+
```
169170
</CodeGroup>
170171

171-
### Transactions
172-
173-
<CodeGroup>
174-
175-
```js Drizzle
176-
await db.transaction(async (transaction) => {
177-
await db.insert(users).values({ id: "4", name: "James" });
178-
await db
179-
.update(users)
180-
.set({ name: "Lucy James Smith" })
181-
.where(eq(users.name, "James"));
182-
});
183-
184-
const result = await db.select({ name: users.name }).from(users).get();
185-
186-
// 'James Smith'
187-
```
188-
189-
```js PowerSync
190-
await powerSyncDb.writeTransaction((transaction) => {
191-
await transaction.execute('INSERT INTO users (id, name) VALUES(4, ?)', ['James']);
192-
await transaction.execute("UPDATE users SET name = ? WHERE name = ?", ['James Smith', 'James']);
193-
})
194-
const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['James Smith'])
195172

196-
// 'James Smith'
197-
198-
```
199-
200-
</CodeGroup>
201173

202174
### Watched Queries
203175

204176
<CodeGroup>
177+
```js Drizzle
178+
import { toCompilableQuery } from "@powersync/drizzle-driver";
205179

206-
```js Drizzle
207-
import { toCompilableQuery } from "@powersync/drizzle-driver";
180+
// `compile()` is automatically called internally in the hooks, but not for `watch()`
181+
const compiledQuery = toCompilableQuery(db.select().from(users)).compile();
208182

209-
// `compile()` is automatically called internally in the hooks, but not for `watch()`
210-
const compiledQuery = toCompilableQuery(db.select().from(users)).compile();
183+
powerSyncDb.watch(compiledQuery.sql, compiledQuery.parameters, {
184+
onResult(results) {
185+
console.log(results.rows?._array);
211186

212-
powerSyncDb.watch(compiledQuery.sql, compiledQuery.parameters, {
213-
onResult(results) {
214-
console.log(results.rows?._array);
187+
// With Typescript typing
188+
// console.log((results.rows?._array as (typeof users.$inferSelect)[]));
189+
},
190+
});
215191

216-
// With Typescript typing
217-
// console.log((results.rows?._array as (typeof users.$inferSelect)[]));
218-
},
219-
});
192+
// [{ id: '1', name: 'John' }]
193+
```
220194
221-
// [{ id: '1', name: 'John' }]
222-
```
195+
```js PowerSync
196+
powerSyncDb.watch("select * from users", [], {
197+
onResult(results) {
198+
console.log(results.rows?._array);
199+
},
200+
});
223201

224-
```js PowerSync
225-
powerSyncDb.watch("select * from users", [], {
226-
onResult(results) {
227-
console.log(results.rows?._array);
228-
},
229-
});
202+
// [{ id: '1', name: 'John' }]
203+
```
204+
</CodeGroup>
230205
231-
// [{ id: '1', name: 'John' }]
232-
```
206+
### Transactions
233207
234-
</CodeGroup>
208+
<CodeGroup>
209+
```js Drizzle
210+
await db.transaction(async (transaction) => {
211+
await db.insert(users).values({ id: "4", name: "James" });
212+
await db
213+
.update(users)
214+
.set({ name: "Lucy James Smith" })
215+
.where(eq(users.name, "James"));
216+
});
217+
218+
const result = await db.select({ name: users.name }).from(users).get();
219+
220+
// 'James Smith'
221+
```
222+
223+
```js PowerSync
224+
await powerSyncDb.writeTransaction((transaction) => {
225+
await transaction.execute('INSERT INTO users (id, name) VALUES(4, ?)', ['James']);
226+
await transaction.execute("UPDATE users SET name = ? WHERE name = ?", ['James Smith', 'James']);
227+
})
228+
const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['James Smith'])
229+
230+
// 'James Smith'
231+
232+
```
233+
</CodeGroup>

0 commit comments

Comments
 (0)