@@ -31,6 +31,14 @@ export const apply = ({
3131 types : PostgresType [ ]
3232 arrayTypes : PostgresType [ ]
3333} ) : 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+
3442 let output = `
3543export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]
3644
@@ -44,9 +52,6 @@ export interface Database {
4452 const schemaViews = [ ...views , ...materializedViews ]
4553 . filter ( ( view ) => view . schema === schema . name )
4654 . 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 ) )
5055 const schemaFunctions = functions
5156 . filter ( ( func ) => {
5257 if ( func . schema !== schema . name ) {
@@ -80,15 +85,11 @@ export interface Database {
8085 ${
8186 schemaTables . length === 0
8287 ? '[_ 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 ) } : {
8990 Row: {
9091 ${ [
91- ...tableColumns . map (
92+ ...columnsByTableId [ table . id ] . map (
9293 ( column ) =>
9394 `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
9495 column . format ,
@@ -109,7 +110,7 @@ export interface Database {
109110 ] }
110111 }
111112 Insert: {
112- ${ tableColumns . map ( ( column ) => {
113+ ${ columnsByTableId [ table . id ] . map ( ( column ) => {
113114 let output = JSON . stringify ( column . name )
114115
115116 if ( column . identity_generation === 'ALWAYS' ) {
@@ -136,7 +137,7 @@ export interface Database {
136137 } ) }
137138 }
138139 Update: {
139- ${ tableColumns . map ( ( column ) => {
140+ ${ columnsByTableId [ table . id ] . map ( ( column ) => {
140141 let output = JSON . stringify ( column . name )
141142
142143 if ( column . identity_generation === 'ALWAYS' ) {
@@ -172,20 +173,17 @@ export interface Database {
172173 ) }
173174 ]
174175 }`
175- } )
176+ )
176177 }
177178 }
178179 Views: {
179180 ${
180181 schemaViews . length === 0
181182 ? '[_ 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 ) } : {
187185 Row: {
188- ${ viewColumns . map (
186+ ${ columnsByTableId [ view . id ] . map (
189187 ( column ) =>
190188 `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
191189 column . format ,
@@ -197,7 +195,7 @@ export interface Database {
197195 ${
198196 'is_updatable' in view && view . is_updatable
199197 ? `Insert: {
200- ${ viewColumns . map ( ( column ) => {
198+ ${ columnsByTableId [ view . id ] . map ( ( column ) => {
201199 let output = JSON . stringify ( column . name )
202200
203201 if ( ! column . is_updatable ) {
@@ -210,7 +208,7 @@ export interface Database {
210208 } ) }
211209 }
212210 Update: {
213- ${ viewColumns . map ( ( column ) => {
211+ ${ columnsByTableId [ view . id ] . map ( ( column ) => {
214212 let output = JSON . stringify ( column . name )
215213
216214 if ( ! column . is_updatable ) {
@@ -243,7 +241,7 @@ export interface Database {
243241 ) }
244242 ]
245243 }`
246- } )
244+ )
247245 }
248246 }
249247 Functions: {
@@ -338,16 +336,14 @@ export interface Database {
338336 )
339337 if ( relation ) {
340338 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+ ) }
351347 }`
352348 }
353349
0 commit comments