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