Skip to content

Commit 4b7c60f

Browse files
committed
Added working types to toPowerSyncTable and toPowerSyncSchema.
1 parent 1e0b18b commit 4b7c60f

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import { wrapPowerSyncWithDrizzle, type PowerSyncSQLiteDatabase } from './sqlite/db';
22
import { toCompilableQuery } from './utils/compilableQuery';
33
import {
4-
toPowerSyncTable,
54
toPowerSyncSchema,
6-
DrizzleTablePowerSyncOptions,
7-
DrizzleTableWithPowerSyncOptions
5+
toPowerSyncTable,
6+
type DrizzleTablePowerSyncOptions,
7+
type DrizzleTableWithPowerSyncOptions,
8+
type Expand,
9+
type ExtractPowerSyncColumns,
10+
type TableName,
11+
type TablesFromSchemaEntries
812
} from './utils/schema';
913

1014
export {
11-
wrapPowerSyncWithDrizzle,
12-
toCompilableQuery,
13-
toPowerSyncTable,
14-
toPowerSyncSchema,
1515
DrizzleTablePowerSyncOptions,
1616
DrizzleTableWithPowerSyncOptions,
17-
PowerSyncSQLiteDatabase
17+
Expand,
18+
ExtractPowerSyncColumns,
19+
PowerSyncSQLiteDatabase,
20+
TableName,
21+
TablesFromSchemaEntries,
22+
toCompilableQuery,
23+
toPowerSyncSchema,
24+
toPowerSyncTable,
25+
wrapPowerSyncWithDrizzle
1826
};

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { column, IndexShorthand, Schema, Table, type BaseColumnType, type TableV2Options } from '@powersync/common';
2-
import { isTable, Relations } from 'drizzle-orm';
2+
import { InferSelectModel, isTable, Relations } from 'drizzle-orm';
33
import {
44
getTableConfig,
55
SQLiteInteger,
@@ -9,10 +9,16 @@ import {
99
type TableConfig
1010
} from 'drizzle-orm/sqlite-core';
1111

12-
export function toPowerSyncTable<T extends TableConfig>(
13-
table: SQLiteTableWithColumns<T>,
12+
export type ExtractPowerSyncColumns<T extends SQLiteTableWithColumns<any>> = {
13+
[K in keyof InferSelectModel<T> as K extends 'id' ? never : K]: BaseColumnType<InferSelectModel<T>[K]>;
14+
};
15+
16+
export type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
17+
18+
export function toPowerSyncTable<T extends SQLiteTableWithColumns<any>>(
19+
table: T,
1420
options?: Omit<TableV2Options, 'indexes'>
15-
) {
21+
): Table<Expand<ExtractPowerSyncColumns<T>>> {
1622
const { columns: drizzleColumns, indexes: drizzleIndexes } = getTableConfig(table);
1723

1824
const columns: { [key: string]: BaseColumnType<number | string | null> } = {};
@@ -52,7 +58,7 @@ export function toPowerSyncTable<T extends TableConfig>(
5258

5359
indexes[index.config.name] = columns;
5460
}
55-
return new Table(columns, { ...options, indexes });
61+
return new Table(columns, { ...options, indexes }) as Table<Expand<ExtractPowerSyncColumns<T>>>;
5662
}
5763

5864
export type DrizzleTablePowerSyncOptions = Omit<TableV2Options, 'indexes'>;
@@ -62,9 +68,28 @@ export type DrizzleTableWithPowerSyncOptions = {
6268
options?: DrizzleTablePowerSyncOptions | undefined;
6369
};
6470

65-
export function toPowerSyncSchema(
66-
schemaEntries: Record<string, SQLiteTableWithColumns<any> | Relations | DrizzleTableWithPowerSyncOptions>
67-
) {
71+
export type TableName<T> =
72+
T extends SQLiteTableWithColumns<any>
73+
? T['_']['name']
74+
: T extends DrizzleTableWithPowerSyncOptions
75+
? T['tableDefinition']['_']['name']
76+
: never;
77+
78+
export type TablesFromSchemaEntries<T> = {
79+
[K in keyof T as T[K] extends Relations
80+
? never
81+
: T[K] extends SQLiteTableWithColumns<any> | DrizzleTableWithPowerSyncOptions
82+
? TableName<T[K]>
83+
: never]: T[K] extends SQLiteTableWithColumns<any>
84+
? Table<Expand<ExtractPowerSyncColumns<T[K]>>>
85+
: T[K] extends DrizzleTableWithPowerSyncOptions
86+
? Table<Expand<ExtractPowerSyncColumns<T[K]['tableDefinition']>>>
87+
: never;
88+
};
89+
90+
export function toPowerSyncSchema<
91+
T extends Record<string, SQLiteTableWithColumns<any> | Relations | DrizzleTableWithPowerSyncOptions>
92+
>(schemaEntries: T): Schema<Expand<TablesFromSchemaEntries<T>>> {
6893
const tables: Record<string, Table> = {};
6994
for (const schemaEntry of Object.values(schemaEntries)) {
7095
let maybeTable: SQLiteTableWithColumns<any> | Relations | undefined = undefined;
@@ -84,5 +109,5 @@ export function toPowerSyncSchema(
84109
}
85110
}
86111

87-
return new Schema(tables);
112+
return new Schema(tables) as Schema<Expand<TablesFromSchemaEntries<T>>>;
88113
}

0 commit comments

Comments
 (0)