Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support gen types from command line #789

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 41 additions & 63 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
import { apply as applyTypescriptTemplate } from './templates/typescript.js'
import { apply as applyGoTemplate } from './templates/go.js'
import { apply as applySwiftTemplate } from './templates/swift.js'
import { getGeneratorMetadata } from '../lib/generators.js'

const logger = pino({
formatters: {
Expand All @@ -32,48 +31,6 @@ 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: generatorMetadata, error: generatorMetadataError } = await getGeneratorMetadata(
pgMeta,
{
includedSchemas: GENERATE_TYPES_INCLUDED_SCHEMAS,
}
)
if (generatorMetadataError) {
throw new Error(generatorMetadataError.message)
}

let output: string | null = null
switch (GENERATE_TYPES) {
case 'typescript':
output = await applyTypescriptTemplate({
...generatorMetadata,
detectOneToOneRelationships: GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
})
break
case 'swift':
output = await applySwiftTemplate({
...generatorMetadata,
accessControl: GENERATE_TYPES_SWIFT_ACCESS_CONTROL,
})
case 'go':
output = applyGoTemplate(generatorMetadata)
break
}

return output
}

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 === 'typescript') {
// TODO: Move to a separate script.
const pgMeta: PostgresMeta = new PostgresMeta({
...DEFAULT_POOL_CONFIG,
connectionString: PG_CONNECTION,
Expand Down Expand Up @@ -154,26 +111,47 @@ if (EXPORT_DOCS) {
throw new Error(typesError.message)
}

console.log(
await applyTypescriptTemplate({
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,
})
)
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) {
Expand Down