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
3 changes: 2 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ npm run gen:types:typescript # Generate TypeScript types from DB schema
npm run gen:types:python # Generate Python types (Pydantic models)
npm run gen:types:go # Generate Go types
npm run gen:types:swift # Generate Swift types (beta)
npm run gen:types:kotlin # Generate Kotlin types (kotlinx.serialization)

# With custom DB connection:
PG_META_DB_URL=postgresql://... npm run gen:types:typescript
Expand Down Expand Up @@ -121,7 +122,7 @@ Type generation (`npm run gen:types:*`) works by:
4. Templates output type definitions to stdout

Environment variables:
- `PG_META_GENERATE_TYPES`: Language (typescript, python, go, swift)
- `PG_META_GENERATE_TYPES`: Language (typescript, python, go, swift, kotlin)
- `PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS`: Comma-separated schemas to include
- `PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS`: Enable 1:1 relationship detection
- `PG_META_POSTGREST_VERSION`: PostgREST version for TypeScript template compatibility
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"gen:types:go": "PG_META_GENERATE_TYPES=go node --loader ts-node/esm src/server/server.ts",
"gen:types:swift": "PG_META_GENERATE_TYPES=swift node --loader ts-node/esm src/server/server.ts",
"gen:types:python": "PG_META_GENERATE_TYPES=python node --loader ts-node/esm src/server/server.ts",
"gen:types:kotlin": "PG_META_GENERATE_TYPES=kotlin node --loader ts-node/esm src/server/server.ts",
"start": "node dist/server/server.js",
"dev": "trap 'npm run db:clean' INT && run-s db:clean db:run && run-s dev:code",
"dev:code": "nodemon --exec node --loader ts-node/esm src/server/server.ts | pino-pretty --colorize",
Expand Down
4 changes: 4 additions & 0 deletions src/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import crypto from 'crypto'
import { PoolConfig } from '../lib/types.js'
import { getSecret } from '../lib/secrets.js'
import { AccessControl } from './templates/swift.js'
import { Visibility } from './templates/kotlin.js'
import pkg from '#package.json' with { type: 'json' }

export const PG_META_HOST = process.env.PG_META_HOST || '0.0.0.0'
Expand Down Expand Up @@ -50,6 +51,9 @@ export const GENERATE_TYPES_SWIFT_ACCESS_CONTROL = process.env
.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL
? (process.env.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL as AccessControl)
: 'internal'
export const GENERATE_TYPES_KOTLIN_VISIBILITY = process.env.PG_META_GENERATE_TYPES_KOTLIN_VISIBILITY
? (process.env.PG_META_GENERATE_TYPES_KOTLIN_VISIBILITY as Visibility)
: 'public'

// json/jsonb/text types
export const VALID_UNNAMED_FUNCTION_ARG_TYPES = new Set([114, 3802, 25])
Expand Down
39 changes: 39 additions & 0 deletions src/server/routes/generators/kotlin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { FastifyInstance } from 'fastify'
import { PostgresMeta } from '../../../lib/index.js'
import { createConnectionConfig, extractRequestForLogging } from '../../utils.js'
import { apply as applyKotlinTemplate, Visibility } from '../../templates/kotlin.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
visibility?: Visibility
}
}>('/', 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 visibility = request.query.visibility ?? 'public'

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 applyKotlinTemplate({
...generatorMeta,
visibility,
})
})
}
2 changes: 2 additions & 0 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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 KotlinTypeGenRoute from './generators/kotlin.js'
import { PG_CONNECTION, CRYPTO_KEY } from '../constants.js'

export default async (fastify: FastifyInstance) => {
Expand Down Expand Up @@ -84,4 +85,5 @@ export default async (fastify: FastifyInstance) => {
fastify.register(GoTypeGenRoute, { prefix: '/generators/go' })
fastify.register(SwiftTypeGenRoute, { prefix: '/generators/swift' })
fastify.register(PythonTypeGenRoute, { prefix: '/generators/python' })
fastify.register(KotlinTypeGenRoute, { prefix: '/generators/kotlin' })
}
7 changes: 7 additions & 0 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
GENERATE_TYPES,
GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
GENERATE_TYPES_INCLUDED_SCHEMAS,
GENERATE_TYPES_KOTLIN_VISIBILITY,
GENERATE_TYPES_SWIFT_ACCESS_CONTROL,
PG_CONNECTION,
PG_META_HOST,
Expand All @@ -19,6 +20,7 @@ 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 { apply as applyPythonTemplate } from './templates/python.js'
import { apply as applyKotlinTemplate } from './templates/kotlin.js'

const logger = pino({
formatters: {
Expand Down Expand Up @@ -146,6 +148,11 @@ async function getTypeOutput(): Promise<string | null> {
return applyGoTemplate(config)
case 'python':
return applyPythonTemplate(config)
case 'kotlin':
return applyKotlinTemplate({
...config,
visibility: GENERATE_TYPES_KOTLIN_VISIBILITY,
})
default:
throw new Error(`Unsupported language for GENERATE_TYPES: ${GENERATE_TYPES}`)
}
Expand Down
Loading