30 lines
491 B
JavaScript
30 lines
491 B
JavaScript
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;
|
|
}
|
|
};
|