@@ -3,7 +3,9 @@ import type {
3
3
PostgresColumn ,
4
4
PostgresFunction ,
5
5
PostgresSchema ,
6
+ PostgresTable ,
6
7
PostgresType ,
8
+ PostgresView ,
7
9
} from '../../lib/index.js'
8
10
import type { GeneratorMetadata } from '../../lib/generators.js'
9
11
@@ -80,19 +82,20 @@ export type Database = {
80
82
${ [
81
83
...columnsByTableId [ table . id ] . map (
82
84
( column ) =>
83
- `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
84
- column . format ,
85
+ `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType ( column . format , {
85
86
types,
86
- schemas
87
- ) } ${ column . is_nullable ? '| null' : '' } `
87
+ schemas,
88
+ tables,
89
+ views,
90
+ } ) } ${ column . is_nullable ? '| null' : '' } `
88
91
) ,
89
92
...schemaFunctions
90
93
. filter ( ( fn ) => fn . argument_types === table . name )
91
94
. map ( ( fn ) => {
92
95
const type = types . find ( ( { id } ) => id === fn . return_type_id )
93
96
let tsType = 'unknown'
94
97
if ( type ) {
95
- tsType = pgTypeToTsType ( type . name , types , schemas )
98
+ tsType = pgTypeToTsType ( type . name , { types, schemas, tables , views } )
96
99
}
97
100
return `${ JSON . stringify ( fn . name ) } : ${ tsType } | null`
98
101
} ) ,
@@ -116,7 +119,7 @@ export type Database = {
116
119
output += ':'
117
120
}
118
121
119
- output += pgTypeToTsType ( column . format , types , schemas )
122
+ output += pgTypeToTsType ( column . format , { types, schemas, tables , views } )
120
123
121
124
if ( column . is_nullable ) {
122
125
output += '| null'
@@ -133,7 +136,7 @@ export type Database = {
133
136
return `${ output } ?: never`
134
137
}
135
138
136
- output += `?: ${ pgTypeToTsType ( column . format , types , schemas ) } `
139
+ output += `?: ${ pgTypeToTsType ( column . format , { types, schemas, tables , views } ) } `
137
140
138
141
if ( column . is_nullable ) {
139
142
output += '| null'
@@ -180,11 +183,12 @@ export type Database = {
180
183
Row: {
181
184
${ columnsByTableId [ view . id ] . map (
182
185
( column ) =>
183
- `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
184
- column . format ,
186
+ `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType ( column . format , {
185
187
types,
186
- schemas
187
- ) } ${ column . is_nullable ? '| null' : '' } `
188
+ schemas,
189
+ tables,
190
+ views,
191
+ } ) } ${ column . is_nullable ? '| null' : '' } `
188
192
) }
189
193
}
190
194
${
@@ -197,7 +201,7 @@ export type Database = {
197
201
return `${ output } ?: never`
198
202
}
199
203
200
- output += `?: ${ pgTypeToTsType ( column . format , types , schemas ) } | null`
204
+ output += `?: ${ pgTypeToTsType ( column . format , { types, schemas, tables , views } ) } | null`
201
205
202
206
return output
203
207
} ) }
@@ -210,7 +214,7 @@ export type Database = {
210
214
return `${ output } ?: never`
211
215
}
212
216
213
- output += `?: ${ pgTypeToTsType ( column . format , types , schemas ) } | null`
217
+ output += `?: ${ pgTypeToTsType ( column . format , { types, schemas, tables , views } ) } | null`
214
218
215
219
return output
216
220
} ) }
@@ -279,7 +283,7 @@ export type Database = {
279
283
const type = types . find ( ( { id } ) => id === type_id )
280
284
let tsType = 'unknown'
281
285
if ( type ) {
282
- tsType = pgTypeToTsType ( type . name , types , schemas )
286
+ tsType = pgTypeToTsType ( type . name , { types, schemas, tables , views } )
283
287
}
284
288
return { name, type : tsType , has_default }
285
289
} )
@@ -299,7 +303,7 @@ export type Database = {
299
303
const type = types . find ( ( { id } ) => id === type_id )
300
304
let tsType = 'unknown'
301
305
if ( type ) {
302
- tsType = pgTypeToTsType ( type . name , types , schemas )
306
+ tsType = pgTypeToTsType ( type . name , { types, schemas, tables , views } )
303
307
}
304
308
return { name, type : tsType }
305
309
} )
@@ -319,19 +323,20 @@ export type Database = {
319
323
return `{
320
324
${ columnsByTableId [ relation . id ] . map (
321
325
( column ) =>
322
- `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
323
- column . format ,
326
+ `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType ( column . format , {
324
327
types,
325
- schemas
326
- ) } ${ column . is_nullable ? '| null' : '' } `
328
+ schemas,
329
+ tables,
330
+ views,
331
+ } ) } ${ column . is_nullable ? '| null' : '' } `
327
332
) }
328
333
}`
329
334
}
330
335
331
336
// Case 3: returns base/array/composite/enum type.
332
337
const type = types . find ( ( { id } ) => id === return_type_id )
333
338
if ( type ) {
334
- return pgTypeToTsType ( type . name , types , schemas )
339
+ return pgTypeToTsType ( type . name , { types, schemas, tables , views } )
335
340
}
336
341
337
342
return 'unknown'
@@ -367,7 +372,7 @@ export type Database = {
367
372
const type = types . find ( ( { id } ) => id === type_id )
368
373
let tsType = 'unknown'
369
374
if ( type ) {
370
- tsType = `${ pgTypeToTsType ( type . name , types , schemas ) } | null`
375
+ tsType = `${ pgTypeToTsType ( type . name , { types, schemas, tables , views } ) } | null`
371
376
}
372
377
return `${ JSON . stringify ( name ) } : ${ tsType } `
373
378
} ) }
@@ -470,8 +475,17 @@ export type Enums<
470
475
// TODO: Make this more robust. Currently doesn't handle range types - returns them as unknown.
471
476
const pgTypeToTsType = (
472
477
pgType : string ,
473
- types : PostgresType [ ] ,
474
- schemas : PostgresSchema [ ]
478
+ {
479
+ types,
480
+ schemas,
481
+ tables,
482
+ views,
483
+ } : {
484
+ types : PostgresType [ ]
485
+ schemas : PostgresSchema [ ]
486
+ tables : PostgresTable [ ]
487
+ views : PostgresView [ ]
488
+ }
475
489
) : string => {
476
490
if ( pgType === 'bool' ) {
477
491
return 'boolean'
@@ -501,7 +515,7 @@ const pgTypeToTsType = (
501
515
} else if ( pgType === 'record' ) {
502
516
return 'Record<string, unknown>'
503
517
} else if ( pgType . startsWith ( '_' ) ) {
504
- return `(${ pgTypeToTsType ( pgType . substring ( 1 ) , types , schemas ) } )[]`
518
+ return `(${ pgTypeToTsType ( pgType . substring ( 1 ) , { types, schemas, tables , views } ) } )[]`
505
519
} else {
506
520
const enumType = types . find ( ( type ) => type . name === pgType && type . enums . length > 0 )
507
521
if ( enumType ) {
@@ -523,6 +537,26 @@ const pgTypeToTsType = (
523
537
return 'unknown'
524
538
}
525
539
540
+ const tableRowType = tables . find ( ( table ) => table . name === pgType )
541
+ if ( tableRowType ) {
542
+ if ( schemas . some ( ( { name } ) => name === tableRowType . schema ) ) {
543
+ return `Database[${ JSON . stringify ( tableRowType . schema ) } ]['Tables'][${ JSON . stringify (
544
+ tableRowType . name
545
+ ) } ]['Row']`
546
+ }
547
+ return 'unknown'
548
+ }
549
+
550
+ const viewRowType = views . find ( ( view ) => view . name === pgType )
551
+ if ( viewRowType ) {
552
+ if ( schemas . some ( ( { name } ) => name === viewRowType . schema ) ) {
553
+ return `Database[${ JSON . stringify ( viewRowType . schema ) } ]['Views'][${ JSON . stringify (
554
+ viewRowType . name
555
+ ) } ]['Row']`
556
+ }
557
+ return 'unknown'
558
+ }
559
+
526
560
return 'unknown'
527
561
}
528
562
}
0 commit comments