Skip to content

Commit 3238948

Browse files
committed
Dropped toPowerSyncSchema in favour of DrizzleAppSchema constructor.
1 parent e312705 commit 3238948

File tree

5 files changed

+23
-82
lines changed

5 files changed

+23
-82
lines changed

.changeset/tender-mugs-deliver.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@powersync/drizzle-driver': minor
33
---
44

5-
Added `toPowersyncTable` and `toPowerSyncSchema` helper functions to convert a Drizzle schema into a PowerSync app schema
5+
Added helper `toPowersyncTable` function and `DrizzleAppSchema` constructor to convert a Drizzle schema into a PowerSync app schema.

packages/drizzle-driver/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export const drizzleSchema = {
4747
todosRelations
4848
};
4949

50-
// As an alternative to manually defining a PowerSync schema, generate the local PowerSync schema from the Drizzle schema with `toPowerSyncSchema`:
51-
// import { toPowerSyncSchema } from '@powersync/drizzle-driver';
52-
// export const AppSchema = toPowerSyncSchema(drizzleSchema);
50+
// As an alternative to manually defining a PowerSync schema, generate the local PowerSync schema from the Drizzle schema with the `DrizzleAppSchema` constructor:
51+
// import { DrizzleAppSchema } from '@powersync/drizzle-driver';
52+
// export const AppSchema = new DrizzleAppSchema(drizzleSchema);
5353
//
5454
// This is optional, but recommended, since you will only need to maintain one schema on the client-side
5555
// Read on to learn more.
@@ -69,14 +69,14 @@ export const db = wrapPowerSyncWithDrizzle(powerSyncDb, {
6969

7070
## Schema Conversion
7171

72-
The `toPowerSyncSchema` function simplifies the process of integrating Drizzle with PowerSync. It infers the local [PowerSync schema](https://docs.powersync.com/installation/client-side-setup/define-your-schema) from your Drizzle schema definition, providing a unified development experience.
72+
The `DrizzleAppSchema` constructor simplifies the process of integrating Drizzle with PowerSync. It infers the local [PowerSync schema](https://docs.powersync.com/installation/client-side-setup/define-your-schema) from your Drizzle schema definition, providing a unified development experience.
7373

7474
As the PowerSync schema only supports SQLite types (`text`, `integer`, and `real`), the same limitation extends to the Drizzle table definitions.
7575

76-
To use it, define your Drizzle tables and supply the schema to the `toPowerSyncSchema` function:
76+
To use it, define your Drizzle tables and supply the schema to the `DrizzleAppSchema` function:
7777

7878
```js
79-
import { toPowerSyncSchema } from '@powersync/drizzle-driver';
79+
import { DrizzleAppSchema } from '@powersync/drizzle-driver';
8080
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
8181

8282
// Define a Drizzle table
@@ -92,7 +92,7 @@ export const drizzleSchema = {
9292
};
9393

9494
// Infer the PowerSync schema from your Drizzle schema
95-
export const AppSchema = toPowerSyncSchema(drizzleSchema);
95+
export const AppSchema = new DrizzleAppSchema(drizzleSchema);
9696
```
9797

9898
### Defining PowerSync Options
@@ -101,8 +101,8 @@ The PowerSync table definition allows additional options supported by PowerSync'
101101
They can be specified as follows. Note that these options exclude indexes as they can be specified in a Drizzle table.
102102

103103
```js
104-
import { toPowerSyncSchema } from '@powersync/drizzle-driver';
105-
// import { toPowerSyncSchema, type DrizzleTableWithPowerSyncOptions} from '@powersync/drizzle-driver'; for TypeScript
104+
import { DrizzleAppSchema } from '@powersync/drizzle-driver';
105+
// import { DrizzleAppSchema, type DrizzleTableWithPowerSyncOptions} from '@powersync/drizzle-driver'; for TypeScript
106106

107107
const listsWithOptions = { tableDefinition: logs, options: { localOnly: true } };
108108
// const listsWithOptions: DrizzleTableWithPowerSyncOptions = { tableDefinition: logs, options: { localOnly: true } }; for TypeScript
@@ -111,7 +111,7 @@ export const drizzleSchemaWithOptions = {
111111
lists: listsWithOptions
112112
};
113113

114-
export const AppSchema = toPowerSyncSchema(drizzleSchemaWithOptions);
114+
export const AppSchema = new DrizzleAppSchema(drizzleSchemaWithOptions);
115115
```
116116

117117
### Converting a Single Table From Drizzle to PowerSync

packages/drizzle-driver/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { wrapPowerSyncWithDrizzle, type PowerSyncSQLiteDatabase } from './sqlite/db';
22
import { toCompilableQuery } from './utils/compilableQuery';
33
import {
4-
toPowerSyncSchema,
4+
DrizzleAppSchema,
55
toPowerSyncTable,
66
type DrizzleTablePowerSyncOptions,
77
type DrizzleTableWithPowerSyncOptions,
@@ -12,6 +12,7 @@ import {
1212
} from './utils/schema';
1313

1414
export {
15+
DrizzleAppSchema,
1516
DrizzleTablePowerSyncOptions,
1617
DrizzleTableWithPowerSyncOptions,
1718
Expand,
@@ -20,7 +21,6 @@ export {
2021
TableName,
2122
TablesFromSchemaEntries,
2223
toCompilableQuery,
23-
toPowerSyncSchema,
2424
toPowerSyncTable,
2525
wrapPowerSyncWithDrizzle
2626
};

packages/drizzle-driver/src/utils/schema.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,7 @@ export type TablesFromSchemaEntries<T> = {
9595
: never;
9696
};
9797

98-
export function toPowerSyncSchema<
99-
T extends Record<string, SQLiteTableWithColumns<any> | Relations | DrizzleTableWithPowerSyncOptions>
100-
>(schemaEntries: T): Schema<Expand<TablesFromSchemaEntries<T>>> {
101-
const tables: Record<string, Table> = {};
102-
for (const schemaEntry of Object.values(schemaEntries)) {
103-
let maybeTable: SQLiteTableWithColumns<any> | Relations | undefined = undefined;
104-
let maybeOptions: DrizzleTablePowerSyncOptions | undefined = undefined;
105-
106-
if (typeof schemaEntry === 'object' && 'tableDefinition' in schemaEntry) {
107-
const tableWithOptions = schemaEntry as DrizzleTableWithPowerSyncOptions;
108-
maybeTable = tableWithOptions.tableDefinition;
109-
maybeOptions = tableWithOptions.options;
110-
} else {
111-
maybeTable = schemaEntry;
112-
}
113-
114-
if (isTable(maybeTable)) {
115-
const { name } = getTableConfig(maybeTable);
116-
tables[name] = toPowerSyncTable(maybeTable as SQLiteTableWithColumns<TableConfig>, maybeOptions);
117-
}
118-
}
119-
120-
return new Schema(tables) as Schema<Expand<TablesFromSchemaEntries<T>>>;
121-
}
122-
123-
export function toPowerSyncTables<
98+
function toPowerSyncTables<
12499
T extends Record<string, SQLiteTableWithColumns<any> | Relations | DrizzleTableWithPowerSyncOptions>
125100
>(schemaEntries: T) {
126101
const tables: Record<string, Table> = {};

packages/drizzle-driver/tests/sqlite/schema.test.ts

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { column, Schema, Table } from '@powersync/common';
22
import { index, integer, real, sqliteTable, text } from 'drizzle-orm/sqlite-core';
33
import { describe, expect, it } from 'vitest';
4-
import {
5-
DrizzleAppSchema,
6-
DrizzleTableWithPowerSyncOptions,
7-
toPowerSyncSchema,
8-
toPowerSyncTable
9-
} from '../../src/utils/schema';
4+
import { DrizzleAppSchema, DrizzleTableWithPowerSyncOptions, toPowerSyncTable } from '../../src/utils/schema';
105

116
describe('toPowerSyncTable', () => {
127
it('basic conversion', () => {
@@ -29,36 +24,6 @@ describe('toPowerSyncTable', () => {
2924
expect(convertedList).toEqual(expectedLists);
3025
});
3126

32-
it('basic conversion class', () => {
33-
const lists = sqliteTable('lists', {
34-
id: text('id').primaryKey(),
35-
name: text('name').notNull(),
36-
owner_id: text('owner_id'),
37-
counter: integer('counter'),
38-
completion: real('completion')
39-
});
40-
const convertedList = new DrizzleAppSchema({ lists });
41-
42-
const a: (typeof convertedList)['types']['lists'] = {
43-
name: 'd',
44-
completion: 1,
45-
counter: 0,
46-
id: '1',
47-
owner_id: null
48-
};
49-
});
50-
51-
it('classed based types', () => {
52-
const lists = sqliteTable('lists', {
53-
id: text('id').primaryKey(),
54-
name: text('name').notNull(),
55-
owner_id: text('owner_id'),
56-
counter: integer('counter'),
57-
completion: real('completion')
58-
});
59-
const drizzle = new DrizzleAppSchema({ lists });
60-
});
61-
6227
it('conversion with index', () => {
6328
const lists = sqliteTable(
6429
'lists',
@@ -103,7 +68,7 @@ describe('toPowerSyncTable', () => {
10368
});
10469
});
10570

106-
describe('toPowerSyncSchema', () => {
71+
describe('DrizzleAppSchema constructor', () => {
10772
it('basic conversion', () => {
10873
const lists = sqliteTable('lists', {
10974
id: text('id').primaryKey(),
@@ -123,7 +88,8 @@ describe('toPowerSyncSchema', () => {
12388
lists,
12489
todos
12590
};
126-
const convertedSchema = toPowerSyncSchema(drizzleSchema);
91+
92+
const convertedSchema = new DrizzleAppSchema(drizzleSchema);
12793

12894
const expectedSchema = new Schema({
12995
lists: new Table({
@@ -138,7 +104,7 @@ describe('toPowerSyncSchema', () => {
138104
})
139105
});
140106

141-
expect(convertedSchema).toEqual(expectedSchema);
107+
expect(convertedSchema.tables).toEqual(expectedSchema.tables);
142108
});
143109

144110
it('conversion with options', () => {
@@ -164,7 +130,7 @@ describe('toPowerSyncSchema', () => {
164130
todos
165131
};
166132

167-
const convertedSchema = toPowerSyncSchema(drizzleSchemaWithOptions);
133+
const convertedSchema = new DrizzleAppSchema(drizzleSchemaWithOptions);
168134

169135
const expectedSchema = new Schema({
170136
lists: new Table(
@@ -182,7 +148,7 @@ describe('toPowerSyncSchema', () => {
182148
})
183149
});
184150

185-
expect(convertedSchema).toEqual(expectedSchema);
151+
expect(convertedSchema.tables).toEqual(expectedSchema.tables);
186152
});
187153

188154
it('conversion with index', () => {
@@ -211,7 +177,7 @@ describe('toPowerSyncSchema', () => {
211177
todos
212178
};
213179

214-
const convertedSchema = toPowerSyncSchema(drizzleSchemaWithOptions);
180+
const convertedSchema = new DrizzleAppSchema(drizzleSchemaWithOptions);
215181

216182
const expectedSchema = new Schema({
217183
lists: new Table({
@@ -229,6 +195,6 @@ describe('toPowerSyncSchema', () => {
229195
)
230196
});
231197

232-
expect(convertedSchema).toEqual(expectedSchema);
198+
expect(convertedSchema.tables).toEqual(expectedSchema.tables);
233199
});
234200
});

0 commit comments

Comments
 (0)