Skip to content

Commit bc156de

Browse files
authored
feat: support gen types from command line (#789)
1 parent ddc389d commit bc156de

File tree

1 file changed

+41
-63
lines changed

1 file changed

+41
-63
lines changed

src/server/server.ts

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
import { apply as applyTypescriptTemplate } from './templates/typescript.js'
1818
import { apply as applyGoTemplate } from './templates/go.js'
1919
import { apply as applySwiftTemplate } from './templates/swift.js'
20-
import { getGeneratorMetadata } from '../lib/generators.js'
2120

2221
const logger = pino({
2322
formatters: {
@@ -32,48 +31,6 @@ const app = buildApp({ logger })
3231
const adminApp = buildAdminApp({ logger })
3332

3433
async function getTypeOutput(): Promise<string | null> {
35-
const pgMeta: PostgresMeta = new PostgresMeta({
36-
...DEFAULT_POOL_CONFIG,
37-
connectionString: PG_CONNECTION,
38-
})
39-
const { data: generatorMetadata, error: generatorMetadataError } = await getGeneratorMetadata(
40-
pgMeta,
41-
{
42-
includedSchemas: GENERATE_TYPES_INCLUDED_SCHEMAS,
43-
}
44-
)
45-
if (generatorMetadataError) {
46-
throw new Error(generatorMetadataError.message)
47-
}
48-
49-
let output: string | null = null
50-
switch (GENERATE_TYPES) {
51-
case 'typescript':
52-
output = await applyTypescriptTemplate({
53-
...generatorMetadata,
54-
detectOneToOneRelationships: GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
55-
})
56-
break
57-
case 'swift':
58-
output = await applySwiftTemplate({
59-
...generatorMetadata,
60-
accessControl: GENERATE_TYPES_SWIFT_ACCESS_CONTROL,
61-
})
62-
case 'go':
63-
output = applyGoTemplate(generatorMetadata)
64-
break
65-
}
66-
67-
return output
68-
}
69-
70-
if (EXPORT_DOCS) {
71-
// TODO: Move to a separate script.
72-
await app.ready()
73-
// @ts-ignore: app.swagger() is a Fastify decorator, so doesn't show up in the types
74-
console.log(JSON.stringify(app.swagger(), null, 2))
75-
} else if (GENERATE_TYPES === 'typescript') {
76-
// TODO: Move to a separate script.
7734
const pgMeta: PostgresMeta = new PostgresMeta({
7835
...DEFAULT_POOL_CONFIG,
7936
connectionString: PG_CONNECTION,
@@ -154,26 +111,47 @@ if (EXPORT_DOCS) {
154111
throw new Error(typesError.message)
155112
}
156113

157-
console.log(
158-
await applyTypescriptTemplate({
159-
schemas: schemas!.filter(
160-
({ name }) =>
161-
GENERATE_TYPES_INCLUDED_SCHEMAS.length === 0 ||
162-
GENERATE_TYPES_INCLUDED_SCHEMAS.includes(name)
163-
),
164-
tables: tables!,
165-
foreignTables: foreignTables!,
166-
views: views!,
167-
materializedViews: materializedViews!,
168-
columns: columns!,
169-
relationships: relationships!,
170-
functions: functions!.filter(
171-
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
172-
),
173-
types: types!,
174-
detectOneToOneRelationships: GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
175-
})
176-
)
114+
const config = {
115+
schemas: schemas!.filter(
116+
({ name }) =>
117+
GENERATE_TYPES_INCLUDED_SCHEMAS.length === 0 ||
118+
GENERATE_TYPES_INCLUDED_SCHEMAS.includes(name)
119+
),
120+
tables: tables!,
121+
foreignTables: foreignTables!,
122+
views: views!,
123+
materializedViews: materializedViews!,
124+
columns: columns!,
125+
relationships: relationships!,
126+
functions: functions!.filter(
127+
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
128+
),
129+
types: types!,
130+
detectOneToOneRelationships: GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
131+
}
132+
133+
switch (GENERATE_TYPES?.toLowerCase()) {
134+
case 'typescript':
135+
return await applyTypescriptTemplate(config)
136+
case 'swift':
137+
return await applySwiftTemplate({
138+
...config,
139+
accessControl: GENERATE_TYPES_SWIFT_ACCESS_CONTROL,
140+
})
141+
case 'go':
142+
return applyGoTemplate(config)
143+
default:
144+
throw new Error(`Unsupported language for GENERATE_TYPES: ${GENERATE_TYPES}`)
145+
}
146+
}
147+
148+
if (EXPORT_DOCS) {
149+
// TODO: Move to a separate script.
150+
await app.ready()
151+
// @ts-ignore: app.swagger() is a Fastify decorator, so doesn't show up in the types
152+
console.log(JSON.stringify(app.swagger(), null, 2))
153+
} else if (GENERATE_TYPES) {
154+
console.log(await getTypeOutput())
177155
} else {
178156
const closeListeners = closeWithGrace(async ({ err }) => {
179157
if (err) {

0 commit comments

Comments
 (0)