@@ -32,13 +32,48 @@ export const apply = ({
32
32
let output = `
33
33
package database
34
34
35
- ${ tables . map ( ( table ) => generateTableStruct ( schemas . find ( ( schema ) => schema . name === table . schema ) ! , table , columnsByTableId [ table . id ] , types ) ) . join ( '\n\n' ) }
35
+ ${ tables
36
+ . map ( ( table ) =>
37
+ generateTableStruct (
38
+ schemas . find ( ( schema ) => schema . name === table . schema ) ! ,
39
+ table ,
40
+ columnsByTableId [ table . id ] ,
41
+ types
42
+ )
43
+ )
44
+ . join ( '\n\n' ) }
36
45
37
- ${ views . map ( ( view ) => generateTableStruct ( schemas . find ( ( schema ) => schema . name === view . schema ) ! , view , columnsByTableId [ view . id ] , types ) ) . join ( '\n\n' ) }
46
+ ${ views
47
+ . map ( ( view ) =>
48
+ generateTableStruct (
49
+ schemas . find ( ( schema ) => schema . name === view . schema ) ! ,
50
+ view ,
51
+ columnsByTableId [ view . id ] ,
52
+ types
53
+ )
54
+ )
55
+ . join ( '\n\n' ) }
38
56
39
- ${ materializedViews . map ( ( materializedView ) => generateTableStruct ( schemas . find ( ( schema ) => schema . name === materializedView . schema ) ! , materializedView , columnsByTableId [ materializedView . id ] , types ) ) . join ( '\n\n' ) }
57
+ ${ materializedViews
58
+ . map ( ( materializedView ) =>
59
+ generateTableStruct (
60
+ schemas . find ( ( schema ) => schema . name === materializedView . schema ) ! ,
61
+ materializedView ,
62
+ columnsByTableId [ materializedView . id ] ,
63
+ types
64
+ )
65
+ )
66
+ . join ( '\n\n' ) }
40
67
41
- ${ compositeTypes . map ( ( compositeType ) => generateCompositeTypeStruct ( schemas . find ( ( schema ) => schema . name === compositeType . schema ) ! , compositeType , types ) ) . join ( '\n\n' ) }
68
+ ${ compositeTypes
69
+ . map ( ( compositeType ) =>
70
+ generateCompositeTypeStruct (
71
+ schemas . find ( ( schema ) => schema . name === compositeType . schema ) ! ,
72
+ compositeType ,
73
+ types
74
+ )
75
+ )
76
+ . join ( '\n\n' ) }
42
77
` . trim ( )
43
78
44
79
return output
@@ -62,7 +97,12 @@ function formatForGoTypeName(name: string): string {
62
97
. join ( '' )
63
98
}
64
99
65
- function generateTableStruct ( schema : PostgresSchema , table : PostgresTable | PostgresView | PostgresMaterializedView , columns : PostgresColumn [ ] , types : PostgresType [ ] ) : string {
100
+ function generateTableStruct (
101
+ schema : PostgresSchema ,
102
+ table : PostgresTable | PostgresView | PostgresMaterializedView ,
103
+ columns : PostgresColumn [ ] ,
104
+ types : PostgresType [ ]
105
+ ) : string {
66
106
// Storing columns as a tuple of [formattedName, type, name] rather than creating the string
67
107
// representation of the line allows us to pre-format the entries. Go formats
68
108
// struct fields to be aligned, e.g.:
@@ -77,25 +117,33 @@ function generateTableStruct(schema: PostgresSchema, table: PostgresTable | Post
77
117
column . name ,
78
118
] )
79
119
80
- const [ maxFormattedNameLength , maxTypeLength ] = columnEntries . reduce ( ( [ maxFormattedName , maxType ] , [ formattedName , type ] ) => {
81
- return [ Math . max ( maxFormattedName , formattedName . length ) , Math . max ( maxType , type . length ) ]
82
- } , [ 0 , 0 ] )
120
+ const [ maxFormattedNameLength , maxTypeLength ] = columnEntries . reduce (
121
+ ( [ maxFormattedName , maxType ] , [ formattedName , type ] ) => {
122
+ return [ Math . max ( maxFormattedName , formattedName . length ) , Math . max ( maxType , type . length ) ]
123
+ } ,
124
+ [ 0 , 0 ]
125
+ )
83
126
84
127
// Pad the formatted name and type to align the struct fields, then join
85
128
// create the final string representation of the struct fields.
86
129
const formattedColumnEntries = columnEntries . map ( ( [ formattedName , type , name ] ) => {
87
- return ` ${ formattedName . padEnd ( maxFormattedNameLength ) } ${ type . padEnd ( maxTypeLength ) } \`json:"${ name } "\``
130
+ return ` ${ formattedName . padEnd ( maxFormattedNameLength ) } ${ type . padEnd (
131
+ maxTypeLength
132
+ ) } \`json:"${ name } "\``
88
133
} )
89
134
90
-
91
135
return `
92
136
type ${ formatForGoTypeName ( schema . name ) } ${ formatForGoTypeName ( table . name ) } struct {
93
137
${ formattedColumnEntries . join ( '\n' ) }
94
138
}
95
139
` . trim ( )
96
140
}
97
141
98
- function generateCompositeTypeStruct ( schema : PostgresSchema , type : PostgresType , types : PostgresType [ ] ) : string {
142
+ function generateCompositeTypeStruct (
143
+ schema : PostgresSchema ,
144
+ type : PostgresType ,
145
+ types : PostgresType [ ]
146
+ ) : string {
99
147
// Use the type_id of the attributes to find the types of the attributes
100
148
const typeWithRetrievedAttributes = {
101
149
...type ,
@@ -105,22 +153,29 @@ function generateCompositeTypeStruct(schema: PostgresSchema, type: PostgresType,
105
153
...attribute ,
106
154
type,
107
155
}
108
- } )
156
+ } ) ,
109
157
}
110
- const attributeEntries : [ string , string , string ] [ ] = typeWithRetrievedAttributes . attributes . map ( ( attribute ) => [
111
- formatForGoTypeName ( attribute . name ) ,
112
- pgTypeToGoType ( attribute . type ! . format ) ,
113
- attribute . name ,
114
- ] )
158
+ const attributeEntries : [ string , string , string ] [ ] = typeWithRetrievedAttributes . attributes . map (
159
+ ( attribute ) => [
160
+ formatForGoTypeName ( attribute . name ) ,
161
+ pgTypeToGoType ( attribute . type ! . format ) ,
162
+ attribute . name ,
163
+ ]
164
+ )
115
165
116
- const [ maxFormattedNameLength , maxTypeLength ] = attributeEntries . reduce ( ( [ maxFormattedName , maxType ] , [ formattedName , type ] ) => {
117
- return [ Math . max ( maxFormattedName , formattedName . length ) , Math . max ( maxType , type . length ) ]
118
- } , [ 0 , 0 ] )
166
+ const [ maxFormattedNameLength , maxTypeLength ] = attributeEntries . reduce (
167
+ ( [ maxFormattedName , maxType ] , [ formattedName , type ] ) => {
168
+ return [ Math . max ( maxFormattedName , formattedName . length ) , Math . max ( maxType , type . length ) ]
169
+ } ,
170
+ [ 0 , 0 ]
171
+ )
119
172
120
173
// Pad the formatted name and type to align the struct fields, then join
121
174
// create the final string representation of the struct fields.
122
175
const formattedAttributeEntries = attributeEntries . map ( ( [ formattedName , type , name ] ) => {
123
- return ` ${ formattedName . padEnd ( maxFormattedNameLength ) } ${ type . padEnd ( maxTypeLength ) } \`json:"${ name } "\``
176
+ return ` ${ formattedName . padEnd ( maxFormattedNameLength ) } ${ type . padEnd (
177
+ maxTypeLength
178
+ ) } \`json:"${ name } "\``
124
179
} )
125
180
126
181
return `
0 commit comments