@@ -32,19 +32,12 @@ export const apply = async ({
32
32
postgrestVersion ?: string
33
33
} ) : Promise < string > => {
34
34
schemas . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
35
-
36
- const columnsByTableId = Object . fromEntries < PostgresColumn [ ] > (
37
- [ ...tables , ...foreignTables , ...views , ...materializedViews ] . map ( ( t ) => [ t . id , [ ] ] )
35
+ relationships . sort (
36
+ ( a , b ) =>
37
+ a . foreign_key_name . localeCompare ( b . foreign_key_name ) ||
38
+ a . referenced_relation . localeCompare ( b . referenced_relation ) ||
39
+ JSON . stringify ( a . referenced_columns ) . localeCompare ( JSON . stringify ( b . referenced_columns ) )
38
40
)
39
- for ( const column of columns ) {
40
- if ( column . table_id in columnsByTableId ) {
41
- columnsByTableId [ column . table_id ] . push ( column )
42
- }
43
- }
44
- for ( const tableId in columnsByTableId ) {
45
- columnsByTableId [ tableId ] . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
46
- }
47
-
48
41
const introspectionBySchema = Object . fromEntries < {
49
42
tables : {
50
43
table : Pick < PostgresTable , 'id' | 'name' | 'schema' | 'columns' >
@@ -64,26 +57,46 @@ export const apply = async ({
64
57
] )
65
58
)
66
59
60
+ const columnsByTableId = Object . fromEntries < PostgresColumn [ ] > (
61
+ [ ...tables , ...foreignTables , ...views , ...materializedViews ] . map ( ( t ) => [ t . id , [ ] ] )
62
+ )
63
+ // group types by id for quicker lookup
64
+ const typesById = new Map < number , ( typeof types ) [ number ] > ( )
65
+
66
+ for ( const column of columns ) {
67
+ if ( column . table_id in columnsByTableId ) {
68
+ columnsByTableId [ column . table_id ] . push ( column )
69
+ }
70
+ }
71
+ for ( const tableId in columnsByTableId ) {
72
+ columnsByTableId [ tableId ] . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
73
+ }
74
+
75
+ for ( const type of types ) {
76
+ typesById . set ( type . id , type )
77
+ if ( type . schema in introspectionBySchema ) {
78
+ if ( type . enums . length > 0 ) {
79
+ introspectionBySchema [ type . schema ] . enums . push ( type )
80
+ }
81
+ if ( type . attributes . length > 0 ) {
82
+ introspectionBySchema [ type . schema ] . compositeTypes . push ( type )
83
+ }
84
+ }
85
+ }
86
+
67
87
function getRelationships (
68
88
object : { schema : string ; name : string } ,
69
89
relationships : GeneratorMetadata [ 'relationships' ]
70
90
) : Pick <
71
91
GeneratorMetadata [ 'relationships' ] [ number ] ,
72
92
'foreign_key_name' | 'columns' | 'is_one_to_one' | 'referenced_relation' | 'referenced_columns'
73
93
> [ ] {
74
- return relationships
75
- . filter (
76
- ( relationship ) =>
77
- relationship . schema === object . schema &&
78
- relationship . referenced_schema === object . schema &&
79
- relationship . relation === object . name
80
- )
81
- . toSorted (
82
- ( a , b ) =>
83
- a . foreign_key_name . localeCompare ( b . foreign_key_name ) ||
84
- a . referenced_relation . localeCompare ( b . referenced_relation ) ||
85
- JSON . stringify ( a . referenced_columns ) . localeCompare ( JSON . stringify ( b . referenced_columns ) )
86
- )
94
+ return relationships . filter (
95
+ ( relationship ) =>
96
+ relationship . schema === object . schema &&
97
+ relationship . referenced_schema === object . schema &&
98
+ relationship . relation === object . name
99
+ )
87
100
}
88
101
89
102
function generateRelationshiptTsDefinition ( relationship : TsRelationship ) : string {
@@ -148,16 +161,6 @@ export const apply = async ({
148
161
}
149
162
}
150
163
}
151
- for ( const type of types ) {
152
- if ( type . schema in introspectionBySchema ) {
153
- if ( type . enums . length > 0 ) {
154
- introspectionBySchema [ type . schema ] . enums . push ( type )
155
- }
156
- if ( type . attributes . length > 0 ) {
157
- introspectionBySchema [ type . schema ] . compositeTypes . push ( type )
158
- }
159
- }
160
- }
161
164
for ( const schema in introspectionBySchema ) {
162
165
introspectionBySchema [ schema ] . tables . sort ( ( a , b ) => a . table . name . localeCompare ( b . table . name ) )
163
166
introspectionBySchema [ schema ] . views . sort ( ( a , b ) => a . view . name . localeCompare ( b . view . name ) )
@@ -166,15 +169,6 @@ export const apply = async ({
166
169
introspectionBySchema [ schema ] . compositeTypes . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
167
170
}
168
171
169
- // group types by id for quicker lookup
170
- const typesById = types . reduce (
171
- ( acc , type ) => {
172
- acc [ type . id ] = type
173
- return acc
174
- } ,
175
- { } as Record < number , ( typeof types ) [ number ] >
176
- )
177
-
178
172
const getFunctionTsReturnType = ( fn : PostgresFunction , returnType : string ) => {
179
173
return `${ returnType } ${ fn . is_set_returning_function ? '[]' : '' } `
180
174
}
@@ -183,7 +177,7 @@ export const apply = async ({
183
177
const tableArgs = fn . args . filter ( ( { mode } ) => mode === 'table' )
184
178
if ( tableArgs . length > 0 ) {
185
179
const argsNameAndType = tableArgs . map ( ( { name, type_id } ) => {
186
- const type = typesById [ type_id ]
180
+ const type = typesById . get ( type_id )
187
181
let tsType = 'unknown'
188
182
if ( type ) {
189
183
tsType = pgTypeToTsType ( schema , type . name , {
@@ -224,7 +218,7 @@ export const apply = async ({
224
218
}
225
219
226
220
// Case 3: returns base/array/composite/enum type.
227
- const type = typesById [ fn . return_type_id ]
221
+ const type = typesById . get ( fn . return_type_id )
228
222
if ( type ) {
229
223
return pgTypeToTsType ( schema , type . name , {
230
224
types,
@@ -247,7 +241,7 @@ export const apply = async ({
247
241
return 'Record<PropertyKey, never>'
248
242
}
249
243
const argsNameAndType = inArgs . map ( ( { name, type_id, has_default } ) => {
250
- const type = typesById [ type_id ]
244
+ const type = typesById . get ( type_id )
251
245
let tsType = 'unknown'
252
246
if ( type ) {
253
247
tsType = pgTypeToTsType ( schema , type . name , {
@@ -492,7 +486,7 @@ export type Database = {
492
486
( { name, attributes } ) =>
493
487
`${ JSON . stringify ( name ) } : {
494
488
${ attributes . map ( ( { name, type_id } ) => {
495
- const type = typesById [ type_id ]
489
+ const type = typesById . get ( type_id )
496
490
let tsType = 'unknown'
497
491
if ( type ) {
498
492
tsType = `${ pgTypeToTsType ( schema , type . name , {
0 commit comments