@@ -8,13 +8,16 @@ import {
88 type TableV2Options
99} from '@powersync/common' ;
1010import { InferSelectModel , isTable , Relations } from 'drizzle-orm' ;
11+ import type { Casing } from 'drizzle-orm' ;
12+ import { CasingCache } from 'drizzle-orm/casing' ;
1113import {
1214 getTableConfig ,
1315 SQLiteInteger ,
1416 SQLiteReal ,
1517 SQLiteText ,
1618 type SQLiteTableWithColumns ,
17- type TableConfig
19+ type TableConfig ,
20+ type SQLiteColumn
1821} from 'drizzle-orm/sqlite-core' ;
1922
2023export type ExtractPowerSyncColumns < T extends SQLiteTableWithColumns < any > > = {
@@ -25,14 +28,17 @@ export type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
2528
2629export function toPowerSyncTable < T extends SQLiteTableWithColumns < any > > (
2730 table : T ,
28- options ?: Omit < TableV2Options , 'indexes' >
31+ options ?: Omit < TableV2Options , 'indexes' > & { casingCache ?: CasingCache }
2932) : Table < Expand < ExtractPowerSyncColumns < T > > > {
3033 const { columns : drizzleColumns , indexes : drizzleIndexes } = getTableConfig ( table ) ;
34+ const { casingCache } = options ?? { } ;
3135
3236 const columns : { [ key : string ] : BaseColumnType < number | string | null > } = { } ;
3337 for ( const drizzleColumn of drizzleColumns ) {
38+ const name = casingCache ?. getColumnCasing ( drizzleColumn ) ?? drizzleColumn . name ;
39+
3440 // Skip the id column
35- if ( drizzleColumn . name === 'id' ) {
41+ if ( name === 'id' ) {
3642 continue ;
3743 }
3844
@@ -50,7 +56,7 @@ export function toPowerSyncTable<T extends SQLiteTableWithColumns<any>>(
5056 default :
5157 throw new Error ( `Unsupported column type: ${ drizzleColumn . columnType } ` ) ;
5258 }
53- columns [ drizzleColumn . name ] = mappedType ;
59+ columns [ name ] = mappedType ;
5460 }
5561 const indexes : IndexShorthand = { } ;
5662
@@ -61,7 +67,9 @@ export function toPowerSyncTable<T extends SQLiteTableWithColumns<any>>(
6167 }
6268 const columns : string [ ] = [ ] ;
6369 for ( const indexColumn of index . config . columns ) {
64- columns . push ( ( indexColumn as { name : string } ) . name ) ;
70+ const name = casingCache ?. getColumnCasing ( indexColumn as SQLiteColumn ) ?? ( indexColumn as { name : string } ) . name ;
71+
72+ columns . push ( name ) ;
6573 }
6674
6775 indexes [ index . config . name ] = columns ;
@@ -73,7 +81,7 @@ export type DrizzleTablePowerSyncOptions = Omit<TableV2Options, 'indexes'>;
7381
7482export type DrizzleTableWithPowerSyncOptions = {
7583 tableDefinition : SQLiteTableWithColumns < any > ;
76- options ?: DrizzleTablePowerSyncOptions | undefined ;
84+ options ?: DrizzleTablePowerSyncOptions ;
7785} ;
7886
7987export type TableName < T > =
@@ -97,7 +105,9 @@ export type TablesFromSchemaEntries<T> = {
97105
98106function toPowerSyncTables <
99107 T extends Record < string , SQLiteTableWithColumns < any > | Relations | DrizzleTableWithPowerSyncOptions >
100- > ( schemaEntries : T ) {
108+ > ( schemaEntries : T , options ?: DrizzleAppSchemaOptions ) {
109+ const casingCache = options ?. casing ? new CasingCache ( options ?. casing ) : undefined ;
110+
101111 const tables : Record < string , Table > = { } ;
102112 for ( const schemaEntry of Object . values ( schemaEntries ) ) {
103113 let maybeTable : SQLiteTableWithColumns < any > | Relations | undefined = undefined ;
@@ -113,18 +123,24 @@ function toPowerSyncTables<
113123
114124 if ( isTable ( maybeTable ) ) {
115125 const { name } = getTableConfig ( maybeTable ) ;
116- tables [ name ] = toPowerSyncTable ( maybeTable as SQLiteTableWithColumns < TableConfig > , maybeOptions ) ;
126+ tables [ name ] = toPowerSyncTable ( maybeTable as SQLiteTableWithColumns < TableConfig > , {
127+ ...maybeOptions ,
128+ casingCache
129+ } ) ;
117130 }
118131 }
119132
120133 return tables ;
121134}
122135
136+ export type DrizzleAppSchemaOptions = {
137+ casing ?: Casing ;
138+ } ;
123139export class DrizzleAppSchema <
124140 T extends Record < string , SQLiteTableWithColumns < any > | Relations | DrizzleTableWithPowerSyncOptions >
125141> extends Schema {
126- constructor ( drizzleSchema : T ) {
127- super ( toPowerSyncTables ( drizzleSchema ) ) ;
142+ constructor ( drizzleSchema : T , options ?: DrizzleAppSchemaOptions ) {
143+ super ( toPowerSyncTables ( drizzleSchema , options ) ) ;
128144 // This is just used for typing
129145 this . types = { } as SchemaTableType < Expand < TablesFromSchemaEntries < T > > > ;
130146 }
0 commit comments