1
1
import { PostgresMeta } from '../../lib/index.js'
2
- import { DEFAULT_POOL_CONFIG } from '../constants.js'
3
- import { extractRequestForLogging } from '../utils.js'
2
+ import { createConnectionConfig , extractRequestForLogging } from '../utils.js'
4
3
import { Type } from '@sinclair/typebox'
5
4
import {
6
5
postgresColumnCreateSchema ,
@@ -16,6 +15,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
16
15
schema : {
17
16
headers : Type . Object ( {
18
17
pg : Type . String ( ) ,
18
+ 'x-pg-application-name' : Type . Optional ( Type . String ( ) ) ,
19
19
} ) ,
20
20
querystring : Type . Object ( {
21
21
include_system_schemas : Type . Optional ( Type . Boolean ( ) ) ,
@@ -33,14 +33,14 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
33
33
} ,
34
34
} ,
35
35
async ( request , reply ) => {
36
- const connectionString = request . headers . pg
37
36
const includeSystemSchemas = request . query . include_system_schemas
38
37
const includedSchemas = request . query . included_schemas ?. split ( ',' )
39
38
const excludedSchemas = request . query . excluded_schemas ?. split ( ',' )
40
39
const limit = request . query . limit
41
40
const offset = request . query . offset
42
41
43
- const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
42
+ const config = createConnectionConfig ( request )
43
+ const pgMeta = new PostgresMeta ( config )
44
44
const { data, error } = await pgMeta . columns . list ( {
45
45
includeSystemSchemas,
46
46
includedSchemas,
@@ -65,6 +65,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
65
65
schema : {
66
66
headers : Type . Object ( {
67
67
pg : Type . String ( ) ,
68
+ 'x-pg-application-name' : Type . Optional ( Type . String ( ) ) ,
68
69
} ) ,
69
70
params : Type . Object ( {
70
71
tableId : Type . String ( ) ,
@@ -86,13 +87,13 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
86
87
async ( request , reply ) => {
87
88
if ( request . params . ordinalPosition === '' ) {
88
89
const {
89
- headers : { pg : connectionString } ,
90
90
query : { limit, offset } ,
91
91
params : { tableId } ,
92
92
} = request
93
93
const includeSystemSchemas = request . query . include_system_schemas
94
94
95
- const pgMeta : PostgresMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
95
+ const config = createConnectionConfig ( request )
96
+ const pgMeta : PostgresMeta = new PostgresMeta ( config )
96
97
const { data, error } = await pgMeta . columns . list ( {
97
98
tableId : Number ( tableId ) ,
98
99
includeSystemSchemas,
@@ -109,12 +110,12 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
109
110
return data [ 0 ]
110
111
} else if ( / ^ \. \d + $ / . test ( request . params . ordinalPosition ) ) {
111
112
const {
112
- headers : { pg : connectionString } ,
113
113
params : { tableId, ordinalPosition : ordinalPositionWithDot } ,
114
114
} = request
115
115
const ordinalPosition = ordinalPositionWithDot . slice ( 1 )
116
116
117
- const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
117
+ const config = createConnectionConfig ( request )
118
+ const pgMeta = new PostgresMeta ( config )
118
119
const { data, error } = await pgMeta . columns . retrieve ( {
119
120
id : `${ tableId } .${ ordinalPosition } ` ,
120
121
} )
@@ -139,6 +140,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
139
140
schema : {
140
141
headers : Type . Object ( {
141
142
pg : Type . String ( ) ,
143
+ 'x-pg-application-name' : Type . Optional ( Type . String ( ) ) ,
142
144
} ) ,
143
145
body : postgresColumnCreateSchema ,
144
146
response : {
@@ -150,9 +152,8 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
150
152
} ,
151
153
} ,
152
154
async ( request , reply ) => {
153
- const connectionString = request . headers . pg
154
-
155
- const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
155
+ const config = createConnectionConfig ( request )
156
+ const pgMeta = new PostgresMeta ( config )
156
157
const { data, error } = await pgMeta . columns . create ( request . body )
157
158
await pgMeta . end ( )
158
159
if ( error ) {
@@ -172,6 +173,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
172
173
schema : {
173
174
headers : Type . Object ( {
174
175
pg : Type . String ( ) ,
176
+ 'x-pg-application-name' : Type . Optional ( Type . String ( ) ) ,
175
177
} ) ,
176
178
params : Type . Object ( {
177
179
id : Type . String ( ) ,
@@ -186,9 +188,8 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
186
188
} ,
187
189
} ,
188
190
async ( request , reply ) => {
189
- const connectionString = request . headers . pg
190
-
191
- const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
191
+ const config = createConnectionConfig ( request )
192
+ const pgMeta = new PostgresMeta ( config )
192
193
const { data, error } = await pgMeta . columns . update ( request . params . id , request . body )
193
194
await pgMeta . end ( )
194
195
if ( error ) {
@@ -208,6 +209,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
208
209
schema : {
209
210
headers : Type . Object ( {
210
211
pg : Type . String ( ) ,
212
+ 'x-pg-application-name' : Type . Optional ( Type . String ( ) ) ,
211
213
} ) ,
212
214
params : Type . Object ( {
213
215
id : Type . String ( ) ,
@@ -224,10 +226,10 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
224
226
} ,
225
227
} ,
226
228
async ( request , reply ) => {
227
- const connectionString = request . headers . pg
228
229
const cascade = request . query . cascade === 'true'
229
230
230
- const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
231
+ const config = createConnectionConfig ( request )
232
+ const pgMeta = new PostgresMeta ( config )
231
233
const { data, error } = await pgMeta . columns . remove ( request . params . id , { cascade } )
232
234
await pgMeta . end ( )
233
235
if ( error ) {
0 commit comments