@@ -31,6 +31,14 @@ export const apply = ({
31
31
types : PostgresType [ ]
32
32
arrayTypes : PostgresType [ ]
33
33
} ) : string => {
34
+ const columnsByTableId = columns
35
+ . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
36
+ . reduce ( ( acc , curr ) => {
37
+ acc [ curr . table_id ] ??= [ ]
38
+ acc [ curr . table_id ] . push ( curr )
39
+ return acc
40
+ } , { } as Record < string , PostgresColumn [ ] > )
41
+
34
42
let output = `
35
43
export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]
36
44
@@ -44,9 +52,6 @@ export interface Database {
44
52
const schemaViews = [ ...views , ...materializedViews ]
45
53
. filter ( ( view ) => view . schema === schema . name )
46
54
. sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
47
- const schemaColumns = columns
48
- . filter ( ( column ) => column . schema === schema . name )
49
- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
50
55
const schemaFunctions = functions
51
56
. filter ( ( func ) => {
52
57
if ( func . schema !== schema . name ) {
@@ -80,15 +85,11 @@ export interface Database {
80
85
${
81
86
schemaTables . length === 0
82
87
? '[_ in never]: never'
83
- : schemaTables . map ( ( table ) => {
84
- const tableColumns = schemaColumns . filter (
85
- ( column ) => column . table_id === table . id
86
- )
87
-
88
- return `${ JSON . stringify ( table . name ) } : {
88
+ : schemaTables . map (
89
+ ( table ) => `${ JSON . stringify ( table . name ) } : {
89
90
Row: {
90
91
${ [
91
- ...tableColumns . map (
92
+ ...columnsByTableId [ table . id ] . map (
92
93
( column ) =>
93
94
`${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
94
95
column . format ,
@@ -109,7 +110,7 @@ export interface Database {
109
110
] }
110
111
}
111
112
Insert: {
112
- ${ tableColumns . map ( ( column ) => {
113
+ ${ columnsByTableId [ table . id ] . map ( ( column ) => {
113
114
let output = JSON . stringify ( column . name )
114
115
115
116
if ( column . identity_generation === 'ALWAYS' ) {
@@ -136,7 +137,7 @@ export interface Database {
136
137
} ) }
137
138
}
138
139
Update: {
139
- ${ tableColumns . map ( ( column ) => {
140
+ ${ columnsByTableId [ table . id ] . map ( ( column ) => {
140
141
let output = JSON . stringify ( column . name )
141
142
142
143
if ( column . identity_generation === 'ALWAYS' ) {
@@ -172,20 +173,17 @@ export interface Database {
172
173
) }
173
174
]
174
175
}`
175
- } )
176
+ )
176
177
}
177
178
}
178
179
Views: {
179
180
${
180
181
schemaViews . length === 0
181
182
? '[_ in never]: never'
182
- : schemaViews . map ( ( view ) => {
183
- const viewColumns = schemaColumns . filter (
184
- ( column ) => column . table_id === view . id
185
- )
186
- return `${ JSON . stringify ( view . name ) } : {
183
+ : schemaViews . map (
184
+ ( view ) => `${ JSON . stringify ( view . name ) } : {
187
185
Row: {
188
- ${ viewColumns . map (
186
+ ${ columnsByTableId [ view . id ] . map (
189
187
( column ) =>
190
188
`${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
191
189
column . format ,
@@ -197,7 +195,7 @@ export interface Database {
197
195
${
198
196
'is_updatable' in view && view . is_updatable
199
197
? `Insert: {
200
- ${ viewColumns . map ( ( column ) => {
198
+ ${ columnsByTableId [ view . id ] . map ( ( column ) => {
201
199
let output = JSON . stringify ( column . name )
202
200
203
201
if ( ! column . is_updatable ) {
@@ -210,7 +208,7 @@ export interface Database {
210
208
} ) }
211
209
}
212
210
Update: {
213
- ${ viewColumns . map ( ( column ) => {
211
+ ${ columnsByTableId [ view . id ] . map ( ( column ) => {
214
212
let output = JSON . stringify ( column . name )
215
213
216
214
if ( ! column . is_updatable ) {
@@ -243,7 +241,7 @@ export interface Database {
243
241
) }
244
242
]
245
243
}`
246
- } )
244
+ )
247
245
}
248
246
}
249
247
Functions: {
@@ -338,16 +336,14 @@ export interface Database {
338
336
)
339
337
if ( relation ) {
340
338
return `{
341
- ${ schemaColumns
342
- . filter ( ( column ) => column . table_id === relation . id )
343
- . map (
344
- ( column ) =>
345
- `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
346
- column . format ,
347
- types ,
348
- schemas
349
- ) } ${ column . is_nullable ? '| null' : '' } `
350
- ) }
339
+ ${ columnsByTableId [ relation . id ] . map (
340
+ ( column ) =>
341
+ `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
342
+ column . format ,
343
+ types ,
344
+ schemas
345
+ ) } ${ column . is_nullable ? '| null' : '' } `
346
+ ) }
351
347
}`
352
348
}
353
349
0 commit comments