Skip to content

Commit 01039ba

Browse files
committed
Added mechanism to map Drizzle table to a PowerSync table.
1 parent b153571 commit 01039ba

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

packages/drizzle-driver/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,36 @@ export const db = wrapPowerSyncWithDrizzle(powerSyncDb, {
5959
});
6060
```
6161

62+
## Converting Drizzle Tables to PowerSync Tables
63+
64+
The `toPowerSyncTable` function simplifies the process of integrating Drizzle with PowerSync. Define your Drizzle tables, convert each using `toPowerSyncTable`, and supply the converted table definitions into your PowerSync schema for a unified development experience.
65+
66+
As the PowerSync table only supports `text`, `integer`, and `real`, the same limitation extends to the Drizzle table definitions.
67+
68+
```js
69+
import { toPowerSyncTable } from '@powersync/drizzle-driver';
70+
import { Schema } from '@powersync/web';
71+
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
72+
73+
// Define a Drizzle table
74+
const lists = sqliteTable('lists', {
75+
id: text('id').primaryKey().notNull(),
76+
created_at: text('created_at'),
77+
name: text('name').notNull(),
78+
owner_id: text('owner_id')
79+
});
80+
81+
const psLists = toPowerSyncTable(lists); // converts the Drizzle table to a PowerSync table
82+
// toPowerSyncTable(lists, { localOnly: true }); - th allows for PowerSync table configuration
83+
84+
export const AppSchema = new Schema({
85+
lists: psLists // names the table `lists` in the PowerSync schema
86+
});
87+
```
88+
6289
## Known limitations
6390

6491
- The integration does not currently support nested transactions (also known as `savepoints`).
65-
- The Drizzle schema needs to be created manually, and it should match the table definitions of your PowerSync schema.
6692

6793
### Compilable queries
6894

packages/drizzle-driver/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
"@powersync/common": "workspace:^1.19.0",
3030
"drizzle-orm": "<1.0.0"
3131
},
32+
"dependencies": {
33+
"@powersync/common": "workspace:*"
34+
},
3235
"devDependencies": {
3336
"@powersync/web": "workspace:*",
3437
"@journeyapps/wa-sqlite": "^0.4.1",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { wrapPowerSyncWithDrizzle, type PowerSyncSQLiteDatabase } from './sqlite/db';
22
import { toCompilableQuery } from './utils/compilableQuery';
3+
import { toPowerSyncTable } from './utils/schema';
34

4-
export { wrapPowerSyncWithDrizzle, toCompilableQuery, PowerSyncSQLiteDatabase };
5+
export { wrapPowerSyncWithDrizzle, toCompilableQuery, toPowerSyncTable, PowerSyncSQLiteDatabase };
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { column, Table, type BaseColumnType, type TableV2Options } from '@powersync/common';
2+
import {
3+
SQLiteInteger,
4+
SQLiteReal,
5+
SQLiteText,
6+
type SQLiteTableWithColumns,
7+
type TableConfig
8+
} from 'drizzle-orm/sqlite-core';
9+
10+
export function toPowerSyncTable<T extends TableConfig>(table: SQLiteTableWithColumns<T>, options?: TableV2Options) {
11+
const columns: { [key: string]: BaseColumnType<number | string | null> } = {};
12+
for (const [columnName, columnValue] of Object.entries(table)) {
13+
// Skip the id column
14+
if (columnName === 'id') {
15+
continue;
16+
}
17+
18+
let mappedType: BaseColumnType<number | string | null>;
19+
switch (columnValue.columnType) {
20+
case SQLiteText.name:
21+
mappedType = column.text;
22+
break;
23+
case SQLiteInteger.name:
24+
mappedType = column.integer;
25+
break;
26+
case SQLiteReal.name:
27+
mappedType = column.real;
28+
break;
29+
default:
30+
throw new Error(`Unsupported column type: ${columnValue.columnType}`);
31+
}
32+
columns[columnName] = mappedType;
33+
}
34+
return new Table(columns, options);
35+
}

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)