@@ -60,6 +60,9 @@ export interface Database {
60
60
const schemaEnums = types
61
61
. filter ( ( type ) => type . schema === schema . name && type . enums . length > 0 )
62
62
. sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
63
+ const schemaCompositeTypes = types
64
+ . filter ( ( type ) => type . schema === schema . name && type . attributes . length > 0 )
65
+ . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
63
66
return `${ JSON . stringify ( schema . name ) } : {
64
67
Tables: {
65
68
${
@@ -294,6 +297,28 @@ export interface Database {
294
297
)
295
298
}
296
299
}
300
+ CompositeTypes: {
301
+ ${
302
+ schemaCompositeTypes . length === 0
303
+ ? '[_ in never]: never'
304
+ : schemaCompositeTypes . map (
305
+ ( { name, attributes } ) =>
306
+ `${ JSON . stringify ( name ) } : {
307
+ ${ attributes . map ( ( { name, type_id } ) => {
308
+ const type = types . find ( ( { id } ) => id === type_id )
309
+ if ( type ) {
310
+ return `${ JSON . stringify ( name ) } : ${ pgTypeToTsType (
311
+ type . name ,
312
+ types ,
313
+ schemas
314
+ ) } `
315
+ }
316
+ return 'unknown'
317
+ } ) }
318
+ }`
319
+ )
320
+ }
321
+ }
297
322
}`
298
323
} ) }
299
324
}`
@@ -305,7 +330,7 @@ export interface Database {
305
330
return output
306
331
}
307
332
308
- // TODO: Make this more robust. Currently doesn't handle composite types - returns them as unknown.
333
+ // TODO: Make this more robust. Currently doesn't handle range types - returns them as unknown.
309
334
const pgTypeToTsType = (
310
335
pgType : string ,
311
336
types : PostgresType [ ] ,
@@ -339,12 +364,24 @@ const pgTypeToTsType = (
339
364
} else if ( pgType . startsWith ( '_' ) ) {
340
365
return `(${ pgTypeToTsType ( pgType . substring ( 1 ) , types , schemas ) } )[]`
341
366
} else {
342
- const type = types . find ( ( type ) => type . name === pgType && type . enums . length > 0 )
343
- if ( type ) {
344
- if ( schemas . some ( ( { name } ) => name === type . schema ) ) {
345
- return `Database[${ JSON . stringify ( type . schema ) } ]['Enums'][${ JSON . stringify ( type . name ) } ]`
367
+ const enumType = types . find ( ( type ) => type . name === pgType && type . enums . length > 0 )
368
+ if ( enumType ) {
369
+ if ( schemas . some ( ( { name } ) => name === enumType . schema ) ) {
370
+ return `Database[${ JSON . stringify ( enumType . schema ) } ]['Enums'][${ JSON . stringify (
371
+ enumType . name
372
+ ) } ]`
373
+ }
374
+ return enumType . enums . map ( ( variant ) => JSON . stringify ( variant ) ) . join ( '|' )
375
+ }
376
+
377
+ const compositeType = types . find ( ( type ) => type . name === pgType && type . attributes . length > 0 )
378
+ if ( compositeType ) {
379
+ if ( schemas . some ( ( { name } ) => name === compositeType . schema ) ) {
380
+ return `Database[${ JSON . stringify (
381
+ compositeType . schema
382
+ ) } ]['CompositeTypes'][${ JSON . stringify ( compositeType . name ) } ]`
346
383
}
347
- return type . enums . map ( ( variant ) => JSON . stringify ( variant ) ) . join ( '|' )
384
+ return 'unknown'
348
385
}
349
386
350
387
return 'unknown'
0 commit comments