add services/worker/src/db/client.js
This commit is contained in:
parent
76e15b4b76
commit
9e2833ba85
1 changed files with 30 additions and 0 deletions
30
services/worker/src/db/client.js
Normal file
30
services/worker/src/db/client.js
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue