11import { 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' ;
33import {
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
5864export 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