11import prettier from 'prettier'
22import type {
3+ PostgresColumn ,
34 PostgresFunction ,
45 PostgresMaterializedView ,
56 PostgresRelationship ,
@@ -14,15 +15,17 @@ export const apply = ({
1415 tables,
1516 views,
1617 materializedViews,
18+ columns,
1719 relationships,
1820 functions,
1921 types,
2022 arrayTypes,
2123} : {
2224 schemas : PostgresSchema [ ]
23- tables : ( PostgresTable & { columns : unknown [ ] } ) [ ]
24- views : ( PostgresView & { columns : unknown [ ] } ) [ ]
25- materializedViews : ( PostgresMaterializedView & { columns : unknown [ ] } ) [ ]
25+ tables : Omit < PostgresTable , 'columns' > [ ]
26+ views : Omit < PostgresView , 'columns' > [ ]
27+ materializedViews : Omit < PostgresMaterializedView , 'columns' > [ ]
28+ columns : PostgresColumn [ ]
2629 relationships : PostgresRelationship [ ]
2730 functions : PostgresFunction [ ]
2831 types : PostgresType [ ]
@@ -41,6 +44,9 @@ export interface Database {
4144 const schemaViews = [ ...views , ...materializedViews ]
4245 . filter ( ( view ) => view . schema === schema . name )
4346 . 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 ) )
4450 const schemaFunctions = functions
4551 . filter ( ( func ) => {
4652 if ( func . schema !== schema . name ) {
@@ -78,8 +84,8 @@ export interface Database {
7884 ( table ) => `${ JSON . stringify ( table . name ) } : {
7985 Row: {
8086 ${ [
81- ...table . columns
82- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
87+ ...schemaColumns
88+ . filter ( ( column ) => column . table_id === table . id )
8389 . map (
8490 ( column ) =>
8591 `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
@@ -101,8 +107,8 @@ export interface Database {
101107 ] }
102108 }
103109 Insert: {
104- ${ table . columns
105- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
110+ ${ schemaColumns
111+ . filter ( ( column ) => column . table_id === table . id )
106112 . map ( ( column ) => {
107113 let output = JSON . stringify ( column . name )
108114
@@ -130,8 +136,8 @@ export interface Database {
130136 } ) }
131137 }
132138 Update: {
133- ${ table . columns
134- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
139+ ${ schemaColumns
140+ . filter ( ( column ) => column . table_id === table . id )
135141 . map ( ( column ) => {
136142 let output = JSON . stringify ( column . name )
137143
@@ -178,8 +184,8 @@ export interface Database {
178184 : schemaViews . map (
179185 ( view ) => `${ JSON . stringify ( view . name ) } : {
180186 Row: {
181- ${ view . columns
182- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
187+ ${ schemaColumns
188+ . filter ( ( column ) => column . table_id === view . id )
183189 . map (
184190 ( column ) =>
185191 `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
@@ -192,8 +198,8 @@ export interface Database {
192198 ${
193199 'is_updatable' in view && view . is_updatable
194200 ? `Insert: {
195- ${ view . columns
196- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
201+ ${ schemaColumns
202+ . filter ( ( column ) => column . table_id === view . id )
197203 . map ( ( column ) => {
198204 let output = JSON . stringify ( column . name )
199205
@@ -211,8 +217,8 @@ export interface Database {
211217 } ) }
212218 }
213219 Update: {
214- ${ view . columns
215- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
220+ ${ schemaColumns
221+ . filter ( ( column ) => column . table_id === view . id )
216222 . map ( ( column ) => {
217223 let output = JSON . stringify ( column . name )
218224
@@ -345,8 +351,8 @@ export interface Database {
345351 )
346352 if ( relation ) {
347353 return `{
348- ${ relation . columns
349- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
354+ ${ schemaColumns
355+ . filter ( ( column ) => column . table_id === relation . id )
350356 . map (
351357 ( column ) =>
352358 `${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
0 commit comments