Skip to content

Commit

Permalink
feat(typegen): add swift template (#779)
Browse files Browse the repository at this point in the history
* feat(typegen): add swift template

* test: add test case for table with identity other than id

* update test snapshots

* test: add public access control test case for swift generator

* feat: generate materializedviews, views and composite types for swift

* refactor swift typegen
  • Loading branch information
grdsdev authored Jun 24, 2024
1 parent a473298 commit d280bc1
Show file tree
Hide file tree
Showing 9 changed files with 1,283 additions and 149 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"docs:export": "PG_META_EXPORT_DOCS=true node --loader ts-node/esm src/server/server.ts > openapi.json",
"gen:types:typescript": "PG_META_GENERATE_TYPES=typescript node --loader ts-node/esm src/server/server.ts",
"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",
"start": "node dist/server/server.js",
"dev": "trap 'npm run db:clean' INT && run-s db:clean db:run && nodemon --exec node --loader ts-node/esm src/server/server.ts | pino-pretty --colorize",
"test": "run-s db:clean db:run test:run db:clean",
Expand Down
4 changes: 3 additions & 1 deletion src/server/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import crypto from 'crypto'
import { PoolConfig } from 'pg'
import { getSecret } from '../lib/secrets.js'
import { AccessControl } from './templates/swift.js'

export const PG_META_HOST = process.env.PG_META_HOST || '0.0.0.0'
export const PG_META_PORT = Number(process.env.PG_META_PORT || 1337)
Expand Down Expand Up @@ -40,7 +41,8 @@ export const GENERATE_TYPES_INCLUDED_SCHEMAS = GENERATE_TYPES
: []
export const GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS =
process.env.PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS === 'true'

export const GENERATE_TYPES_SWIFT_ACCESS_CONTROL =
(process.env.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL as AccessControl) || 'internal'
export const DEFAULT_POOL_CONFIG: PoolConfig = {
max: 1,
connectionTimeoutMillis: PG_CONN_TIMEOUT_SECS * 1000,
Expand Down
40 changes: 40 additions & 0 deletions src/server/routes/generators/swift.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 applySwiftTemplate, AccessControl } from '../../templates/swift.js'
import { getGeneratorMetadata } from '../../../lib/generators.js'

export default async (fastify: FastifyInstance) => {
fastify.get<{
Headers: { pg: string }
Querystring: {
excluded_schemas?: string
included_schemas?: string
access_control?: AccessControl
}
}>('/', 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 accessControl = request.query.access_control ?? 'internal'

const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
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 applySwiftTemplate({
...generatorMeta,
accessControl,
})
})
}
2 changes: 2 additions & 0 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import TypesRoute from './types.js'
import ViewsRoute from './views.js'
import TypeScriptTypeGenRoute from './generators/typescript.js'
import GoTypeGenRoute from './generators/go.js'
import SwiftTypeGenRoute from './generators/swift.js'
import { PG_CONNECTION, CRYPTO_KEY } from '../constants.js'

export default async (fastify: FastifyInstance) => {
Expand Down Expand Up @@ -65,4 +66,5 @@ export default async (fastify: FastifyInstance) => {
fastify.register(ViewsRoute, { prefix: '/views' })
fastify.register(TypeScriptTypeGenRoute, { prefix: '/generators/typescript' })
fastify.register(GoTypeGenRoute, { prefix: '/generators/go' })
fastify.register(SwiftTypeGenRoute, { prefix: '/generators/swift' })
}
7 changes: 7 additions & 0 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {
GENERATE_TYPES,
GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
GENERATE_TYPES_INCLUDED_SCHEMAS,
GENERATE_TYPES_SWIFT_ACCESS_CONTROL,
PG_CONNECTION,
PG_META_HOST,
PG_META_PORT,
} from './constants.js'
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({
Expand Down Expand Up @@ -52,6 +54,11 @@ async function getTypeOutput(): Promise<string | null> {
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
Expand Down
Loading

0 comments on commit d280bc1

Please sign in to comment.