@@ -32,19 +32,12 @@ export const apply = async ({
3232 postgrestVersion ?: string
3333} ) : Promise < string > => {
3434 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 ) )
3840 )
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-
4841 const introspectionBySchema = Object . fromEntries < {
4942 tables : {
5043 table : Pick < PostgresTable , 'id' | 'name' | 'schema' | 'columns' >
@@ -64,26 +57,46 @@ export const apply = async ({
6457 ] )
6558 )
6659
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+
6787 function getRelationships (
6888 object : { schema : string ; name : string } ,
6989 relationships : GeneratorMetadata [ 'relationships' ]
7090 ) : Pick <
7191 GeneratorMetadata [ 'relationships' ] [ number ] ,
7292 'foreign_key_name' | 'columns' | 'is_one_to_one' | 'referenced_relation' | 'referenced_columns'
7393 > [ ] {
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+ )
87100 }
88101
89102 function generateRelationshiptTsDefinition ( relationship : TsRelationship ) : string {
@@ -148,16 +161,6 @@ export const apply = async ({
148161 }
149162 }
150163 }
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- }
161164 for ( const schema in introspectionBySchema ) {
162165 introspectionBySchema [ schema ] . tables . sort ( ( a , b ) => a . table . name . localeCompare ( b . table . name ) )
163166 introspectionBySchema [ schema ] . views . sort ( ( a , b ) => a . view . name . localeCompare ( b . view . name ) )
@@ -166,15 +169,6 @@ export const apply = async ({
166169 introspectionBySchema [ schema ] . compositeTypes . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
167170 }
168171
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-
178172 const getFunctionTsReturnType = ( fn : PostgresFunction , returnType : string ) => {
179173 return `${ returnType } ${ fn . is_set_returning_function ? '[]' : '' } `
180174 }
@@ -183,7 +177,7 @@ export const apply = async ({
183177 const tableArgs = fn . args . filter ( ( { mode } ) => mode === 'table' )
184178 if ( tableArgs . length > 0 ) {
185179 const argsNameAndType = tableArgs . map ( ( { name, type_id } ) => {
186- const type = typesById [ type_id ]
180+ const type = typesById . get ( type_id )
187181 let tsType = 'unknown'
188182 if ( type ) {
189183 tsType = pgTypeToTsType ( schema , type . name , {
@@ -224,7 +218,7 @@ export const apply = async ({
224218 }
225219
226220 // 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 )
228222 if ( type ) {
229223 return pgTypeToTsType ( schema , type . name , {
230224 types,
@@ -247,7 +241,7 @@ export const apply = async ({
247241 return 'Record<PropertyKey, never>'
248242 }
249243 const argsNameAndType = inArgs . map ( ( { name, type_id, has_default } ) => {
250- const type = typesById [ type_id ]
244+ const type = typesById . get ( type_id )
251245 let tsType = 'unknown'
252246 if ( type ) {
253247 tsType = pgTypeToTsType ( schema , type . name , {
@@ -492,7 +486,7 @@ export type Database = {
492486 ( { name, attributes } ) =>
493487 `${ JSON . stringify ( name ) } : {
494488 ${ attributes . map ( ( { name, type_id } ) => {
495- const type = typesById [ type_id ]
489+ const type = typesById . get ( type_id )
496490 let tsType = 'unknown'
497491 if ( type ) {
498492 tsType = `${ pgTypeToTsType ( schema , type . name , {
0 commit comments