Skip to content

Commit 1e0b18b

Browse files
committed
Added toPowerSyncSchema()
1 parent a1a8bd9 commit 1e0b18b

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import { wrapPowerSyncWithDrizzle, type PowerSyncSQLiteDatabase } from './sqlite/db';
22
import { toCompilableQuery } from './utils/compilableQuery';
3-
import { toPowerSyncTable } from './utils/schema';
3+
import {
4+
toPowerSyncTable,
5+
toPowerSyncSchema,
6+
DrizzleTablePowerSyncOptions,
7+
DrizzleTableWithPowerSyncOptions
8+
} from './utils/schema';
49

5-
export { wrapPowerSyncWithDrizzle, toCompilableQuery, toPowerSyncTable, PowerSyncSQLiteDatabase };
10+
export {
11+
wrapPowerSyncWithDrizzle,
12+
toCompilableQuery,
13+
toPowerSyncTable,
14+
toPowerSyncSchema,
15+
DrizzleTablePowerSyncOptions,
16+
DrizzleTableWithPowerSyncOptions,
17+
PowerSyncSQLiteDatabase
18+
};

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { column, IndexShorthand, Table, type BaseColumnType, type TableV2Options } from '@powersync/common';
1+
import { column, IndexShorthand, Schema, Table, type BaseColumnType, type TableV2Options } from '@powersync/common';
2+
import { isTable, Relations } from 'drizzle-orm';
23
import {
34
getTableConfig,
45
SQLiteInteger,
@@ -53,3 +54,35 @@ export function toPowerSyncTable<T extends TableConfig>(
5354
}
5455
return new Table(columns, { ...options, indexes });
5556
}
57+
58+
export type DrizzleTablePowerSyncOptions = Omit<TableV2Options, 'indexes'>;
59+
60+
export type DrizzleTableWithPowerSyncOptions = {
61+
tableDefinition: SQLiteTableWithColumns<any>;
62+
options?: DrizzleTablePowerSyncOptions | undefined;
63+
};
64+
65+
export function toPowerSyncSchema(
66+
schemaEntries: Record<string, SQLiteTableWithColumns<any> | Relations | DrizzleTableWithPowerSyncOptions>
67+
) {
68+
const tables: Record<string, Table> = {};
69+
for (const schemaEntry of Object.values(schemaEntries)) {
70+
let maybeTable: SQLiteTableWithColumns<any> | Relations | undefined = undefined;
71+
let maybeOptions: DrizzleTablePowerSyncOptions | undefined = undefined;
72+
73+
if (typeof schemaEntry === 'object' && 'tableDefinition' in schemaEntry) {
74+
const tableWithOptions = schemaEntry as DrizzleTableWithPowerSyncOptions;
75+
maybeTable = tableWithOptions.tableDefinition;
76+
maybeOptions = tableWithOptions.options;
77+
} else {
78+
maybeTable = schemaEntry;
79+
}
80+
81+
if (isTable(maybeTable)) {
82+
const { name } = getTableConfig(maybeTable);
83+
tables[name] = toPowerSyncTable(maybeTable as SQLiteTableWithColumns<TableConfig>, maybeOptions);
84+
}
85+
}
86+
87+
return new Schema(tables);
88+
}

0 commit comments

Comments
 (0)