Skip to content

Commit 772c377

Browse files
authored
Merge branch 'docs' into supabase-jwt
2 parents 1ce69e1 + 8ac6040 commit 772c377

File tree

2 files changed

+129
-27
lines changed

2 files changed

+129
-27
lines changed

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

Lines changed: 101 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,132 @@ This package enables using [Drizzle](https://orm.drizzle.team/) with the PowerSy
1111

1212
Set up the PowerSync Database and wrap it with Drizzle.
1313

14-
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).
15-
1614
```js
17-
import { wrapPowerSyncWithDrizzle } from "@powersync/drizzle-driver";
18-
import { PowerSyncDatabase } from "@powersync/web";
19-
import { relations } from "drizzle-orm";
20-
import { index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
21-
import { appSchema } from "./schema";
22-
23-
// Define Drizzle schema
24-
export const lists = sqliteTable("lists", {
25-
id: text("id"),
26-
name: text("name"),
15+
import { wrapPowerSyncWithDrizzle } from '@powersync/drizzle-driver';
16+
import { PowerSyncDatabase } from '@powersync/web';
17+
import { relations } from 'drizzle-orm';
18+
import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
19+
import { AppSchema } from './schema';
20+
21+
export const lists = sqliteTable('lists', {
22+
id: text('id'),
23+
name: text('name')
2724
});
2825

29-
export const todos = sqliteTable("todos", {
30-
id: text("id"),
31-
description: text("description"),
32-
list_id: text("list_id"),
33-
created_at: text("created_at"),
26+
export const todos = sqliteTable('todos', {
27+
id: text('id'),
28+
description: text('description'),
29+
list_id: text('list_id'),
30+
created_at: text('created_at')
3431
});
3532

3633
export const listsRelations = relations(lists, ({ one, many }) => ({
37-
todos: many(todos),
34+
todos: many(todos)
3835
}));
3936

4037
export const todosRelations = relations(todos, ({ one, many }) => ({
4138
list: one(lists, {
4239
fields: [todos.list_id],
43-
references: [lists.id],
44-
}),
40+
references: [lists.id]
41+
})
4542
}));
4643

4744
export const drizzleSchema = {
4845
lists,
4946
todos,
5047
listsRelations,
51-
todosRelations,
48+
todosRelations
5249
};
5350

54-
// Existing PowerSync database constructor
51+
// As an alternative to manually defining a PowerSync schema, generate the local PowerSync schema from the Drizzle schema with the `DrizzleAppSchema` constructor:
52+
// import { DrizzleAppSchema } from '@powersync/drizzle-driver';
53+
// export const AppSchema = new DrizzleAppSchema(drizzleSchema);
54+
//
55+
// This is optional, but recommended, since you will only need to maintain one schema on the client-side
56+
// Read on to learn more.
57+
5558
export const powerSyncDb = new PowerSyncDatabase({
5659
database: {
57-
dbFilename: "test.sqlite",
60+
dbFilename: 'test.sqlite'
5861
},
59-
schema: appSchema,
62+
schema: AppSchema
6063
});
6164

62-
// Wrap the database with Drizzle
63-
// Provide the Drizzle schema you created above
65+
// This is the DB you will use in queries
6466
export const db = wrapPowerSyncWithDrizzle(powerSyncDb, {
65-
schema: drizzleSchema,
67+
schema: drizzleSchema
68+
});
69+
```
70+
71+
## Schema Conversion
72+
73+
The `DrizzleAppSchema` constructor simplifies the process of integrating Drizzle with PowerSync. It infers the local [PowerSync schema](/installation/client-side-setup/define-your-schema) from your Drizzle schema definition, providing a unified development experience.
74+
75+
As the PowerSync schema only supports SQLite types (`text`, `integer`, and `real`), the same limitation extends to the Drizzle table definitions.
76+
77+
To use it, define your Drizzle tables and supply the schema to the `DrizzleAppSchema` function:
78+
79+
```js
80+
import { DrizzleAppSchema } from '@powersync/drizzle-driver';
81+
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
82+
83+
// Define a Drizzle table
84+
const lists = sqliteTable('lists', {
85+
id: text('id').primaryKey().notNull(),
86+
created_at: text('created_at'),
87+
name: text('name').notNull(),
88+
owner_id: text('owner_id')
89+
});
90+
91+
export const drizzleSchema = {
92+
lists
93+
};
94+
95+
// Infer the PowerSync schema from your Drizzle schema
96+
export const AppSchema = new DrizzleAppSchema(drizzleSchema);
97+
```
98+
99+
### Defining PowerSync Options
100+
101+
The PowerSync table definition allows additional options supported by PowerSync's app schema beyond that which are supported by Drizzle.
102+
They can be specified as follows. Note that these options exclude indexes as they can be specified in a Drizzle table.
103+
104+
```js
105+
import { DrizzleAppSchema } from '@powersync/drizzle-driver';
106+
// import { DrizzleAppSchema, type DrizzleTableWithPowerSyncOptions} from '@powersync/drizzle-driver'; for TypeScript
107+
108+
const listsWithOptions = { tableDefinition: logs, options: { localOnly: true } };
109+
// const listsWithOptions: DrizzleTableWithPowerSyncOptions = { tableDefinition: logs, options: { localOnly: true } }; for TypeScript
110+
111+
export const drizzleSchemaWithOptions = {
112+
lists: listsWithOptions
113+
};
114+
115+
export const AppSchema = new DrizzleAppSchema(drizzleSchemaWithOptions);
116+
```
117+
118+
### Converting a Single Table From Drizzle to PowerSync
119+
120+
Drizzle tables can also be converted on a table-by-table basis with `toPowerSyncTable`.
121+
122+
```js
123+
import { toPowerSyncTable } from '@powersync/drizzle-driver';
124+
import { Schema } from '@powersync/web';
125+
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
126+
127+
// Define a Drizzle table
128+
const lists = sqliteTable('lists', {
129+
id: text('id').primaryKey().notNull(),
130+
created_at: text('created_at'),
131+
name: text('name').notNull(),
132+
owner_id: text('owner_id')
133+
});
134+
135+
const psLists = toPowerSyncTable(lists); // converts the Drizzle table to a PowerSync table
136+
// toPowerSyncTable(lists, { localOnly: true }); - allows for PowerSync table configuration
137+
138+
export const AppSchema = new Schema({
139+
lists: psLists // names the table `lists` in the PowerSync schema
66140
});
67141
```
68142

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ Below are examples comparing Kysely and PowerSync syntax for common database ope
137137
```
138138
</CodeGroup>
139139

140+
### Watched Queries
141+
142+
For watched queries with Kysely it's recommended to use the `watch()` function from the wrapper package which takes in a Kysely query.
143+
144+
<CodeGroup>
145+
```js Kysely
146+
const query = db.selectFrom('users').select();
147+
148+
db.watch(query, {
149+
onResult(results) {
150+
console.log(results);
151+
},
152+
});
153+
154+
// [{ id: '1', name: 'John' }]
155+
```
156+
157+
```js PowerSync
158+
powerSyncDb.watch("select * from users", [], {
159+
onResult(results) {
160+
console.log(results.rows?._array);
161+
},
162+
});
163+
164+
// [{ id: '1', name: 'John' }]
165+
```
166+
</CodeGroup>
167+
140168
### Transactions
141169
142170
<CodeGroup>

0 commit comments

Comments
 (0)