Skip to content

Commit 2edddd3

Browse files
committed
feat: shoehorn CLI-mode
Used for docs export & typegen. Probably need to move it to `src/cli` in the future, but works well enough for now.
1 parent 721b4f5 commit 2edddd3

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"build:main": "tsc -p tsconfig.json && cpy 'src/lib/sql/*.sql' dist/main/sql",
2020
"build:module": "tsc -p tsconfig.module.json && cpy 'src/lib/sql/*.sql' dist/module/sql",
2121
"build:server": "tsc -p tsconfig.server.json && cpy 'src/lib/sql/*.sql' bin/src/lib/sql",
22-
"docs:export": "PG_META_EXPORT_DOCS=true ts-node-dev src/server/app.ts",
22+
"docs:export": "ts-node-dev --quiet src/server/app.ts docs export > openapi.json",
23+
"gen:types:typescript": "ts-node-dev --quiet src/server/app.ts gen types typescript",
2324
"start": "NODE_ENV=production node bin/src/server/app.js",
2425
"dev": "trap 'npm run db:clean' INT && run-s db:clean db:run && NODE_ENV=development ts-node-dev src/server/app.ts | pino-pretty --colorize",
2526
"pkg": "run-s clean build:server && pkg --out-path bin .pkg.config.json",

src/server/app.ts

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import fastify from 'fastify'
2-
import { PG_META_EXPORT_DOCS, PG_META_PORT, PG_META_REQ_HEADER } from './constants'
2+
import { PostgresMeta } from '../lib'
3+
import {
4+
DEFAULT_POOL_CONFIG,
5+
EXPORT_DOCS,
6+
GENERATE_TYPES,
7+
GENERATE_TYPES_EXCLUDED_SCHEMAS,
8+
PG_CONNECTION,
9+
PG_META_PORT,
10+
PG_META_REQ_HEADER,
11+
} from './constants'
312
import routes from './routes'
13+
import { apply as applyTypescriptTemplate } from './templates/typescript'
414
import { extractRequestForLogging } from './utils'
515
import pino from 'pino'
616
import pkg from '../../package.json'
@@ -31,7 +41,7 @@ app.setNotFoundHandler((request, reply) => {
3141
reply.code(404).send({ error: 'Not found' })
3242
})
3343

34-
if (PG_META_EXPORT_DOCS) {
44+
if (EXPORT_DOCS) {
3545
app.register(require('fastify-swagger'), {
3646
openapi: {
3747
servers: [],
@@ -44,16 +54,45 @@ if (PG_META_EXPORT_DOCS) {
4454
})
4555

4656
app.ready(() => {
47-
require('fs').writeFileSync(
48-
'openapi.json',
49-
JSON.stringify(
50-
// @ts-ignore: app.swagger() is a Fastify decorator, so doesn't show up in the types
51-
app.swagger(),
52-
null,
53-
2
54-
) + '\n'
55-
)
57+
// @ts-ignore: app.swagger() is a Fastify decorator, so doesn't show up in the types
58+
console.log(JSON.stringify(app.swagger(), null, 2))
5659
})
60+
} else if (GENERATE_TYPES === 'typescript') {
61+
;(async () => {
62+
const pgMeta: PostgresMeta = new PostgresMeta({
63+
...DEFAULT_POOL_CONFIG,
64+
connectionString: PG_CONNECTION,
65+
})
66+
const { data: schemas, error: schemasError } = await pgMeta.schemas.list()
67+
const { data: tables, error: tablesError } = await pgMeta.tables.list()
68+
const { data: functions, error: functionsError } = await pgMeta.functions.list()
69+
const { data: types, error: typesError } = await pgMeta.types.list({
70+
includeSystemSchemas: true,
71+
})
72+
await pgMeta.end()
73+
74+
if (schemasError) {
75+
throw schemasError
76+
}
77+
if (tablesError) {
78+
throw schemasError
79+
}
80+
if (functionsError) {
81+
throw schemasError
82+
}
83+
if (typesError) {
84+
throw typesError
85+
}
86+
87+
console.log(
88+
applyTypescriptTemplate({
89+
schemas: schemas.filter(({ name }) => !GENERATE_TYPES_EXCLUDED_SCHEMAS.includes(name)),
90+
tables,
91+
functions,
92+
types,
93+
})
94+
)
95+
})()
5796
} else {
5897
app.ready(() => {
5998
app.listen(PG_META_PORT, '0.0.0.0', () => {

src/server/constants.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ const PG_CONN_TIMEOUT_SECS = Number(process.env.PG_CONN_TIMEOUT_SECS || 15)
1212

1313
export const PG_CONNECTION = `postgres://${PG_META_DB_USER}:${PG_META_DB_PASSWORD}@${PG_META_DB_HOST}:${PG_META_DB_PORT}/${PG_META_DB_NAME}?sslmode=${PG_META_DB_SSL_MODE}`
1414

15-
export const PG_META_EXPORT_DOCS = process.env.PG_META_EXPORT_DOCS === 'true' || false
15+
export const EXPORT_DOCS = process.argv[2] === 'docs' && process.argv[3] === 'export'
16+
export const GENERATE_TYPES =
17+
process.argv[2] === 'gen' && process.argv[3] === 'types' ? process.argv[4] : undefined
18+
export const GENERATE_TYPES_EXCLUDED_SCHEMAS =
19+
GENERATE_TYPES && process.argv[5] === '--exclude-schemas' ? process.argv[6]?.split(',') ?? [] : []
1620

1721
export const DEFAULT_POOL_CONFIG = { max: 1, connectionTimeoutMillis: PG_CONN_TIMEOUT_SECS * 1000 }
1822
export const PG_META_REQ_HEADER = process.env.PG_META_REQ_HEADER || 'request-id'

0 commit comments

Comments
 (0)