forked from supabase/postgres-meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
181 lines (173 loc) · 5.27 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import closeWithGrace from 'close-with-grace'
import { pino } from 'pino'
import { PostgresMeta } from '../lib/index.js'
import { build as buildApp } from './app.js'
import { build as buildAdminApp } from './admin-app.js'
import {
DEFAULT_POOL_CONFIG,
EXPORT_DOCS,
GENERATE_TYPES,
GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
GENERATE_TYPES_INCLUDED_SCHEMAS,
GENERATE_TYPES_SWIFT_ACCESS_CONTROL,
PG_CONNECTION,
PG_META_HOST,
PG_META_PORT,
} from './constants.js'
import { apply as applyTypescriptTemplate } from './templates/typescript.js'
import { apply as applyGoTemplate } from './templates/go.js'
import { apply as applySwiftTemplate } from './templates/swift.js'
const logger = pino({
formatters: {
level(label) {
return { level: label }
},
},
timestamp: pino.stdTimeFunctions.isoTime,
})
const app = buildApp({ logger })
const adminApp = buildAdminApp({ logger })
async function getTypeOutput(): Promise<string | null> {
const pgMeta: PostgresMeta = new PostgresMeta({
...DEFAULT_POOL_CONFIG,
connectionString: PG_CONNECTION,
})
const [
{ data: schemas, error: schemasError },
{ data: tables, error: tablesError },
{ data: foreignTables, error: foreignTablesError },
{ data: views, error: viewsError },
{ data: materializedViews, error: materializedViewsError },
{ data: columns, error: columnsError },
{ data: relationships, error: relationshipsError },
{ data: functions, error: functionsError },
{ data: types, error: typesError },
] = await Promise.all([
pgMeta.schemas.list(),
pgMeta.tables.list({
includedSchemas:
GENERATE_TYPES_INCLUDED_SCHEMAS.length > 0 ? GENERATE_TYPES_INCLUDED_SCHEMAS : undefined,
includeColumns: false,
}),
pgMeta.foreignTables.list({
includedSchemas:
GENERATE_TYPES_INCLUDED_SCHEMAS.length > 0 ? GENERATE_TYPES_INCLUDED_SCHEMAS : undefined,
includeColumns: false,
}),
pgMeta.views.list({
includedSchemas:
GENERATE_TYPES_INCLUDED_SCHEMAS.length > 0 ? GENERATE_TYPES_INCLUDED_SCHEMAS : undefined,
includeColumns: false,
}),
pgMeta.materializedViews.list({
includedSchemas:
GENERATE_TYPES_INCLUDED_SCHEMAS.length > 0 ? GENERATE_TYPES_INCLUDED_SCHEMAS : undefined,
includeColumns: false,
}),
pgMeta.columns.list({
includedSchemas:
GENERATE_TYPES_INCLUDED_SCHEMAS.length > 0 ? GENERATE_TYPES_INCLUDED_SCHEMAS : undefined,
}),
pgMeta.relationships.list(),
pgMeta.functions.list({
includedSchemas:
GENERATE_TYPES_INCLUDED_SCHEMAS.length > 0 ? GENERATE_TYPES_INCLUDED_SCHEMAS : undefined,
}),
pgMeta.types.list({
includeTableTypes: true,
includeArrayTypes: true,
includeSystemSchemas: true,
}),
])
await pgMeta.end()
if (schemasError) {
throw new Error(schemasError.message)
}
if (tablesError) {
throw new Error(tablesError.message)
}
if (foreignTablesError) {
throw new Error(foreignTablesError.message)
}
if (viewsError) {
throw new Error(viewsError.message)
}
if (materializedViewsError) {
throw new Error(materializedViewsError.message)
}
if (columnsError) {
throw new Error(columnsError.message)
}
if (relationshipsError) {
throw new Error(relationshipsError.message)
}
if (functionsError) {
throw new Error(functionsError.message)
}
if (typesError) {
throw new Error(typesError.message)
}
const config = {
schemas: schemas!.filter(
({ name }) =>
GENERATE_TYPES_INCLUDED_SCHEMAS.length === 0 ||
GENERATE_TYPES_INCLUDED_SCHEMAS.includes(name)
),
tables: tables!,
foreignTables: foreignTables!,
views: views!,
materializedViews: materializedViews!,
columns: columns!,
relationships: relationships!,
functions: functions!.filter(
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
),
types: types!,
detectOneToOneRelationships: GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
}
switch (GENERATE_TYPES?.toLowerCase()) {
case 'typescript':
return await applyTypescriptTemplate(config)
case 'swift':
return await applySwiftTemplate({
...config,
accessControl: GENERATE_TYPES_SWIFT_ACCESS_CONTROL,
})
case 'go':
return applyGoTemplate(config)
default:
throw new Error(`Unsupported language for GENERATE_TYPES: ${GENERATE_TYPES}`)
}
}
if (EXPORT_DOCS) {
// TODO: Move to a separate script.
await app.ready()
// @ts-ignore: app.swagger() is a Fastify decorator, so doesn't show up in the types
console.log(JSON.stringify(app.swagger(), null, 2))
} else if (GENERATE_TYPES) {
console.log(await getTypeOutput())
} else {
const closeListeners = closeWithGrace(async ({ err }) => {
if (err) {
app.log.error(err)
}
await app.close()
await adminApp.close()
})
app.addHook('onClose', async () => {
closeListeners.uninstall()
await adminApp.close()
})
adminApp.addHook('onClose', async () => {
closeListeners.uninstall()
await app.close()
})
app.listen({ port: PG_META_PORT, host: PG_META_HOST }, (err) => {
if (err) {
app.log.error(err)
process.exit(1)
}
const adminPort = PG_META_PORT + 1
adminApp.listen({ port: adminPort, host: PG_META_HOST })
})
}