1
1
import { FastifyInstance } from 'fastify'
2
- import { PostgresMeta } from '../../lib'
3
- import { DEFAULT_POOL_CONFIG } from '../constants'
2
+ import PgMetaCache from '../pgMetaCache'
4
3
5
4
export default async ( fastify : FastifyInstance ) => {
6
5
fastify . get < {
@@ -14,9 +13,8 @@ export default async (fastify: FastifyInstance) => {
14
13
const limit = request . query . limit
15
14
const offset = request . query . offset
16
15
17
- const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
16
+ const pgMeta = PgMetaCache . get ( connectionString )
18
17
const { data, error } = await pgMeta . extensions . list ( { limit, offset } )
19
- await pgMeta . end ( )
20
18
if ( error ) {
21
19
request . log . error ( JSON . stringify ( { error, req : request . body } ) )
22
20
reply . code ( 500 )
@@ -34,9 +32,8 @@ export default async (fastify: FastifyInstance) => {
34
32
} > ( '/:name' , async ( request , reply ) => {
35
33
const connectionString = request . headers . pg
36
34
37
- const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
35
+ const pgMeta = PgMetaCache . get ( connectionString )
38
36
const { data, error } = await pgMeta . extensions . retrieve ( { name : request . params . name } )
39
- await pgMeta . end ( )
40
37
if ( error ) {
41
38
request . log . error ( JSON . stringify ( { error, req : request . body } ) )
42
39
reply . code ( 404 )
@@ -52,9 +49,8 @@ export default async (fastify: FastifyInstance) => {
52
49
} > ( '/' , async ( request , reply ) => {
53
50
const connectionString = request . headers . pg
54
51
55
- const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
52
+ const pgMeta = PgMetaCache . get ( connectionString )
56
53
const { data, error } = await pgMeta . extensions . create ( request . body )
57
- await pgMeta . end ( )
58
54
if ( error ) {
59
55
request . log . error ( JSON . stringify ( { error, req : request . body } ) )
60
56
reply . code ( 400 )
@@ -73,9 +69,8 @@ export default async (fastify: FastifyInstance) => {
73
69
} > ( '/:name' , async ( request , reply ) => {
74
70
const connectionString = request . headers . pg
75
71
76
- const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
72
+ const pgMeta = PgMetaCache . get ( connectionString )
77
73
const { data, error } = await pgMeta . extensions . update ( request . params . name , request . body )
78
- await pgMeta . end ( )
79
74
if ( error ) {
80
75
request . log . error ( JSON . stringify ( { error, req : request . body } ) )
81
76
reply . code ( 400 )
@@ -98,9 +93,8 @@ export default async (fastify: FastifyInstance) => {
98
93
const connectionString = request . headers . pg
99
94
const cascade = request . query . cascade === 'true'
100
95
101
- const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
96
+ const pgMeta = PgMetaCache . get ( connectionString )
102
97
const { data, error } = await pgMeta . extensions . remove ( request . params . name , { cascade } )
103
- await pgMeta . end ( )
104
98
if ( error ) {
105
99
request . log . error ( JSON . stringify ( { error, req : request . body } ) )
106
100
reply . code ( 400 )
0 commit comments