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' 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 = { export const db = {
execute: (sql: string) => pool.query(sql), execute: (sql: string) => pool.query(sql),

View File

@@ -1,15 +1,15 @@
import { describe, it, expect, vi, beforeEach } from 'vitest' 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(() => ({ const mockClient = vi.hoisted(() => ({
query: vi.fn(), query: vi.fn(),
release: vi.fn(), release: vi.fn(),
})) }))
vi.mock('pg', () => ({ vi.mock('./client', () => ({
Pool: vi.fn().mockImplementation(() => ({ pool: {
connect: vi.fn().mockResolvedValue(mockClient), connect: vi.fn().mockResolvedValue(mockClient),
})), },
})) }))
import { withTenant } from './tenant' import { withTenant } from './tenant'

View File

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