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