diff --git a/backend/utils/pgConnectToLst.utils.ts b/backend/utils/pgConnectToLst.utils.ts index 7862a09..8ba2e31 100644 --- a/backend/utils/pgConnectToLst.utils.ts +++ b/backend/utils/pgConnectToLst.utils.ts @@ -1,46 +1,41 @@ import pkg from "pg"; -const { Client } = pkg; +const { Pool } = pkg; -const v1client = new Client({ - host: "localhost", - port: 5432, - user: "postgres", - password: "obelix", +const baseConfig = { + host: process.env.DATABASE_HOST ?? "localhost", + port: parseInt(process.env.DATABASE_PORT ?? "5433", 10), + user: process.env.DATABASE_USER, + password: process.env.DATABASE_PASSWORD, +}; + +// Pools (one per DB) +const v1Pool = new Pool({ + ...baseConfig, database: "lst", }); -const v2client = new Client({ - host: "localhost", - port: 5432, - user: "postgres", - password: "obelix", +const v2Pool = new Pool({ + ...baseConfig, database: "lst_db", }); -export const v1QueryRun= async (query:string)=> { +// Query helpers +export const v1QueryRun = async (query: string, params?: any[]) => { try { - await v1client.connect(); - - const res = await v1client.query(query); - - console.log("Rows affected:", res.rowCount); + const res = await v1Pool.query(query, params); + return res; } catch (err) { - console.error("Error running query:", err); - } finally { - await v1client.end(); + console.error("V1 query error:", err); + throw err; } -} +}; -export const v2QueryRun = async (query:string)=> { +export const v2QueryRun = async (query: string, params?: any[]) => { try { - await v2client.connect(); - - const res = await v2client.query(query); - - console.log("Rows affected:", res.rowCount); + const res = await v2Pool.query(query, params); + return res; } catch (err) { - console.error("Error running query:", err); - } finally { - await v2client.end(); + console.error("V2 query error:", err); + throw err; } -} \ No newline at end of file +}; \ No newline at end of file