Skip to content

Commit 38d6ce6

Browse files
authored
Merge b0478c3 into af17401
2 parents af17401 + b0478c3 commit 38d6ce6

File tree

6 files changed

+777
-0
lines changed

6 files changed

+777
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"gen:types:typescript": "PG_META_GENERATE_TYPES=typescript node --loader ts-node/esm src/server/server.ts",
2626
"gen:types:go": "PG_META_GENERATE_TYPES=go node --loader ts-node/esm src/server/server.ts",
2727
"gen:types:swift": "PG_META_GENERATE_TYPES=swift node --loader ts-node/esm src/server/server.ts",
28+
"gen:types:python": "PG_META_GENERATE_TYPES=python node --loader ts-node/esm src/server/server.ts",
2829
"start": "node dist/server/server.js",
2930
"dev": "trap 'npm run db:clean' INT && run-s db:clean db:run && run-s dev:code",
3031
"dev:code": "nodemon --exec node --loader ts-node/esm src/server/server.ts | pino-pretty --colorize",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { FastifyInstance } from 'fastify'
2+
import { PostgresMeta } from '../../../lib/index.js'
3+
import { createConnectionConfig, extractRequestForLogging } from '../../utils.js'
4+
import { apply as applyPyTemplate } from '../../templates/python.js'
5+
import { getGeneratorMetadata } from '../../../lib/generators.js'
6+
7+
export default async (fastify: FastifyInstance) => {
8+
fastify.get<{
9+
Headers: { pg: string; 'x-pg-application-name'?: string }
10+
Querystring: {
11+
excluded_schemas?: string
12+
included_schemas?: string
13+
}
14+
}>('/', async (request, reply) => {
15+
const config = createConnectionConfig(request)
16+
const excludedSchemas =
17+
request.query.excluded_schemas?.split(',').map((schema) => schema.trim()) ?? []
18+
const includedSchemas =
19+
request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? []
20+
const pgMeta: PostgresMeta = new PostgresMeta(config)
21+
const { data: generatorMeta, error: generatorMetaError } = await getGeneratorMetadata(pgMeta, {
22+
includedSchemas,
23+
excludedSchemas,
24+
})
25+
if (generatorMetaError) {
26+
request.log.error({ error: generatorMetaError, request: extractRequestForLogging(request) })
27+
reply.code(500)
28+
return { error: generatorMetaError.message }
29+
}
30+
31+
return applyPyTemplate(generatorMeta)
32+
})
33+
}

src/server/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import ViewsRoute from './views.js'
2121
import TypeScriptTypeGenRoute from './generators/typescript.js'
2222
import GoTypeGenRoute from './generators/go.js'
2323
import SwiftTypeGenRoute from './generators/swift.js'
24+
import PythonTypeGenRoute from './generators/python.js'
2425
import { PG_CONNECTION, CRYPTO_KEY } from '../constants.js'
2526

2627
export default async (fastify: FastifyInstance) => {
@@ -82,4 +83,5 @@ export default async (fastify: FastifyInstance) => {
8283
fastify.register(TypeScriptTypeGenRoute, { prefix: '/generators/typescript' })
8384
fastify.register(GoTypeGenRoute, { prefix: '/generators/go' })
8485
fastify.register(SwiftTypeGenRoute, { prefix: '/generators/swift' })
86+
fastify.register(PythonTypeGenRoute, { prefix: '/generators/python' })
8587
}

src/server/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { apply as applyTypescriptTemplate } from './templates/typescript.js'
1919
import { apply as applyGoTemplate } from './templates/go.js'
2020
import { apply as applySwiftTemplate } from './templates/swift.js'
21+
import { apply as applyPythonTemplate } from './templates/python.js'
2122

2223
const logger = pino({
2324
formatters: {
@@ -143,6 +144,8 @@ async function getTypeOutput(): Promise<string | null> {
143144
})
144145
case 'go':
145146
return applyGoTemplate(config)
147+
case 'python':
148+
return applyPythonTemplate(config)
146149
default:
147150
throw new Error(`Unsupported language for GENERATE_TYPES: ${GENERATE_TYPES}`)
148151
}

0 commit comments

Comments
 (0)