11import { FastifyInstance } from 'fastify'
2- import { PostgresMeta } from '../../lib/index.js'
3- import { DEFAULT_POOL_CONFIG } from '../constants.js'
2+ import PgMetaCache from '../pgMetaCache.js'
43import { extractRequestForLogging } from '../utils.js'
54
65export default async ( fastify : FastifyInstance ) => {
@@ -22,15 +21,14 @@ export default async (fastify: FastifyInstance) => {
2221 const limit = request . query . limit
2322 const offset = request . query . offset
2423
25- const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
24+ const pgMeta = PgMetaCache . get ( connectionString )
2625 const { data, error } = await pgMeta . columns . list ( {
2726 includeSystemSchemas,
2827 includedSchemas,
2928 excludedSchemas,
3029 limit,
3130 offset,
3231 } )
33- await pgMeta . end ( )
3432 if ( error ) {
3533 request . log . error ( { error, request : extractRequestForLogging ( request ) } )
3634 reply . code ( 500 )
@@ -60,14 +58,13 @@ export default async (fastify: FastifyInstance) => {
6058 } = request
6159 const includeSystemSchemas = request . query . include_system_schemas === 'true'
6260
63- const pgMeta : PostgresMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
61+ const pgMeta = PgMetaCache . get ( connectionString )
6462 const { data, error } = await pgMeta . columns . list ( {
6563 tableId : Number ( tableId ) ,
6664 includeSystemSchemas,
6765 limit : Number ( limit ) ,
6866 offset : Number ( offset ) ,
6967 } )
70- await pgMeta . end ( )
7168 if ( error ) {
7269 request . log . error ( { error, request : extractRequestForLogging ( request ) } )
7370 reply . code ( 500 )
@@ -82,9 +79,8 @@ export default async (fastify: FastifyInstance) => {
8279 } = request
8380 const ordinalPosition = ordinalPositionWithDot . slice ( 1 )
8481
85- const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
82+ const pgMeta = PgMetaCache . get ( connectionString )
8683 const { data, error } = await pgMeta . columns . retrieve ( { id : `${ tableId } .${ ordinalPosition } ` } )
87- await pgMeta . end ( )
8884 if ( error ) {
8985 request . log . error ( { error, request : extractRequestForLogging ( request ) } )
9086 reply . code ( 400 )
@@ -104,9 +100,8 @@ export default async (fastify: FastifyInstance) => {
104100 } > ( '/' , async ( request , reply ) => {
105101 const connectionString = request . headers . pg
106102
107- const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
103+ const pgMeta = PgMetaCache . get ( connectionString )
108104 const { data, error } = await pgMeta . columns . create ( request . body as any )
109- await pgMeta . end ( )
110105 if ( error ) {
111106 request . log . error ( { error, request : extractRequestForLogging ( request ) } )
112107 reply . code ( 400 )
@@ -126,9 +121,8 @@ export default async (fastify: FastifyInstance) => {
126121 } > ( '/:id(\\d+\\.\\d+)' , async ( request , reply ) => {
127122 const connectionString = request . headers . pg
128123
129- const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
124+ const pgMeta = PgMetaCache . get ( connectionString )
130125 const { data, error } = await pgMeta . columns . update ( request . params . id , request . body as any )
131- await pgMeta . end ( )
132126 if ( error ) {
133127 request . log . error ( { error, request : extractRequestForLogging ( request ) } )
134128 reply . code ( 400 )
@@ -151,9 +145,8 @@ export default async (fastify: FastifyInstance) => {
151145 const connectionString = request . headers . pg
152146 const cascade = request . query . cascade === 'true'
153147
154- const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
148+ const pgMeta = PgMetaCache . get ( connectionString )
155149 const { data, error } = await pgMeta . columns . remove ( request . params . id , { cascade } )
156- await pgMeta . end ( )
157150 if ( error ) {
158151 request . log . error ( { error, request : extractRequestForLogging ( request ) } )
159152 reply . code ( 400 )
0 commit comments