8000 fix: always sort the table & view columns · joeally/postgres-meta@62a7cb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 62a7cb8

Browse files
kbsalisweatybridge
authored andcommitted
fix: always sort the table & view columns
1 parent e43a3f4 commit 62a7cb8

File tree

1 file changed

+75
-63
lines changed

1 file changed

+75
-63
lines changed

src/server/templates/typescript.ts

Lines changed: 75 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,16 @@ export interface Database {
6969
(table) => `${JSON.stringify(table.name)}: {
7070
Row: {
7171
${[
72-
...table.columns.map(
73-
(column) =>
74-
`${JSON.stringify(column.name)}: ${pgTypeToTsType(
75-
column.format,
76-
types,
77-
schemas
78-
)} ${column.is_nullable ? '| null' : ''}`
79-
),
72+
...table.columns
73+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
74+
.map(
75+
(column) =>
76+
`${JSON.stringify(column.name)}: ${pgTypeToTsType(
77+
column.format,
78+
types,
79+
schemas
80+
)} ${column.is_nullable ? '| null' : ''}`
81+
),
8082
...schemaFunctions
8183
.filter((fn) => fn.argument_types === table.name)
8284
.map(
@@ -90,48 +92,52 @@ export interface Database {
9092
]}
9193
}
9294
Insert: {
93-
${table.columns.map((column) => {
94-
let output = JSON.stringify(column.name)
95+
${table.columns
96+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
97+
.map((column) => {
98+
let output = JSON.stringify(column.name)
9599
96-
if (column.identity_generation === 'ALWAYS') {
97-
return `${output}?: never`
98-
}
100+
if (column.identity_generation === 'ALWAYS') {
101+
return `${output}?: never`
102+
}
99103
100-
if (
101-
column.is_nullable ||
102-
column.is_identity ||
103-
column.default_value !== null
104-
) {
105-
output += '?:'
106-
} else {
107-
output += ':'
108-
}
104+
if (
105+
column.is_nullable ||
106+
column.is_identity ||
107+
column.default_value !== null
108+
) {
109+
output += '?:'
110+
} else {
111+
output += ':'
112+
}
109113
110-
output += pgTypeToTsType(column.format, types, schemas)
114+
output += pgTypeToTsType(column.format, types, schemas)
111115
112-
if (column.is_nullable) {
113-
output += '| null'
114-
}
116+
if (column.is_nullable) {
117+
output += '| null'
118+
}
115119
116-
return output
117-
})}
120+
return output
121+
})}
118122
}
119123
Update: {
120-
${table.columns.map((column) => {
121-
let output = JSON.stringify(column.name)
124+
${table.columns
125+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
126+
.map((column) => {
127+
let output = JSON.stringify(column.name)
122128
123-
if (column.identity_generation === 'ALWAYS') {
124-
return `${output}?: never`
125-
}
129+
if (column.identity_generation === 'ALWAYS') {
130+
return `${output}?: never`
131+
}
126132
127-
output += `?: ${pgTypeToTsType(column.format, types, schemas)}`
133+
output += `?: ${pgTypeToTsType(column.format, types, schemas)}`
128134
129-
if (column.is_nullable) {
130-
output += '| null'
131-
}
135+
if (column.is_nullable) {
136+
output += '| null'
137+
}
132138
133-
return output
134-
})}
139+
return output
140+
})}
135141
}
136142
}`
137143
)
@@ -144,46 +150,52 @@ export interface Database {
144150
: schemaViews.map(
145151
(view) => `${JSON.stringify(view.name)}: {
146152
Row: {
147-
${view.columns.map(
148-
(column) =>
149-
`${JSON.stringify(column.name)}: ${pgTypeToTsType(
150-
column.format,
151-
types,
152-
schemas
153-
)} ${column.is_nullable ? '| null' : ''}`
154-
)}
153+
${view.columns
154+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
155+
.map(
156+
(column) =>
157+
`${JSON.stringify(column.name)}: ${pgTypeToTsType(
158+
column.format,
159+
types,
160+
schemas
161+
)} ${column.is_nullable ? '| null' : ''}`
162+
)}
155163
}
156164
${
157165
view.is_updatable
158166
? `Insert: {
159-
${view.columns.map((column) => {
160-
let output = JSON.stringify(column.name)
167+
${view.columns
168+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
169+
.map((column) => {
170+
let output = JSON.stringify(column.name)
161171
162-
if (!column.is_updatable) {
163-
return `${output}?: never`
164-
}
172+
if (!column.is_updatable) {
173+
return `${output}?: never`
174+
}
165175
166-
output += `?: ${pgTypeToTsType(column.format, types, schemas)} | null`
176+
output += `?: ${pgTypeToTsType(column.format, types, schemas)} | null`
167177
168-
return output
169-
})}
178+
return output
179+
})}
170180
}`
171181
: ''
172182
}
173183
${
174184
view.is_updatable
175185
? `Update: {
176-
${view.columns.map((column) => {
177-
let output = JSON.stringify(column.name)
186+
${view.columns
187+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
188+
.map((column) => {
189+
let output = JSON.stringify(column.name)
178190
179-
if (!column.is_updatable) {
180-
return `${output}?: never`
181-
}
191+
if (!column.is_updatable) {
192+
return `${output}?: never`
193+
}
182194
183-
output += `?: ${pgTypeToTsType(column.format, types, schemas)} | null`
195+
output += `?: ${pgTypeToTsType(column.format, types, schemas)} | null`
184196
185-
return output
186-
})}
197+
return output
198+
})}
187199
}`
188200
: ''
189201
}

0 commit comments

Comments
 (0)
0