Skip to content

Commit d60c97f

Browse files
committed
fix(typegen): order types by name
1 parent e13cf20 commit d60c97f

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/server/templates/typescript.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,41 @@ export const apply = ({
2424
export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]
2525
2626
export interface Database {
27-
${schemas.map((schema) => {
28-
const schemaTables = tables.filter((table) => table.schema === schema.name)
29-
const schemaViews = views.filter((view) => view.schema === schema.name)
30-
const schemaFunctions = functions.filter((func) => {
31-
if (func.schema !== schema.name) {
32-
return false
33-
}
27+
${schemas
28+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
29+
.map((schema) => {
30+
const schemaTables = tables
31+
.filter((table) => table.schema === schema.name)
32+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
33+
const schemaViews = views
34+
.filter((view) => view.schema === schema.name)
35+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
36+
const schemaFunctions = functions
37+
.filter((func) => {
38+
if (func.schema !== schema.name) {
39+
return false
40+
}
3441
35-
// Either:
36-
// 1. All input args are be named, or
37-
// 2. There is only one input arg which is unnamed
38-
const inArgs = func.args.filter(({ mode }) => ['in', 'inout', 'variadic'].includes(mode))
42+
// Either:
43+
// 1. All input args are be named, or
44+
// 2. There is only one input arg which is unnamed
45+
const inArgs = func.args.filter(({ mode }) => ['in', 'inout', 'variadic'].includes(mode))
3946
40-
if (!inArgs.some(({ name }) => name === '')) {
41-
return true
42-
}
47+
if (!inArgs.some(({ name }) => name === '')) {
48+
return true
49+
}
4350
44-
if (inArgs.length === 1) {
45-
return true
46-
}
51+
if (inArgs.length === 1) {
52+
return true
53+
}
4754
48-
return false
49-
})
50-
const schemaEnums = types.filter((type) => type.schema === schema.name && type.enums.length > 0)
51-
return `${JSON.stringify(schema.name)}: {
55+
return false
56+
})
57+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
58+
const schemaEnums = types
59+
.filter((type) => type.schema === schema.name && type.enums.length > 0)
60+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
61+
return `${JSON.stringify(schema.name)}: {
5262
Tables: {
5363
${
5464
schemaTables.length === 0
@@ -235,7 +245,7 @@ export interface Database {
235245
}
236246
}
237247
}`
238-
})}
248+
})}
239249
}`
240250

241251
output = prettier.format(output, {

0 commit comments

Comments
 (0)