11import { 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'
43import { Type } from '@sinclair/typebox'
54import {
65 postgresColumnCreateSchema ,
@@ -16,6 +15,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
1615 schema : {
1716 headers : Type . Object ( {
1817 pg : Type . String ( ) ,
18+ 'x-pg-application-name' : Type . Optional ( Type . String ( ) ) ,
1919 } ) ,
2020 querystring : Type . Object ( {
2121 include_system_schemas : Type . Optional ( Type . Boolean ( ) ) ,
@@ -33,14 +33,14 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
3333 } ,
3434 } ,
3535 async ( request , reply ) => {
36- const connectionString = request . headers . pg
3736 const includeSystemSchemas = request . query . include_system_schemas
3837 const includedSchemas = request . query . included_schemas ?. split ( ',' )
3938 const excludedSchemas = request . query . excluded_schemas ?. split ( ',' )
4039 const limit = request . query . limit
4140 const offset = request . query . offset
4241
43- const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
42+ const config = createConnectionConfig ( request )
43+ const pgMeta = new PostgresMeta ( config )
4444 const { data, error } = await pgMeta . columns . list ( {
4545 includeSystemSchemas,
4646 includedSchemas,
@@ -65,6 +65,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
6565 schema : {
6666 headers : Type . Object ( {
6767 pg : Type . String ( ) ,
68+ 'x-pg-application-name' : Type . Optional ( Type . String ( ) ) ,
6869 } ) ,
6970 params : Type . Object ( {
7071 tableId : Type . String ( ) ,
@@ -86,13 +87,13 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
8687 async ( request , reply ) => {
8788 if ( request . params . ordinalPosition === '' ) {
8889 const {
89- headers : { pg : connectionString } ,
9090 query : { limit, offset } ,
9191 params : { tableId } ,
9292 } = request
9393 const includeSystemSchemas = request . query . include_system_schemas
9494
95- const pgMeta : PostgresMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
95+ const config = createConnectionConfig ( request )
96+ const pgMeta : PostgresMeta = new PostgresMeta ( config )
9697 const { data, error } = await pgMeta . columns . list ( {
9798 tableId : Number ( tableId ) ,
9899 includeSystemSchemas,
@@ -109,12 +110,12 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
109110 return data [ 0 ]
110111 } else if ( / ^ \. \d + $ / . test ( request . params . ordinalPosition ) ) {
111112 const {
112- headers : { pg : connectionString } ,
113113 params : { tableId, ordinalPosition : ordinalPositionWithDot } ,
114114 } = request
115115 const ordinalPosition = ordinalPositionWithDot . slice ( 1 )
116116
117- const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
117+ const config = createConnectionConfig ( request )
118+ const pgMeta = new PostgresMeta ( config )
118119 const { data, error } = await pgMeta . columns . retrieve ( {
119120 id : `${ tableId } .${ ordinalPosition } ` ,
120121 } )
@@ -139,6 +140,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
139140 schema : {
140141 headers : Type . Object ( {
141142 pg : Type . String ( ) ,
143+ 'x-pg-application-name' : Type . Optional ( Type . String ( ) ) ,
142144 } ) ,
143145 body : postgresColumnCreateSchema ,
144146 response : {
@@ -150,9 +152,8 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
150152 } ,
151153 } ,
152154 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 )
156157 const { data, error } = await pgMeta . columns . create ( request . body )
157158 await pgMeta . end ( )
158159 if ( error ) {
@@ -172,6 +173,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
172173 schema : {
173174 headers : Type . Object ( {
174175 pg : Type . String ( ) ,
176+ 'x-pg-application-name' : Type . Optional ( Type . String ( ) ) ,
175177 } ) ,
176178 params : Type . Object ( {
177179 id : Type . String ( ) ,
@@ -186,9 +188,8 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
186188 } ,
187189 } ,
188190 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 )
192193 const { data, error } = await pgMeta . columns . update ( request . params . id , request . body )
193194 await pgMeta . end ( )
194195 if ( error ) {
@@ -208,6 +209,7 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
208209 schema : {
209210 headers : Type . Object ( {
210211 pg : Type . String ( ) ,
212+ 'x-pg-application-name' : Type . Optional ( Type . String ( ) ) ,
211213 } ) ,
212214 params : Type . Object ( {
213215 id : Type . String ( ) ,
@@ -224,10 +226,10 @@ const route: FastifyPluginAsyncTypebox = async (fastify) => {
224226 } ,
225227 } ,
226228 async ( request , reply ) => {
227- const connectionString = request . headers . pg
228229 const cascade = request . query . cascade === 'true'
229230
230- const pgMeta = new PostgresMeta ( { ...DEFAULT_POOL_CONFIG , connectionString } )
231+ const config = createConnectionConfig ( request )
232+ const pgMeta = new PostgresMeta ( config )
231233 const { data, error } = await pgMeta . columns . remove ( request . params . id , { cascade } )
232234 await pgMeta . end ( )
233235 if ( error ) {
0 commit comments