@@ -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 [ ] ,
@@ -340,12 +365,24 @@ const pgTypeToTsType = (
340
365
} else if ( pgType . startsWith ( '_' ) ) {
341
366
return `(${ pgTypeToTsType ( pgType . substring ( 1 ) , types , schemas ) } )[]`
342
367
} else {
343
- const type = types . find ( ( type ) => type . name === pgType && type . enums . length > 0 )
344
- if ( type ) {
345
- if ( schemas . some ( ( { name } ) => name === type . schema ) ) {
346
- return `Database[${ JSON . stringify ( type . schema ) } ]['Enums'][${ JSON . stringify ( type . name ) } ]`
368
+ const enumType = types . find ( ( type ) => type . name === pgType && type . enums . length > 0 )
369
+ if ( enumType ) {
370
+ if ( schemas . some ( ( { name } ) => name === enumType . schema ) ) {
371
+ return `Database[${ JSON . stringify ( enumType . schema ) } ]['Enums'][${ JSON . stringify (
372
+ enumType . name
373
+ ) } ]`
374
+ }
375
+ return enumType . enums . map ( ( variant ) => JSON . stringify ( variant ) ) . join ( '|' )
376
+ }
377
+
378
+ const compositeType = types . find ( ( type ) => type . name === pgType && type . attributes . length > 0 )
379
+ if ( compositeType ) {
380
+ if ( schemas . some ( ( { name } ) => name === compositeType . schema ) ) {
381
+ return `Database[${ JSON . stringify (
382
+ compositeType . schema
383
+ ) } ]['CompositeTypes'][${ JSON . stringify ( compositeType . name ) } ]`
347
384
}
348
- return type . enums . map ( ( variant ) => JSON . stringify ( variant ) ) . join ( '|' )
385
+ return 'unknown'
349
386
}
350
387
351
388
return 'unknown'
0 commit comments