Files
whatsappNucleo/server/utils/database.ts
josedario87 faedec47d7
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 6m46s
feat: WhatsApp Nucleo con Nuxt 4 + Baileys v7
Reemplazo completo de Evolution API por implementación directa con Baileys.

Características:
- Dashboard completo con Nuxt UI v4
- Soporte para múltiples instancias de WhatsApp
- Conexión via QR code o pairing code
- Persistencia de mensajes en PostgreSQL
- API REST para integraciones externas
- Webhooks con firma HMAC
- SSE para actualizaciones en tiempo real
- Autenticación con Authentik
2025-12-02 17:54:31 -06:00

53 lines
1.1 KiB
TypeScript

import pg from 'pg'
const { Pool } = pg
let pool: pg.Pool | null = null
export function getPool(): pg.Pool {
if (!pool) {
const config = useRuntimeConfig()
pool = new Pool({
connectionString: config.databaseUrl,
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000
})
pool.on('error', (err) => {
console.error('Unexpected error on idle client', err)
})
}
return pool
}
export async function query<T = any>(
text: string,
params?: any[]
): Promise<pg.QueryResult<T>> {
const client = await getPool().connect()
try {
return await client.query<T>(text, params)
} finally {
client.release()
}
}
export async function transaction<T>(
callback: (client: pg.PoolClient) => Promise<T>
): Promise<T> {
const client = await getPool().connect()
try {
await client.query('BEGIN')
const result = await callback(client)
await client.query('COMMIT')
return result
} catch (error) {
await client.query('ROLLBACK')
throw error
} finally {
client.release()
}
}