Skip to content

Commit d280bc1

Browse files
authored
feat(typegen): add swift template (#779)
* 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
1 parent a473298 commit d280bc1

File tree

9 files changed

+1283
-149
lines changed

9 files changed

+1283
-149
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"docs:export": "PG_META_EXPORT_DOCS=true node --loader ts-node/esm src/server/server.ts > openapi.json",
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",
27+
"gen:types:swift": "PG_META_GENERATE_TYPES=swift node --loader ts-node/esm src/server/server.ts",
2728
"start": "node dist/server/server.js",
2829
"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",
2930
"test": "run-s db:clean db:run test:run db:clean",

src/server/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import crypto from 'crypto'
22
import { PoolConfig } from 'pg'
33
import { getSecret } from '../lib/secrets.js'
4+
import { AccessControl } from './templates/swift.js'
45

56
export const PG_META_HOST = process.env.PG_META_HOST || '0.0.0.0'
67
export const PG_META_PORT = Number(process.env.PG_META_PORT || 1337)
@@ -40,7 +41,8 @@ export const GENERATE_TYPES_INCLUDED_SCHEMAS = GENERATE_TYPES
4041
: []
4142
export const GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS =
4243
process.env.PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS === 'true'
43-
44+
export const GENERATE_TYPES_SWIFT_ACCESS_CONTROL =
45+
(process.env.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL as AccessControl) || 'internal'
4446
export const DEFAULT_POOL_CONFIG: PoolConfig = {
4547
max: 1,
4648
connectionTimeoutMillis: PG_CONN_TIMEOUT_SECS * 1000,

src/server/routes/generators/swift.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { FastifyInstance } from 'fastify'
2+
import { PostgresMeta } from '../../../lib/index.js'
3+
import { DEFAULT_POOL_CONFIG } from '../../constants.js'
4+
import { extractRequestForLogging } from '../../utils.js'
5+
import { apply as applySwiftTemplate, AccessControl } from '../../templates/swift.js'
6+
import { getGeneratorMetadata } from '../../../lib/generators.js'
7+
8+
export default async (fastify: FastifyInstance) => {
9+
fastify.get<{
10+
Headers: { pg: string }
11+
Querystring: {
12+
excluded_schemas?: string
13+
included_schemas?: string
14+
access_control?: AccessControl
15+
}
16+
}>('/', async (request, reply) => {
17+
const connectionString = request.headers.pg
18+
const excludedSchemas =
19+
request.query.excluded_schemas?.split(',').map((schema) => schema.trim()) ?? []
20+
const includedSchemas =
21+
request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? []
22+
const accessControl = request.query.access_control ?? 'internal'
23+
24+
const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
25+
const { data: generatorMeta, error: generatorMetaError } = await getGeneratorMetadata(pgMeta, {
26+
includedSchemas,
27+
excludedSchemas,
28+
})
29+
if (generatorMetaError) {
30+
request.log.error({ error: generatorMetaError, request: extractRequestForLogging(request) })
31+
reply.code(500)
32+
return { error: generatorMetaError.message }
33+
}
34+
35+
return applySwiftTemplate({
36+
...generatorMeta,
37+
accessControl,
38+
})
39+
})
40+
}

src/server/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import TypesRoute from './types.js'
2020
import ViewsRoute from './views.js'
2121
import TypeScriptTypeGenRoute from './generators/typescript.js'
2222
import GoTypeGenRoute from './generators/go.js'
23+
import SwiftTypeGenRoute from './generators/swift.js'
2324
import { PG_CONNECTION, CRYPTO_KEY } from '../constants.js'
2425

2526
export default async (fastify: FastifyInstance) => {
@@ -65,4 +66,5 @@ export default async (fastify: FastifyInstance) => {
6566
fastify.register(ViewsRoute, { prefix: '/views' })
6667
fastify.register(TypeScriptTypeGenRoute, { prefix: '/generators/typescript' })
6768
fastify.register(GoTypeGenRoute, { prefix: '/generators/go' })
69+
fastify.register(SwiftTypeGenRoute, { prefix: '/generators/swift' })
6870
}

src/server/server.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import {
99
GENERATE_TYPES,
1010
GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
1111
GENERATE_TYPES_INCLUDED_SCHEMAS,
12+
GENERATE_TYPES_SWIFT_ACCESS_CONTROL,
1213
PG_CONNECTION,
1314
PG_META_HOST,
1415
PG_META_PORT,
1516
} from './constants.js'
1617
import { apply as applyTypescriptTemplate } from './templates/typescript.js'
1718
import { apply as applyGoTemplate } from './templates/go.js'
19+
import { apply as applySwiftTemplate } from './templates/swift.js'
1820
import { getGeneratorMetadata } from '../lib/generators.js'
1921

2022
const logger = pino({
@@ -52,6 +54,11 @@ async function getTypeOutput(): Promise<string | null> {
5254
detectOneToOneRelationships: GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
5355
})
5456
break
57+
case 'swift':
58+
output = await applySwiftTemplate({
59+
...generatorMetadata,
60+
accessControl: GENERATE_TYPES_SWIFT_ACCESS_CONTROL,
61+
})
5562
case 'go':
5663
output = applyGoTemplate(generatorMetadata)
5764
break

0 commit comments

Comments
 (0)