1
1
import { FastifyInstance } from 'fastify'
2
- import PgMetaCache from '../pgMetaCache.js'
2
+ import { PostgresMeta } from '../../lib/index.js'
3
+ import { DEFAULT_POOL_CONFIG } from '../constants.js'
3
4
import { extractRequestForLogging } from '../utils.js'
4
5
5
6
export default async ( fastify : FastifyInstance ) => {
@@ -21,14 +22,15 @@ export default async (fastify: FastifyInstance) => {
21
22
const limit = request . query . limit
22
23
const offset = request . query . offset
23
24
24
- const pgMeta = PgMetaCache . get ( connectionString )
25
+ const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
25
26
const { data, error } = await pgMeta . columns . list ( {
26
27
includeSystemSchemas,
27
28
includedSchemas,
28
29
excludedSchemas,
29
30
limit,
30
31
offset,
31
32
} )
33
+ await pgMeta . end ( )
32
34
if ( error ) {
33
35
request . log . error ( { error, request : extractRequestForLogging ( request ) } )
34
36
reply . code ( 500 )
@@ -58,13 +60,14 @@ export default async (fastify: FastifyInstance) => {
58
60
} = request
59
61
const includeSystemSchemas = request . query . include_system_schemas === 'true'
60
62
61
- const pgMeta = PgMetaCache . get ( connectionString )
63
+ const pgMeta : PostgresMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
62
64
const { data, error } = await pgMeta . columns . list ( {
63
65
tableId : Number ( tableId ) ,
64
66
includeSystemSchemas,
65
67
limit : Number ( limit ) ,
66
68
offset : Number ( offset ) ,
67
69
} )
70
+ await pgMeta . end ( )
68
71
if ( error ) {
69
72
request . log . error ( { error, request : extractRequestForLogging ( request ) } )
70
73
reply . code ( 500 )
@@ -79,8 +82,9 @@ export default async (fastify: FastifyInstance) => {
79
82
} = request
80
83
const ordinalPosition = ordinalPositionWithDot . slice ( 1 )
81
84
82
- const pgMeta = PgMetaCache . get ( connectionString )
85
+ const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
83
86
const { data, error } = await pgMeta . columns . retrieve ( { id : `${ tableId } .${ ordinalPosition } ` } )
87
+ await pgMeta . end ( )
84
88
if ( error ) {
85
89
request . log . error ( { error, request : extractRequestForLogging ( request ) } )
86
90
reply . code ( 400 )
@@ -100,8 +104,9 @@ export default async (fastify: FastifyInstance) => {
100
104
} > ( '/' , async ( request , reply ) => {
101
105
const connectionString = request . headers . pg
102
106
103
- const pgMeta = PgMetaCache . get ( connectionString )
107
+ const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
104
108
const { data, error } = await pgMeta . columns . create ( request . body as any )
109
+ await pgMeta . end ( )
105
110
if ( error ) {
106
111
request . log . error ( { error, request : extractRequestForLogging ( request ) } )
107
112
reply . code ( 400 )
@@ -121,8 +126,9 @@ export default async (fastify: FastifyInstance) => {
121
126
} > ( '/:id(\\d+\\.\\d+)' , async ( request , reply ) => {
122
127
const connectionString = request . headers . pg
123
128
124
- const pgMeta = PgMetaCache . get ( connectionString )
129
+ const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
125
130
const { data, error } = await pgMeta . columns . update ( request . params . id , request . body as any )
131
+ await pgMeta . end ( )
126
132
if ( error ) {
127
133
request . log . error ( { error, request : extractRequestForLogging ( request ) } )
128
134
reply . code ( 400 )
@@ -145,8 +151,9 @@ export default async (fastify: FastifyInstance) => {
145
151
const connectionString = request . headers . pg
146
152
const cascade = request . query . cascade === 'true'
147
153
148
- const pgMeta = PgMetaCache . get ( connectionString )
154
+ const pgMeta = new PostgresMeta ( { ... DEFAULT_POOL_CONFIG , connectionString } )
149
155
const { data, error } = await pgMeta . columns . remove ( request . params . id , { cascade } )
156
+ await pgMeta . end ( )
150
157
if ( error ) {
151
158
request . log . error ( { error, request : extractRequestForLogging ( request ) } )
152
159
reply . code ( 400 )
0 commit comments