@@ -28,6 +28,7 @@ export interface Database {
28
28
const schemaTables = tables . filter ( ( table ) => table . schema === schema . name )
29
29
const schemaViews = views . filter ( ( view ) => view . schema === schema . name )
30
30
const schemaFunctions = functions . filter ( ( func ) => func . schema === schema . name )
31
+ const schemaEnums = types . filter ( ( type ) => type . schema === schema . name && type . enums . length > 0 )
31
32
return `${ JSON . stringify ( schema . name ) } : {
32
33
Tables: {
33
34
${
@@ -38,9 +39,11 @@ export interface Database {
38
39
Row: {
39
40
${ table . columns . map (
40
41
( column ) =>
41
- `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType ( column . format , types ) } ${
42
- column . is_nullable ? '| null' : ''
43
- } `
42
+ `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
43
+ column . format ,
44
+ types ,
45
+ schemas
46
+ ) } ${ column . is_nullable ? '| null' : '' } `
44
47
) }
45
48
}
46
49
Insert: {
@@ -61,7 +64,7 @@ export interface Database {
61
64
output += ':'
62
65
}
63
66
64
- output += pgTypeToTsType ( column . format , types )
67
+ output += pgTypeToTsType ( column . format , types , schemas )
65
68
66
69
if ( column . is_nullable ) {
67
70
output += '| null'
@@ -78,7 +81,7 @@ export interface Database {
78
81
return `${ output } ?: never`
79
82
}
80
83
81
- output += `?: ${ pgTypeToTsType ( column . format , types ) } `
84
+ output += `?: ${ pgTypeToTsType ( column . format , types , schemas ) } `
82
85
83
86
if ( column . is_nullable ) {
84
87
output += '| null'
@@ -102,7 +105,8 @@ export interface Database {
102
105
( column ) =>
103
106
`${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
104
107
column . format ,
105
- types
108
+ types ,
109
+ schemas
106
110
) } | null`
107
111
) }
108
112
}
@@ -116,7 +120,7 @@ export interface Database {
116
120
return `${ output } ?: never`
117
121
}
118
122
119
- output += `?: ${ pgTypeToTsType ( column . format , types ) } | null`
123
+ output += `?: ${ pgTypeToTsType ( column . format , types , schemas ) } | null`
120
124
121
125
return output
122
126
} ) }
@@ -133,7 +137,7 @@ export interface Database {
133
137
return `${ output } ?: never`
134
138
}
135
139
136
- output += `?: ${ pgTypeToTsType ( column . format , types ) } | null`
140
+ output += `?: ${ pgTypeToTsType ( column . format , types , schemas ) } | null`
137
141
138
142
return output
139
143
} ) }
@@ -177,20 +181,32 @@ export interface Database {
177
181
if ( ! type ) {
178
182
return { name, type : 'unknown' }
179
183
}
180
- return { name, type : pgTypeToTsType ( type . name , types ) }
184
+ return { name, type : pgTypeToTsType ( type . name , types , schemas ) }
181
185
} )
182
186
183
187
return `{ ${ argsNameAndType . map (
184
188
( { name, type } ) => `${ JSON . stringify ( name ) } : ${ type } `
185
189
) } }`
186
190
} ) ( ) }
187
- Returns: ${ pgTypeToTsType ( fn . return_type , types ) }
191
+ Returns: ${ pgTypeToTsType ( fn . return_type , types , schemas ) }
188
192
}`
189
193
)
190
194
. join ( '|' ) } `
191
195
)
192
196
} ) ( ) }
193
197
}
198
+ Enums: {
199
+ ${
200
+ schemaEnums . length === 0
201
+ ? '[_ in never]: never'
202
+ : schemaEnums . map (
203
+ ( enum_ ) =>
204
+ `${ JSON . stringify ( enum_ . name ) } : ${ enum_ . enums
205
+ . map ( ( variant ) => JSON . stringify ( variant ) )
206
+ . join ( '|' ) } `
207
+ )
208
+ }
209
+ }
194
210
}`
195
211
} ) }
196
212
}`
@@ -202,7 +218,11 @@ export interface Database {
202
218
}
203
219
204
220
// TODO: Make this more robust. Currently doesn't handle composite types - returns them as unknown.
205
- const pgTypeToTsType = ( pgType : string , types : PostgresType [ ] ) : string => {
221
+ const pgTypeToTsType = (
222
+ pgType : string ,
223
+ types : PostgresType [ ] ,
224
+ schemas : PostgresSchema [ ]
225
+ ) : string => {
206
226
if ( pgType === 'bool' ) {
207
227
return 'boolean'
208
228
} else if ( [ 'int2' , 'int4' , 'int8' , 'float4' , 'float8' , 'numeric' ] . includes ( pgType ) ) {
@@ -229,10 +249,13 @@ const pgTypeToTsType = (pgType: string, types: PostgresType[]): string => {
229
249
} else if ( pgType === 'record' ) {
230
250
return 'Record<string, unknown>[]'
231
251
} else if ( pgType . startsWith ( '_' ) ) {
232
- return `(${ pgTypeToTsType ( pgType . substring ( 1 ) , types ) } )[]`
252
+ return `(${ pgTypeToTsType ( pgType . substring ( 1 ) , types , schemas ) } )[]`
233
253
} else {
234
254
const type = types . find ( ( type ) => type . name === pgType && type . enums . length > 0 )
235
255
if ( type ) {
256
+ if ( schemas . some ( ( { name } ) => name === type . schema ) ) {
257
+ return `Database[${ JSON . stringify ( type . schema ) } ]['Enums'][${ JSON . stringify ( type . name ) } ]`
258
+ }
236
259
return type . enums . map ( ( variant ) => JSON . stringify ( variant ) ) . join ( '|' )
237
260
}
238
261
0 commit comments