-
Notifications
You must be signed in to change notification settings - Fork 9
Introduced Drizzle documentation #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9aaa4e
Introduced Drizzle documentation.
Chriztiaan 91427a1
Whitespace.
Chriztiaan d613f0a
Added relations import to setup example.
Chriztiaan 7749abc
Update client-sdk-references/javascript-web/javascript-orm/drizzle.mdx
Chriztiaan 37cd5a4
Added watched query example for Drizzle.
Chriztiaan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
202 changes: 198 additions & 4 deletions
202
client-sdk-references/javascript-web/javascript-orm/drizzle.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,202 @@ | ||
--- | ||
title: "Drizzle (coming soon)" | ||
icon: "person-digging" | ||
iconType: "solid" | ||
title: "Drizzle" | ||
mode: wide | ||
--- | ||
|
||
We are currently working on adding support for Drizzle. | ||
<Card | ||
title="npm: @powersync/drizzle-driver" | ||
icon="npm" | ||
href="https://www.npmjs.com/package/@powersync/drizzle-driver" | ||
horizontal | ||
/> | ||
|
||
This package enables using [Drizzle](https://orm.drizzle.team/) with PowerSync React Native and web SDKs. | ||
|
||
## Setup | ||
|
||
Set up the PowerSync Database and wrap it with Drizzle. | ||
|
||
Currently, you need to create the Drizzle schema manually, and it should match the table definitions of your PowerSync AppSchema. | ||
Chriztiaan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
```js | ||
import { wrapPowerSyncWithDrizzle } from "@powersync/drizzle-driver"; | ||
import { PowerSyncDatabase } from "@powersync/web"; | ||
import { relations } from "drizzle-orm"; | ||
import { index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core"; | ||
import { appSchema } from "./schema"; | ||
|
||
import { wrapPowerSyncWithDrizzle } from "@powersync/drizzle-driver"; | ||
|
||
export const lists = sqliteTable("lists", { | ||
id: text("id"), | ||
name: text("name"), | ||
}); | ||
|
||
export const todos = sqliteTable("todos", { | ||
id: text("id"), | ||
description: text("description"), | ||
list_id: text("list_id"), | ||
created_at: text("created_at"), | ||
}); | ||
|
||
export const listsRelations = relations(lists, ({ one, many }) => ({ | ||
todos: many(todos), | ||
})); | ||
|
||
export const todosRelations = relations(todos, ({ one, many }) => ({ | ||
list: one(lists, { | ||
fields: [todos.list_id], | ||
references: [lists.id], | ||
}), | ||
})); | ||
|
||
export const drizzleSchema = { | ||
lists, | ||
todos, | ||
listsRelations, | ||
todosRelations, | ||
}; | ||
|
||
export const powerSyncDb = new PowerSyncDatabase({ | ||
database: { | ||
dbFilename: "test.sqlite", | ||
}, | ||
schema: appSchema, | ||
}); | ||
|
||
export const db = wrapPowerSyncWithDrizzle(powerSyncDb, { | ||
schema: drizzleSchema, | ||
}); | ||
``` | ||
|
||
## Compilable queries | ||
|
||
To use Drizzle queries in your hooks and composables, they currently need to be converted using `toCompilableQuery`. | ||
|
||
```js | ||
import { toCompilableQuery } from "@powersync/drizzle-driver"; | ||
|
||
const query = db.select().from(users); | ||
const { data: listRecords, isLoading } = useQuery(toCompilableQuery(query)); | ||
``` | ||
|
||
## Usage Examples | ||
Chriztiaan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Below are examples comparing Drizzle and PowerSync syntax for common database operations. | ||
|
||
### Select Operations | ||
|
||
<CodeGroup> | ||
```js Drizzle | ||
const result = await db.select().from(users); | ||
|
||
// [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }] | ||
|
||
```` | ||
|
||
```js PowerSync | ||
const result = await powerSyncDb.getAll('SELECT * from users'); | ||
// [{ id: '1', name: 'user1' }, { id: '2', name: 'user2' }] | ||
```` | ||
</CodeGroup> | ||
### Insert Operations | ||
<CodeGroup> | ||
```js Drizzle | ||
await db.insert(users).values({ id: '1', name: 'John' }); | ||
const result = await db.select().from(users); | ||
|
||
// [{ id: '1', name: 'John' }] | ||
|
||
```` | ||
|
||
```js PowerSync | ||
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(1, ?)', ['John']); | ||
const result = await powerSyncDb.getAll('SELECT * from users'); | ||
// [{ id: '1', name: 'John' }] | ||
```` | ||
</CodeGroup> | ||
### Delete Operations | ||
<CodeGroup> | ||
```js Drizzle | ||
await db.insert(users).values({ id: '2', name: 'Ben' }); | ||
await db.delete(users).where(eq(users.name, 'Ben')); | ||
const result = await db.select().from(users); | ||
|
||
// [] | ||
|
||
```` | ||
|
||
```js PowerSync | ||
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(2, ?)', ['Ben']); | ||
await powerSyncDb.execute(`DELETE FROM users WHERE name = ?`, ['Ben']); | ||
const result = await powerSyncDb.getAll('SELECT * from users'); | ||
// [] | ||
```` | ||
</CodeGroup> | ||
### Update Operations | ||
<CodeGroup> | ||
```js Drizzle | ||
await db.insert(users).values({ id: '3', name: 'Lucy' }); | ||
await db.update(users).set({ name: 'Lucy Smith' }).where(eq(users.name, 'Lucy')); | ||
const result = await db.select({ name: users.name }).from(users).get(); | ||
|
||
// 'Lucy Smith' | ||
|
||
```` | ||
|
||
```js PowerSync | ||
await powerSyncDb.execute('INSERT INTO users (id, name) VALUES(3, ?)', ['Lucy']); | ||
await powerSyncDb.execute('UPDATE users SET name = ? WHERE name = ?', ['Lucy Smith', 'Lucy']); | ||
const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['Lucy Smith']) | ||
// 'Lucy Smith' | ||
```` | ||
</CodeGroup> | ||
### Transactions | ||
<CodeGroup> | ||
```js Drizzle | ||
await db.transaction(async (transaction) => { | ||
await db.insert(users).values({ id: "4", name: "James" }); | ||
await db | ||
.update(users) | ||
.set({ name: "Lucy James Smith" }) | ||
.where(eq(users.name, "James")); | ||
}); | ||
|
||
const result = await db.select({ name: users.name }).from(users).get(); | ||
|
||
// 'James Smith' | ||
``` | ||
```js PowerSync | ||
await powerSyncDb.writeTransaction((transaction) => { | ||
await transaction.execute('INSERT INTO users (id, name) VALUES(4, ?)', ['James']); | ||
await transaction.execute("UPDATE users SET name = ? WHERE name = ?", ['James Smith', 'James']); | ||
}) | ||
const result = await powerSyncDb.get('SELECT name FROM users WHERE name = ?', ['James Smith']) | ||
|
||
// 'James Smith' | ||
|
||
``` | ||
</CodeGroup> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.