1
1
import prettier from 'prettier'
2
2
import type {
3
+ PostgresColumn ,
3
4
PostgresFunction ,
4
5
PostgresMaterializedView ,
5
6
PostgresRelationship ,
@@ -14,15 +15,17 @@ export const apply = ({
14
15
tables,
15
16
views,
16
17
materializedViews,
18
+ columns,
17
19
relationships,
18
20
functions,
19
21
types,
20
22
arrayTypes,
21
23
} : {
22
24
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 [ ]
26
29
relationships : PostgresRelationship [ ]
27
30
functions : PostgresFunction [ ]
28
31
types : PostgresType [ ]
@@ -41,6 +44,9 @@ export interface Database {
41
44
const schemaViews = [ ...views , ...materializedViews ]
42
45
. filter ( ( view ) => view . schema === schema . name )
43
46
. 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 ) )
44
50
const schemaFunctions = functions
45
51
. filter ( ( func ) => {
46
52
if ( func . schema !== schema . name ) {
@@ -78,8 +84,8 @@ export interface Database {
78
84
( table ) => `${ JSON . stringify ( table . name ) } : {
79
85
Row: {
80
86
${ [
81
- ...table . columns
82
- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
87
+ ...schemaColumns
88
+ . filter ( ( column ) => column . table_id === table . id )
83
89
. map (
84
90
( column ) =>
85
91
`${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
@@ -101,8 +107,8 @@ export interface Database {
101
107
] }
102
108
}
103
109
Insert: {
104
- ${ table . columns
105
- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
110
+ ${ schemaColumns
111
+ . filter ( ( column ) => column . table_id === table . id )
106
112
. map ( ( column ) => {
107
113
let output = JSON . stringify ( column . name )
108
114
@@ -130,8 +136,8 @@ export interface Database {
130
136
} ) }
131
137
}
132
138
Update: {
133
- ${ table . columns
134
- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
139
+ ${ schemaColumns
140
+ . filter ( ( column ) => column . table_id === table . id )
135
141
. map ( ( column ) => {
136
142
let output = JSON . stringify ( column . name )
137
143
@@ -178,8 +184,8 @@ export interface Database {
178
184
: schemaViews . map (
179
185
( view ) => `${ JSON . stringify ( view . name ) } : {
180
186
Row: {
181
- ${ view . columns
182
- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
187
+ ${ schemaColumns
188
+ . filter ( ( column ) => column . table_id === view . id )
183
189
. map (
184
190
( column ) =>
185
191
`${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
@@ -192,8 +198,8 @@ export interface Database {
192
198
${
193
199
'is_updatable' in view && view . is_updatable
194
200
? `Insert: {
195
- ${ view . columns
196
- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
201
+ ${ schemaColumns
202
+ . filter ( ( column ) => column . table_id === view . id )
197
203
. map ( ( column ) => {
198
204
let output = JSON . stringify ( column . name )
199
205
@@ -211,8 +217,8 @@ export interface Database {
211
217
} ) }
212
218
}
213
219
Update: {
214
- ${ view . columns
215
- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
220
+ ${ schemaColumns
221
+ . filter ( ( column ) => column . table_id === view . id )
216
222
. map ( ( column ) => {
217
223
let output = JSON . stringify ( column . name )
218
224
@@ -345,8 +351,8 @@ export interface Database {
345
351
)
346
352
if ( relation ) {
347
353
return `{
348
- ${ relation . columns
349
- . sort ( ( { name : a } , { name : b } ) => a . localeCompare ( b ) )
354
+ ${ schemaColumns
355
+ . filter ( ( column ) => column . table_id === relation . id )
350
356
. map (
351
357
( column ) =>
352
358
`${ JSON . stringify ( column . name ) } : ${ pgTypeToTsType (
0 commit comments