@@ -8,13 +8,16 @@ import {
8
8
type TableV2Options
9
9
} from '@powersync/common' ;
10
10
import { InferSelectModel , isTable , Relations } from 'drizzle-orm' ;
11
+ import type { Casing } from 'drizzle-orm' ;
12
+ import { CasingCache } from 'drizzle-orm/casing' ;
11
13
import {
12
14
getTableConfig ,
13
15
SQLiteInteger ,
14
16
SQLiteReal ,
15
17
SQLiteText ,
16
18
type SQLiteTableWithColumns ,
17
- type TableConfig
19
+ type TableConfig ,
20
+ type SQLiteColumn
18
21
} from 'drizzle-orm/sqlite-core' ;
19
22
20
23
export 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;
25
28
26
29
export function toPowerSyncTable < T extends SQLiteTableWithColumns < any > > (
27
30
table : T ,
28
- options ?: Omit < TableV2Options , 'indexes' >
31
+ options ?: Omit < TableV2Options , 'indexes' > & { casingCache ?: CasingCache }
29
32
) : Table < Expand < ExtractPowerSyncColumns < T > > > {
30
33
const { columns : drizzleColumns , indexes : drizzleIndexes } = getTableConfig ( table ) ;
34
+ const { casingCache } = options ?? { } ;
31
35
32
36
const columns : { [ key : string ] : BaseColumnType < number | string | null > } = { } ;
33
37
for ( const drizzleColumn of drizzleColumns ) {
38
+ const name = casingCache ?. getColumnCasing ( drizzleColumn ) ?? drizzleColumn . name ;
39
+
34
40
// Skip the id column
35
- if ( drizzleColumn . name === 'id' ) {
41
+ if ( name === 'id' ) {
36
42
continue ;
37
43
}
38
44
@@ -50,7 +56,7 @@ export function toPowerSyncTable<T extends SQLiteTableWithColumns<any>>(
50
56
default :
51
57
throw new Error ( `Unsupported column type: ${ drizzleColumn . columnType } ` ) ;
52
58
}
53
- columns [ drizzleColumn . name ] = mappedType ;
59
+ columns [ name ] = mappedType ;
54
60
}
55
61
const indexes : IndexShorthand = { } ;
56
62
@@ -61,7 +67,9 @@ export function toPowerSyncTable<T extends SQLiteTableWithColumns<any>>(
61
67
}
62
68
const columns : string [ ] = [ ] ;
63
69
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 ) ;
65
73
}
66
74
67
75
indexes [ index . config . name ] = columns ;
@@ -73,7 +81,7 @@ export type DrizzleTablePowerSyncOptions = Omit<TableV2Options, 'indexes'>;
73
81
74
82
export type DrizzleTableWithPowerSyncOptions = {
75
83
tableDefinition : SQLiteTableWithColumns < any > ;
76
- options ?: DrizzleTablePowerSyncOptions | undefined ;
84
+ options ?: DrizzleTablePowerSyncOptions ;
77
85
} ;
78
86
79
87
export type TableName < T > =
@@ -97,7 +105,9 @@ export type TablesFromSchemaEntries<T> = {
97
105
98
106
function toPowerSyncTables <
99
107
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
+
101
111
const tables : Record < string , Table > = { } ;
102
112
for ( const schemaEntry of Object . values ( schemaEntries ) ) {
103
113
let maybeTable : SQLiteTableWithColumns < any > | Relations | undefined = undefined ;
@@ -113,18 +123,24 @@ function toPowerSyncTables<
113
123
114
124
if ( isTable ( maybeTable ) ) {
115
125
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
+ } ) ;
117
130
}
118
131
}
119
132
120
133
return tables ;
121
134
}
122
135
136
+ export type DrizzleAppSchemaOptions = {
137
+ casing ?: Casing ;
138
+ } ;
123
139
export class DrizzleAppSchema <
124
140
T extends Record < string , SQLiteTableWithColumns < any > | Relations | DrizzleTableWithPowerSyncOptions >
125
141
> extends Schema {
126
- constructor ( drizzleSchema : T ) {
127
- super ( toPowerSyncTables ( drizzleSchema ) ) ;
142
+ constructor ( drizzleSchema : T , options ?: DrizzleAppSchemaOptions ) {
143
+ super ( toPowerSyncTables ( drizzleSchema , options ) ) ;
128
144
// This is just used for typing
129
145
this . types = { } as SchemaTableType < Expand < TablesFromSchemaEntries < T > > > ;
130
146
}
0 commit comments