@@ -84,7 +84,7 @@ export type Database = {
84
84
${ [
85
85
...columnsByTableId [ table . id ] . map (
86
86
( column ) =>
87
- `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType ( column . format , {
87
+ `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType ( schema , column . format , {
88
88
types,
89
89
schemas,
90
90
tables,
@@ -97,7 +97,12 @@ export type Database = {
97
97
const type = types . find ( ( { id } ) => id === fn . return_type_id )
98
98
let tsType = 'unknown'
99
99
if ( type ) {
100
- tsType = pgTypeToTsType ( type . name , { types, schemas, tables, views } )
100
+ tsType = pgTypeToTsType ( schema , type . name , {
101
+ types,
102
+ schemas,
103
+ tables,
104
+ views,
105
+ } )
101
106
}
102
107
return `${ JSON . stringify ( fn . name ) } : ${ tsType } | null`
103
108
} ) ,
@@ -121,7 +126,12 @@ export type Database = {
121
126
output += ':'
122
127
}
123
128
124
- output += pgTypeToTsType ( column . format , { types, schemas, tables, views } )
129
+ output += pgTypeToTsType ( schema , column . format , {
130
+ types,
131
+ schemas,
132
+ tables,
133
+ views,
134
+ } )
125
135
126
136
if ( column . is_nullable ) {
127
137
output += '| null'
@@ -138,7 +148,12 @@ export type Database = {
138
148
return `${ output } ?: never`
139
149
}
140
150
141
- output += `?: ${ pgTypeToTsType ( column . format , { types, schemas, tables, views } ) } `
151
+ output += `?: ${ pgTypeToTsType ( schema , column . format , {
152
+ types,
153
+ schemas,
154
+ tables,
155
+ views,
156
+ } ) } `
142
157
143
158
if ( column . is_nullable ) {
144
159
output += '| null'
@@ -189,7 +204,7 @@ export type Database = {
189
204
Row: {
190
205
${ columnsByTableId [ view . id ] . map (
191
206
( column ) =>
192
- `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType ( column . format , {
207
+ `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType ( schema , column . format , {
193
208
types,
194
209
schemas,
195
210
tables,
@@ -207,7 +222,12 @@ export type Database = {
207
222
return `${ output } ?: never`
208
223
}
209
224
210
- output += `?: ${ pgTypeToTsType ( column . format , { types, schemas, tables, views } ) } | null`
225
+ output += `?: ${ pgTypeToTsType ( schema , column . format , {
226
+ types,
227
+ schemas,
228
+ tables,
229
+ views,
230
+ } ) } | null`
211
231
212
232
return output
213
233
} ) }
@@ -220,7 +240,12 @@ export type Database = {
220
240
return `${ output } ?: never`
221
241
}
222
242
223
- output += `?: ${ pgTypeToTsType ( column . format , { types, schemas, tables, views } ) } | null`
243
+ output += `?: ${ pgTypeToTsType ( schema , column . format , {
244
+ types,
245
+ schemas,
246
+ tables,
247
+ views,
248
+ } ) } | null`
224
249
225
250
return output
226
251
} ) }
@@ -290,7 +315,12 @@ export type Database = {
290
315
const type = types . find ( ( { id } ) => id === type_id )
291
316
let tsType = 'unknown'
292
317
if ( type ) {
293
- tsType = pgTypeToTsType ( type . name , { types, schemas, tables, views } )
318
+ tsType = pgTypeToTsType ( schema , type . name , {
319
+ types,
320
+ schemas,
321
+ tables,
322
+ views,
323
+ } )
294
324
}
295
325
return { name, type : tsType , has_default }
296
326
} )
@@ -307,7 +337,12 @@ export type Database = {
307
337
const type = types . find ( ( { id } ) => id === type_id )
308
338
let tsType = 'unknown'
309
339
if ( type ) {
310
- tsType = pgTypeToTsType ( type . name , { types, schemas, tables, views } )
340
+ tsType = pgTypeToTsType ( schema , type . name , {
341
+ types,
342
+ schemas,
343
+ tables,
344
+ views,
345
+ } )
311
346
}
312
347
return { name, type : tsType }
313
348
} )
@@ -327,20 +362,29 @@ export type Database = {
327
362
return `{
328
363
${ columnsByTableId [ relation . id ] . map (
329
364
( column ) =>
330
- `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType ( column . format , {
331
- types,
332
- schemas,
333
- tables,
334
- views,
335
- } ) } ${ column . is_nullable ? '| null' : '' } `
365
+ `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
366
+ schema ,
367
+ column . format ,
368
+ {
369
+ types,
370
+ schemas,
371
+ tables,
372
+ views,
373
+ }
374
+ ) } ${ column . is_nullable ? '| null' : '' } `
336
375
) }
337
376
}`
338
377
}
339
378
340
379
// Case 3: returns base/array/composite/enum type.
341
380
const type = types . find ( ( { id } ) => id === fns [ 0 ] . return_type_id )
342
381
if ( type ) {
343
- return pgTypeToTsType ( type . name , { types, schemas, tables, views } )
382
+ return pgTypeToTsType ( schema , type . name , {
383
+ types,
384
+ schemas,
385
+ tables,
386
+ views,
387
+ } )
344
388
}
345
389
346
390
return 'unknown'
@@ -372,7 +416,12 @@ export type Database = {
372
416
const type = types . find ( ( { id } ) => id === type_id )
373
417
let tsType = 'unknown'
374
418
if ( type ) {
375
- tsType = `${ pgTypeToTsType ( type . name , { types, schemas, tables, views } ) } | null`
419
+ tsType = `${ pgTypeToTsType ( schema , type . name , {
420
+ types,
421
+ schemas,
422
+ tables,
423
+ views,
424
+ } ) } | null`
376
425
}
377
426
return `${ JSON . stringify ( name ) } : ${ tsType } `
378
427
} ) }
@@ -519,6 +568,7 @@ export const Constants = {
519
568
520
569
// TODO: Make this more robust. Currently doesn't handle range types - returns them as unknown.
521
570
const pgTypeToTsType = (
571
+ schema : PostgresSchema ,
522
572
pgType : string ,
523
573
{
524
574
types,
@@ -560,10 +610,16 @@ const pgTypeToTsType = (
560
610
} else if ( pgType === 'record' ) {
561
611
return 'Record<string, unknown>'
562
612
} else if ( pgType . startsWith ( '_' ) ) {
563
- return `(${ pgTypeToTsType ( pgType . substring ( 1 ) , { types, schemas, tables, views } ) } )[]`
613
+ return `(${ pgTypeToTsType ( schema , pgType . substring ( 1 ) , {
614
+ types,
615
+ schemas,
616
+ tables,
617
+ views,
618
+ } ) } )[]`
564
619
} else {
565
- const enumType = types . find ( ( type ) => type . name === pgType && type . enums . length > 0 )
566
- if ( enumType ) {
620
+ const enumTypes = types . filter ( ( type ) => type . name === pgType && type . enums . length > 0 )
621
+ if ( enumTypes . length > 0 ) {
622
+ const enumType = enumTypes . find ( ( type ) => type . schema === schema . name ) || enumTypes [ 0 ]
567
623
if ( schemas . some ( ( { name } ) => name === enumType . schema ) ) {
568
624
return `Database[${ JSON . stringify ( enumType . schema ) } ]['Enums'][${ JSON . stringify (
569
625
enumType . name
@@ -572,8 +628,12 @@ const pgTypeToTsType = (
572
628
return enumType . enums . map ( ( variant ) => JSON . stringify ( variant ) ) . join ( '|' )
573
629
}
574
630
575
- const compositeType = types . find ( ( type ) => type . name === pgType && type . attributes . length > 0 )
576
- if ( compositeType ) {
631
+ const compositeTypes = types . filter (
632
+ ( type ) => type . name === pgType && type . attributes . length > 0
633
+ )
634
+ if ( compositeTypes . length > 0 ) {
635
+ const compositeType =
636
+ compositeTypes . find ( ( type ) => type . schema === schema . name ) || compositeTypes [ 0 ]
577
637
if ( schemas . some ( ( { name } ) => name === compositeType . schema ) ) {
578
638
return `Database[${ JSON . stringify (
579
639
compositeType . schema
@@ -582,8 +642,10 @@ const pgTypeToTsType = (
582
642
return 'unknown'
583
643
}
584
644
585
- const tableRowType = tables . find ( ( table ) => table . name === pgType )
586
- if ( tableRowType ) {
645
+ const tableRowTypes = tables . filter ( ( table ) => table . name === pgType )
646
+ if ( tableRowTypes . length > 0 ) {
647
+ const tableRowType =
648
+ tableRowTypes . find ( ( type ) => type . schema === schema . name ) || tableRowTypes [ 0 ]
587
649
if ( schemas . some ( ( { name } ) => name === tableRowType . schema ) ) {
588
650
return `Database[${ JSON . stringify ( tableRowType . schema ) } ]['Tables'][${ JSON . stringify (
589
651
tableRowType . name
@@ -592,8 +654,10 @@ const pgTypeToTsType = (
592
654
return 'unknown'
593
655
}
594
656
595
- const viewRowType = views . find ( ( view ) => view . name === pgType )
596
- if ( viewRowType ) {
657
+ const viewRowTypes = views . filter ( ( view ) => view . name === pgType )
658
+ if ( viewRowTypes . length > 0 ) {
659
+ const viewRowType =
660
+ viewRowTypes . find ( ( type ) => type . schema === schema . name ) || viewRowTypes [ 0 ]
597
661
if ( schemas . some ( ( { name } ) => name === viewRowType . schema ) ) {
598
662
return `Database[${ JSON . stringify ( viewRowType . schema ) } ]['Views'][${ JSON . stringify (
599
663
viewRowType . name
0 commit comments