Skip to content

Commit 9d1f2af

Browse files
class method test
1 parent 1a1dca7 commit 9d1f2af

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,40 @@ export function toPowerSyncSchema<
111111

112112
return new Schema(tables) as Schema<Expand<TablesFromSchemaEntries<T>>>;
113113
}
114+
115+
export function toPowerSyncTables<
116+
T extends Record<string, SQLiteTableWithColumns<any> | Relations | DrizzleTableWithPowerSyncOptions>
117+
>(schemaEntries: T) {
118+
const tables: Record<string, Table> = {};
119+
for (const schemaEntry of Object.values(schemaEntries)) {
120+
let maybeTable: SQLiteTableWithColumns<any> | Relations | undefined = undefined;
121+
let maybeOptions: DrizzleTablePowerSyncOptions | undefined = undefined;
122+
123+
if (typeof schemaEntry === 'object' && 'tableDefinition' in schemaEntry) {
124+
const tableWithOptions = schemaEntry as DrizzleTableWithPowerSyncOptions;
125+
maybeTable = tableWithOptions.tableDefinition;
126+
maybeOptions = tableWithOptions.options;
127+
} else {
128+
maybeTable = schemaEntry;
129+
}
130+
131+
if (isTable(maybeTable)) {
132+
const { name } = getTableConfig(maybeTable);
133+
tables[name] = toPowerSyncTable(maybeTable as SQLiteTableWithColumns<TableConfig>, maybeOptions);
134+
}
135+
}
136+
137+
return tables;
138+
}
139+
140+
export class DrizzleAppSchema<
141+
T extends Record<string, SQLiteTableWithColumns<any> | Relations | DrizzleTableWithPowerSyncOptions>
142+
> extends Schema {
143+
constructor(drizzleSchema: T) {
144+
super(toPowerSyncTables(drizzleSchema));
145+
// This is just used for typing
146+
this.types = {} as any;
147+
}
148+
149+
types: Expand<TablesFromSchemaEntries<T>>;
150+
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
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 { DrizzleTableWithPowerSyncOptions, toPowerSyncSchema, toPowerSyncTable } from '../../src/utils/schema';
4+
import {
5+
DrizzleAppSchema,
6+
DrizzleTableWithPowerSyncOptions,
7+
toPowerSyncSchema,
8+
toPowerSyncTable
9+
} from '../../src/utils/schema';
510

611
describe('toPowerSyncTable', () => {
712
it('basic conversion', () => {
@@ -24,6 +29,17 @@ describe('toPowerSyncTable', () => {
2429
expect(convertedList).toEqual(expectedLists);
2530
});
2631

32+
it('classed based types', () => {
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 drizzle = new DrizzleAppSchema({ lists });
41+
});
42+
2743
it('conversion with index', () => {
2844
const lists = sqliteTable(
2945
'lists',

0 commit comments

Comments
 (0)