@@ -36,33 +36,6 @@ export const apply = async ({
36
36
postgrestVersion ?: string
37
37
} ) : Promise < string > => {
38
38
schemas . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
39
-
40
- const columnsByTableId : Record < number , PostgresColumn [ ] > = { }
41
- const tablesNamesByTableId : Record < number , string > = { }
42
- const relationTypeByIds = new Map < number , PostgresType > ( )
43
- // group types by id for quicker lookup
44
- const typesById = types . reduce (
45
- ( acc , type ) => {
46
- acc [ type . id ] = type
47
- return acc
48
- } ,
49
- { } as Record < number , ( typeof types ) [ number ] >
50
- )
51
- const tablesLike = [ ...tables , ...foreignTables , ...views , ...materializedViews ]
52
-
53
- for ( const tableLike of tablesLike ) {
54
- columnsByTableId [ tableLike . id ] = [ ]
55
- tablesNamesByTableId [ tableLike . id ] = tableLike . name
56
- }
57
- for ( const column of columns ) {
58
- if ( column . table_id in columnsByTableId ) {
59
- columnsByTableId [ column . table_id ] . push ( column )
60
- }
61
- }
62
- for ( const tableId in columnsByTableId ) {
63
- columnsByTableId [ tableId ] . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
64
- }
65
-
66
39
const introspectionBySchema = Object . fromEntries < {
67
40
tables : {
68
41
table : Pick < PostgresTable , 'id' | 'name' | 'schema' | 'columns' >
@@ -81,6 +54,41 @@ export const apply = async ({
81
54
{ tables : [ ] , views : [ ] , functions : [ ] , enums : [ ] , compositeTypes : [ ] } ,
82
55
] )
83
56
)
57
+ const columnsByTableId : Record < number , PostgresColumn [ ] > = { }
58
+ const tablesNamesByTableId : Record < number , string > = { }
59
+ const relationTypeByIds = new Map < number , ( typeof types ) [ number ] > ( )
60
+ // group types by id for quicker lookup
61
+ const typesById = new Map < number , ( typeof types ) [ number ] > ( )
62
+ const tablesLike = [ ...tables , ...foreignTables , ...views , ...materializedViews ]
63
+
64
+ for ( const tableLike of tablesLike ) {
65
+ columnsByTableId [ tableLike . id ] = [ ]
66
+ tablesNamesByTableId [ tableLike . id ] = tableLike . name
67
+ }
68
+ for ( const column of columns ) {
69
+ if ( column . table_id in columnsByTableId ) {
70
+ columnsByTableId [ column . table_id ] . push ( column )
71
+ }
72
+ }
73
+ for ( const tableId in columnsByTableId ) {
74
+ columnsByTableId [ tableId ] . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
75
+ }
76
+
77
+ for ( const type of types ) {
78
+ typesById . set ( type . id , type )
79
+ // Save all the types that are relation types for quicker lookup
80
+ if ( type . type_relation_id ) {
81
+ relationTypeByIds . set ( type . id , type )
82
+ }
83
+ if ( type . schema in introspectionBySchema ) {
84
+ if ( type . enums . length > 0 ) {
85
+ introspectionBySchema [ type . schema ] . enums . push ( type )
86
+ }
87
+ if ( type . attributes . length > 0 ) {
88
+ introspectionBySchema [ type . schema ] . compositeTypes . push ( type )
89
+ }
90
+ }
91
+ }
84
92
85
93
function getRelationships (
86
94
object : { schema : string ; name : string } ,
@@ -148,20 +156,6 @@ export const apply = async ({
148
156
} )
149
157
}
150
158
}
151
- for ( const type of types ) {
152
- // Save all the types that are relation types for quicker lookup
153
- if ( type . type_relation_id ) {
154
- relationTypeByIds . set ( type . id , type )
155
- }
156
- if ( type . schema in introspectionBySchema ) {
157
- if ( type . enums . length > 0 ) {
158
- introspectionBySchema [ type . schema ] . enums . push ( type )
159
- }
160
- if ( type . attributes . length > 0 ) {
161
- introspectionBySchema [ type . schema ] . compositeTypes . push ( type )
162
- }
163
- }
164
- }
165
159
// Helper function to get table/view name from relation id
166
160
const getTableNameFromRelationId = (
167
161
relationId : number | null ,
@@ -235,15 +229,15 @@ export const apply = async ({
235
229
// Case 1: Standard embedded function with proper setof detection
236
230
if ( returnsSetOfTable && returnTableName ) {
237
231
setofOptionsInfo = `SetofOptions: {
238
- from: ${ JSON . stringify ( typesById [ fn . args [ 0 ] . type_id ] . format ) }
232
+ from: ${ JSON . stringify ( typesById . get ( fn . args [ 0 ] . type_id ) ? .format ) }
239
233
to: ${ JSON . stringify ( returnTableName ) }
240
234
isOneToOne: ${ Boolean ( ! returnsMultipleRows ) }
241
235
isSetofReturn: true
242
236
}`
243
237
}
244
238
// Case 2: Handle RETURNS table-name those are always a one to one relationship
245
239
else if ( returnTableName && ! returnsSetOfTable ) {
246
- const sourceTable = typesById [ fn . args [ 0 ] . type_id ] . format
240
+ const sourceTable = typesById . get ( fn . args [ 0 ] . type_id ) ? .format
247
241
const targetTable = returnTableName
248
242
setofOptionsInfo = `SetofOptions: {
249
243
from: ${ JSON . stringify ( sourceTable ) }
@@ -273,7 +267,7 @@ export const apply = async ({
273
267
const tableArgs = fn . args . filter ( ( { mode } ) => mode === 'table' )
274
268
if ( tableArgs . length > 0 ) {
275
269
const argsNameAndType = tableArgs . map ( ( { name, type_id } ) => {
276
- const type = typesById [ type_id ]
270
+ const type = typesById . get ( type_id )
277
271
let tsType = 'unknown'
278
272
if ( type ) {
279
273
tsType = pgTypeToTsType ( schema , type . name , {
@@ -324,7 +318,7 @@ export const apply = async ({
324
318
}
325
319
326
320
// Case 3: returns base/array/composite/enum type.
327
- const type = typesById [ fn . return_type_id ]
321
+ const type = typesById . get ( fn . return_type_id )
328
322
if ( type ) {
329
323
return pgTypeToTsType ( schema , type . name , {
330
324
types,
@@ -369,7 +363,7 @@ export const apply = async ({
369
363
370
364
if ( conflictingFns . length > 0 ) {
371
365
const conflictingFn = conflictingFns [ 0 ]
372
- const returnTypeName = typesById [ conflictingFn . fn . return_type_id ] ?. name || 'unknown'
366
+ const returnTypeName = typesById . get ( conflictingFn . fn . return_type_id ) ?. name || 'unknown'
373
367
return `Could not choose the best candidate function between: ${ schema . name } .${ fn . name } (), ${ schema . name } .${ fn . name } ( => ${ returnTypeName } ). Try renaming the parameters or the function itself in the database so function overloading can be resolved`
374
368
}
375
369
}
@@ -395,7 +389,7 @@ export const apply = async ({
395
389
} )
396
390
. map ( ( f ) => {
397
391
const args = f . inArgs
398
- return `${ schema . name } .${ fn . name } (${ args . map ( ( a ) => `${ a . name || '' } => ${ typesById [ a . type_id ] ?. name || 'unknown' } ` ) . join ( ', ' ) } )`
392
+ return `${ schema . name } .${ fn . name } (${ args . map ( ( a ) => `${ a . name || '' } => ${ typesById . get ( a . type_id ) ?. name || 'unknown' } ` ) . join ( ', ' ) } )`
399
393
} )
400
394
. join ( ', ' )
401
395
@@ -420,7 +414,7 @@ export const apply = async ({
420
414
if ( conflictError ) {
421
415
if ( inArgs . length > 0 ) {
422
416
const argsNameAndType = inArgs . map ( ( { name, type_id, has_default } ) => {
423
- const type = typesById [ type_id ]
417
+ const type = typesById . get ( type_id )
424
418
let tsType = 'unknown'
425
419
if ( type ) {
426
420
tsType = pgTypeToTsType ( schema , type . name , {
@@ -439,7 +433,7 @@ export const apply = async ({
439
433
// Special case for computed fields returning scalars functions
440
434
if ( inArgs . length > 0 ) {
441
435
const argsNameAndType = inArgs . map ( ( { name, type_id, has_default } ) => {
442
- const type = typesById [ type_id ]
436
+ const type = typesById . get ( type_id )
443
437
let tsType = 'unknown'
444
438
if ( type ) {
445
439
tsType = pgTypeToTsType ( schema , type . name , {
@@ -456,7 +450,7 @@ export const apply = async ({
456
450
returnType = `{ error: true } & ${ JSON . stringify ( `the function ${ schema . name } .${ fn . name } with parameter or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache` ) } `
457
451
} else if ( inArgs . length > 0 ) {
458
452
const argsNameAndType = inArgs . map ( ( { name, type_id, has_default } ) => {
459
- const type = typesById [ type_id ]
453
+ const type = typesById . get ( type_id )
460
454
let tsType = 'unknown'
461
455
if ( type ) {
462
456
tsType = pgTypeToTsType ( schema , type . name , {
@@ -708,7 +702,7 @@ export type Database = {
708
702
( { name, attributes } ) =>
709
703
`${ JSON . stringify ( name ) } : {
710
704
${ attributes . map ( ( { name, type_id } ) => {
711
- const type = typesById [ type_id ]
705
+ const type = typesById . get ( type_id )
712
706
let tsType = 'unknown'
713
707
if ( type ) {
714
708
tsType = `${ pgTypeToTsType ( schema , type . name , {
0 commit comments