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:
@@ -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),
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user