1
1
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'
3
12
import routes from './routes'
13
+ import { apply as applyTypescriptTemplate } from './templates/typescript'
4
14
import { extractRequestForLogging } from './utils'
5
15
import pino from 'pino'
6
16
import pkg from '../../package.json'
@@ -31,7 +41,7 @@ app.setNotFoundHandler((request, reply) => {
31
41
reply . code ( 404 ) . send ( { error : 'Not found' } )
32
42
} )
33
43
34
- if ( PG_META_EXPORT_DOCS ) {
44
+ if ( EXPORT_DOCS ) {
35
45
app . register ( require ( 'fastify-swagger' ) , {
36
46
openapi : {
37
47
servers : [ ] ,
@@ -44,16 +54,45 @@ if (PG_META_EXPORT_DOCS) {
44
54
} )
45
55
46
56
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 ) )
56
59
} )
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
+ } ) ( )
57
96
} else {
58
97
app . ready ( ( ) => {
59
98
app . listen ( PG_META_PORT , '0.0.0.0' , ( ) => {
0 commit comments