Skip to content
Open
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions src/server/routes/generators/python.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { FastifyInstance } from 'fastify'
import { PostgresMeta } from '../../../lib/index.js'
import { DEFAULT_POOL_CONFIG } from '../../constants.js'
import { extractRequestForLogging } from '../../utils.js'
import { apply as applyPyTemplate } from '../../templates/python.js'
import { getGeneratorMetadata } from '../../../lib/generators.js'

export default async (fastify: FastifyInstance) => {
fastify.get<{
Headers: { pg: string }
Querystring: {
excluded_schemas?: string
included_schemas?: string
}
}>('/', async (request, reply) => {
const connectionString = request.headers.pg
const excludedSchemas =
request.query.excluded_schemas?.split(',').map((schema) => schema.trim()) ?? []
const includedSchemas =
request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? []

const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
Comment on lines +4 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick

Would need to use x-application-name headers and utils like the other generation routes to track where the query originate from.

Suggested change
import { extractRequestForLogging } from '../../utils.js'
import { apply as applyPyTemplate } from '../../templates/python.js'
import { getGeneratorMetadata } from '../../../lib/generators.js'
export default async (fastify: FastifyInstance) => {
fastify.get<{
Headers: { pg: string }
Querystring: {
excluded_schemas?: string
included_schemas?: string
}
}>('/', async (request, reply) => {
const connectionString = request.headers.pg
const excludedSchemas =
request.query.excluded_schemas?.split(',').map((schema) => schema.trim()) ?? []
const includedSchemas =
request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? []
const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
import { createConnectionConfig, extractRequestForLogging } from '../../utils.js'
import { apply as applyPyTemplate } from '../../templates/python.js'
import { getGeneratorMetadata } from '../../../lib/generators.js'
export default async (fastify: FastifyInstance) => {
fastify.get<{
Headers: { pg: string; 'x-pg-application-name'?: string }
Querystring: {
excluded_schemas?: string
included_schemas?: string
}
}>('/', async (request, reply) => {
const config = createConnectionConfig(request)
const excludedSchemas =
request.query.excluded_schemas?.split(',').map((schema) => schema.trim()) ?? []
const includedSchemas =
request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? []
const pgMeta: PostgresMeta = new PostgresMeta(config)

const { data: generatorMeta, error: generatorMetaError } = await getGeneratorMetadata(pgMeta, {
includedSchemas,
excludedSchemas,
})
if (generatorMetaError) {
request.log.error({ error: generatorMetaError, request: extractRequestForLogging(request) })
reply.code(500)
return { error: generatorMetaError.message }
}

return applyPyTemplate(generatorMeta)
})
}
2 changes: 2 additions & 0 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ViewsRoute from './views.js'
import TypeScriptTypeGenRoute from './generators/typescript.js'
import GoTypeGenRoute from './generators/go.js'
import SwiftTypeGenRoute from './generators/swift.js'
import PythonTypeGenRoute from './generators/python.js'
import { PG_CONNECTION, CRYPTO_KEY } from '../constants.js'

export default async (fastify: FastifyInstance) => {
Expand Down Expand Up @@ -82,4 +83,5 @@ export default async (fastify: FastifyInstance) => {
fastify.register(TypeScriptTypeGenRoute, { prefix: '/generators/typescript' })
fastify.register(GoTypeGenRoute, { prefix: '/generators/go' })
fastify.register(SwiftTypeGenRoute, { prefix: '/generators/swift' })
fastify.register(PythonTypeGenRoute, { prefix: '/generators/python' })
}
Loading