Switch to pooled pg connections

This commit is contained in:
Sam Becker 2024-04-30 17:35:07 -05:00
parent 74ec5fd234
commit be486ede20

View File

@ -1,4 +1,9 @@
import { Client, QueryResultRow } from 'pg';
import { Pool, QueryResult, QueryResultRow } from 'pg';
const pool = new Pool({
connectionString: process.env.POSTGRES_URL,
ssl: true,
});
export type Primitive = string | number | boolean | undefined | null;
@ -6,13 +11,15 @@ export const directQuery = async <T extends QueryResultRow = any>(
queryString: string,
values: Primitive[],
) => {
const client = new Client({
connectionString: process.env.POSTGRES_URL,
ssl: true,
});
await client.connect();
const response = await client.query<T>(queryString, values);
await client.end();
const client = await pool.connect();
let response: QueryResult<T>;
try {
response = await client.query<T>(queryString, values);
} catch (error) {
throw error;
} finally {
client.release();
}
return response;
};