1- import swagger from '@fastify/swagger'
21import cors from '@fastify/cors'
3- import { fastify } from 'fastify'
4- import { pino } from 'pino'
5- import { PostgresMeta } from '../lib/index.js'
6- import {
7- DEFAULT_POOL_CONFIG ,
8- EXPORT_DOCS ,
9- GENERATE_TYPES ,
10- GENERATE_TYPES_INCLUDED_SCHEMAS ,
11- PG_CONNECTION ,
12- PG_META_HOST ,
13- PG_META_PORT ,
14- PG_META_REQ_HEADER ,
15- } from './constants.js'
2+ import swagger from '@fastify/swagger'
3+ import { fastify , FastifyInstance , FastifyServerOptions } from 'fastify'
4+ import { PG_META_REQ_HEADER } from './constants.js'
165import routes from './routes/index.js'
17- import { apply as applyTypescriptTemplate } from './templates/typescript.js'
186import { extractRequestForLogging } from './utils.js'
19- import { build as buildAdminApp } from './admin-app.js'
207// Pseudo package declared only for this module
218import pkg from '#package.json' assert { type : 'json' }
229
23- const logger = pino ( {
24- formatters : {
25- level ( label ) {
26- return { level : label }
27- } ,
28- } ,
29- timestamp : pino . stdTimeFunctions . isoTime ,
30- } )
31-
32- const app = fastify ( {
33- logger,
34- disableRequestLogging : true ,
35- requestIdHeader : PG_META_REQ_HEADER ,
36- } )
10+ export const build = ( opts : FastifyServerOptions = { } ) : FastifyInstance => {
11+ const app = fastify ( {
12+ disableRequestLogging : true ,
13+ requestIdHeader : PG_META_REQ_HEADER ,
14+ ...opts ,
15+ } )
3716
38- app . setErrorHandler ( ( error , request , reply ) => {
39- app . log . error ( { error : error . toString ( ) , request : extractRequestForLogging ( request ) } )
40- reply . code ( 500 ) . send ( { error : error . message } )
41- } )
17+ app . setErrorHandler ( ( error , request , reply ) => {
18+ app . log . error ( { error : error . toString ( ) , request : extractRequestForLogging ( request ) } )
19+ reply . code ( 500 ) . send ( { error : error . message } )
20+ } )
4221
43- app . setNotFoundHandler ( ( request , reply ) => {
44- app . log . error ( { error : 'Not found' , request : extractRequestForLogging ( request ) } )
45- reply . code ( 404 ) . send ( { error : 'Not found' } )
46- } )
22+ app . setNotFoundHandler ( ( request , reply ) => {
23+ app . log . error ( { error : 'Not found' , request : extractRequestForLogging ( request ) } )
24+ reply . code ( 404 ) . send ( { error : 'Not found' } )
25+ } )
4726
48- if ( EXPORT_DOCS ) {
4927 app . register ( swagger , {
5028 openapi : {
5129 servers : [ ] ,
@@ -57,85 +35,22 @@ if (EXPORT_DOCS) {
5735 } ,
5836 } )
5937
60- app . ready ( ( ) => {
61- // @ts -ignore: app.swagger() is a Fastify decorator, so doesn't show up in the types
62- console . log ( JSON . stringify ( app . swagger ( ) , null , 2 ) )
63- } )
64- } else if ( GENERATE_TYPES === 'typescript' ) {
65- ; ( async ( ) => {
66- const pgMeta : PostgresMeta = new PostgresMeta ( {
67- ...DEFAULT_POOL_CONFIG ,
68- connectionString : PG_CONNECTION ,
69- } )
70- const { data : schemas , error : schemasError } = await pgMeta . schemas . list ( )
71- const { data : tables , error : tablesError } = await pgMeta . tables . list ( )
72- const { data : views , error : viewsError } = await pgMeta . views . list ( )
73- const { data : functions , error : functionsError } = await pgMeta . functions . list ( )
74- const { data : types , error : typesError } = await pgMeta . types . list ( {
75- includeArrayTypes : true ,
76- includeSystemSchemas : true ,
77- } )
78- await pgMeta . end ( )
38+ app . register ( cors )
7939
80- if ( schemasError ) {
81- throw new Error ( schemasError . message )
82- }
83- if ( tablesError ) {
84- throw new Error ( tablesError . message )
40+ app . get ( '/' , async ( _request , _reply ) => {
41+ return {
42+ status : 200 ,
43+ name : pkg . name ,
44+ version : pkg . version ,
45+ documentation : 'https://github.com/supabase/postgres-meta' ,
8546 }
86- if ( viewsError ) {
87- throw new Error ( viewsError . message )
88- }
89- if ( functionsError ) {
90- throw new Error ( functionsError . message )
91- }
92- if ( typesError ) {
93- throw new Error ( typesError . message )
94- }
95-
96- console . log (
97- applyTypescriptTemplate ( {
98- schemas : schemas . filter (
99- ( { name } ) =>
100- GENERATE_TYPES_INCLUDED_SCHEMAS . length === 0 ||
101- GENERATE_TYPES_INCLUDED_SCHEMAS . includes ( name )
102- ) ,
103- tables,
104- views,
105- functions : functions . filter (
106- ( { return_type } ) => ! [ 'trigger' , 'event_trigger' ] . includes ( return_type )
107- ) ,
108- types : types . filter ( ( { name } ) => name [ 0 ] !== '_' ) ,
109- arrayTypes : types . filter ( ( { name } ) => name [ 0 ] === '_' ) ,
110- } )
111- )
112- } ) ( )
113- } else {
114- app . ready ( ( ) => {
115- app . listen ( { port : PG_META_PORT , host : PG_META_HOST } , ( ) => {
116- app . log . info ( `App started on port ${ PG_META_PORT } ` )
117- const adminApp = buildAdminApp ( { logger } )
118- const adminPort = PG_META_PORT + 1
119- adminApp . listen ( { port : adminPort , host : PG_META_HOST } , ( ) => {
120- adminApp . log . info ( `Admin App started on port ${ adminPort } ` )
121- } )
122- } )
12347 } )
124- }
125-
126- app . register ( cors )
12748
128- app . get ( '/' , async ( _request , _reply ) => {
129- return {
130- status : 200 ,
131- name : pkg . name ,
132- version : pkg . version ,
133- documentation : 'https://github.com/supabase/postgres-meta' ,
134- }
135- } )
49+ app . get ( '/health' , async ( _request , _reply ) => {
50+ return { date : new Date ( ) }
51+ } )
13652
137- app . get ( '/health' , async ( _request , _reply ) => {
138- return { date : new Date ( ) }
139- } )
53+ app . register ( routes )
14054
141- app . register ( routes )
55+ return app
56+ }
0 commit comments