@@ -19,7 +19,6 @@ export const apply = async ({
19
19
relationships,
20
20
functions,
21
21
types,
22
- arrayTypes,
23
22
detectOneToOneRelationships,
24
23
} : {
25
24
schemas : PostgresSchema [ ]
@@ -30,7 +29,6 @@ export const apply = async ({
30
29
relationships : PostgresRelationship [ ]
31
30
functions : PostgresFunction [ ]
32
31
types : PostgresType [ ]
33
- arrayTypes : PostgresType [ ]
34
32
detectOneToOneRelationships : boolean
35
33
} ) : Promise < string > => {
36
34
const columnsByTableId = Object . fromEntries < PostgresColumn [ ] > (
@@ -102,12 +100,9 @@ export type Database = {
102
100
...schemaFunctions
103
101
. filter ( ( fn ) => fn . argument_types === table . name )
104
102
. map ( ( fn ) => {
105
- const type = types . find ( ( { id } ) => id === fn . return_type_id )
106
- let tsType = 'unknown'
107
- if ( type ) {
108
- tsType = pgTypeToTsType ( type . name , types , schemas )
109
- }
110
- return `${ JSON . stringify ( fn . name ) } : ${ tsType } | null`
103
+ const pgType = types . find ( ( { id } ) => id === fn . return_type_id )
104
+ const type = pgTypeToTsType ( pgType ?. name , types , schemas )
105
+ return `${ JSON . stringify ( fn . name ) } : ${ type } | null`
111
106
} ) ,
112
107
] }
113
108
}
@@ -289,25 +284,9 @@ export type Database = {
289
284
}
290
285
291
286
const argsNameAndType = inArgs . map ( ( { name, type_id, has_default } ) => {
292
- let type = arrayTypes . find ( ( { id } ) => id === type_id )
293
- if ( type ) {
294
- // If it's an array type, the name looks like `_int8`.
295
- const elementTypeName = type . name . substring ( 1 )
296
- return {
297
- name,
298
- type : `(${ pgTypeToTsType ( elementTypeName , types , schemas ) } )[]` ,
299
- has_default,
300
- }
301
- }
302
- type = types . find ( ( { id } ) => id === type_id )
303
- if ( type ) {
304
- return {
305
- name,
306
- type : pgTypeToTsType ( type . name , types , schemas ) ,
307
- has_default,
308
- }
309
- }
310
- return { name, type : 'unknown' , has_default }
287
+ const pgType = types . find ( ( { id } ) => id === type_id )
288
+ const type = pgTypeToTsType ( pgType ?. name , types , schemas )
289
+ return { name, type, has_default }
311
290
} )
312
291
313
292
return `{
@@ -322,20 +301,9 @@ export type Database = {
322
301
const tableArgs = args . filter ( ( { mode } ) => mode === 'table' )
323
302
if ( tableArgs . length > 0 ) {
324
303
const argsNameAndType = tableArgs . map ( ( { name, type_id } ) => {
325
- let type = arrayTypes . find ( ( { id } ) => id === type_id )
326
- if ( type ) {
327
- // If it's an array type, the name looks like `_int8`.
328
- const elementTypeName = type . name . substring ( 1 )
329
- return {
330
- name,
331
- type : `(${ pgTypeToTsType ( elementTypeName , types , schemas ) } )[]` ,
332
- }
333
- }
334
- type = types . find ( ( { id } ) => id === type_id )
335
- if ( type ) {
336
- return { name, type : pgTypeToTsType ( type . name , types , schemas ) }
337
- }
338
- return { name, type : 'unknown' }
304
+ const pgType = types . find ( ( { id } ) => id === type_id )
305
+ const type = pgTypeToTsType ( pgType ?. name , types , schemas )
306
+ return { name, type }
339
307
} )
340
308
341
309
return `{
@@ -364,11 +332,7 @@ export type Database = {
364
332
365
333
// Case 3: returns base/composite/enum type.
366
334
const type = types . find ( ( { id } ) => id === return_type_id )
367
- if ( type ) {
368
- return pgTypeToTsType ( type . name , types , schemas )
369
- }
370
-
371
- return 'unknown'
335
+ return pgTypeToTsType ( type ?. name , types , schemas )
372
336
} ) ( ) } )${ is_set_returning_function ? '[]' : '' }
373
337
}`
374
338
)
@@ -398,15 +362,9 @@ export type Database = {
398
362
( { name, attributes } ) =>
399
363
`${ JSON . stringify ( name ) } : {
400
364
${ attributes . map ( ( { name, type_id } ) => {
401
- const type = types . find ( ( { id } ) => id === type_id )
402
- if ( type ) {
403
- return `${ JSON . stringify ( name ) } : ${ pgTypeToTsType (
404
- type . name ,
405
- types ,
406
- schemas
407
- ) } `
408
- }
409
- return `${ JSON . stringify ( name ) } : unknown`
365
+ const pgType = types . find ( ( { id } ) => id === type_id )
366
+ const type = pgTypeToTsType ( pgType ?. name , types , schemas )
367
+ return `${ JSON . stringify ( name ) } : ${ type } `
410
368
} ) }
411
369
}`
412
370
)
@@ -506,11 +464,13 @@ export type Enums<
506
464
507
465
// TODO: Make this more robust. Currently doesn't handle range types - returns them as unknown.
508
466
const pgTypeToTsType = (
509
- pgType : string ,
467
+ pgType : string | undefined ,
510
468
types : PostgresType [ ] ,
511
469
schemas : PostgresSchema [ ]
512
470
) : string => {
513
- if ( pgType === 'bool' ) {
471
+ if ( pgType === undefined ) {
472
+ return 'unknown'
473
+ } else if ( pgType === 'bool' ) {
514
474
return 'boolean'
515
475
} else if ( [ 'int2' , 'int4' , 'int8' , 'float4' , 'float8' , 'numeric' ] . includes ( pgType ) ) {
516
476
return 'number'
0 commit comments