fix(db): properly set search_path via URL options for PgBouncer compatibility

This commit is contained in:
2026-03-16 03:20:29 +04:00
parent 543a6f90ef
commit fcafdefa76

View File

@@ -9,13 +9,22 @@ export interface DbClientOptions {
export function createDbClient(databaseUrl: string, options: DbClientOptions = {}) { export function createDbClient(databaseUrl: string, options: DbClientOptions = {}) {
const dbSchema = process.env.DB_SCHEMA || 'public' const dbSchema = process.env.DB_SCHEMA || 'public'
const queryClient = postgres(databaseUrl, { // Parse and clean the URL to set search_path properly
const url = new URL(databaseUrl)
// Remove schema and options params to avoid conflicts
url.searchParams.delete('schema')
url.searchParams.delete('options')
// Set search_path via options parameter (required for PgBouncer compatibility)
url.searchParams.set('options', `-c search_path=${dbSchema}`)
const cleanUrl = url.toString()
const queryClient = postgres(cleanUrl, {
max: options.max ?? 5, max: options.max ?? 5,
prepare: options.prepare ?? false, prepare: options.prepare ?? false,
onnotice: () => {}, onnotice: () => {},
connection: {
search_path: dbSchema
},
transform: { transform: {
...postgres.camel, ...postgres.camel,
undefined: null undefined: null