11import 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'
312import routes from './routes'
13+ import { apply as applyTypescriptTemplate } from './templates/typescript'
414import { extractRequestForLogging } from './utils'
515import pino from 'pino'
616import 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' , ( ) => {
0 commit comments