diff --git a/services/worker/src/db/client.js b/services/worker/src/db/client.js new file mode 100644 index 0000000..0a254e4 --- /dev/null +++ b/services/worker/src/db/client.js @@ -0,0 +1,30 @@ +import pg from 'pg'; + +const { Pool } = pg; + +let pool; + +export const getPool = () => { + if (!pool) { + pool = new Pool({ + connectionString: process.env.DATABASE_URL, + }); + } + return pool; +}; + +export const query = async (text, params) => { + const client = await getPool().connect(); + try { + return await client.query(text, params); + } finally { + client.release(); + } +}; + +export const closePool = async () => { + if (pool) { + await pool.end(); + pool = null; + } +};