fix: Einzelner shared Pool, finally-Block verschluckt nicht Original-Fehler, Pool-Error-Handler

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-17 08:30:57 +00:00
parent cdfdd025d2
commit c01befa477
3 changed files with 16 additions and 9 deletions

View File

@@ -1,6 +1,10 @@
import { Pool, type QueryResultRow } from 'pg'
const pool = new Pool({ connectionString: process.env.DATABASE_URL })
export const pool = new Pool({ connectionString: process.env.DATABASE_URL })
pool.on('error', (err) => {
console.error({ msg: 'Idle pool connection error', error: err.message })
})
export const db = {
execute: (sql: string) => pool.query(sql),

View File

@@ -1,15 +1,15 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
// Pool mocken — gibt einen fake PoolClient zurück
// Pool aus client.ts mocken — gibt einen fake PoolClient zurück
const mockClient = vi.hoisted(() => ({
query: vi.fn(),
release: vi.fn(),
}))
vi.mock('pg', () => ({
Pool: vi.fn().mockImplementation(() => ({
vi.mock('./client', () => ({
pool: {
connect: vi.fn().mockResolvedValue(mockClient),
})),
},
}))
import { withTenant } from './tenant'

View File

@@ -1,6 +1,5 @@
import { Pool, type QueryResultRow } from 'pg'
const pool = new Pool({ connectionString: process.env.DATABASE_URL })
import { type QueryResultRow } from 'pg'
import { pool } from './client'
interface TenantClient {
execute: (sql: string) => Promise<void>
@@ -25,7 +24,11 @@ export async function withTenant<T>(
}
return await fn(tenantClient)
} finally {
try {
await client.query('SET search_path = public')
} catch (resetErr) {
console.error({ msg: 'search_path reset failed', error: (resetErr as Error).message })
}
client.release()
}
}