36 lines
926 B
TypeScript
36 lines
926 B
TypeScript
import {
|
|
Pool,
|
|
PostgresError,
|
|
} from "https://deno.land/x/postgres@v0.16.1/mod.ts";
|
|
import {
|
|
type QueryArguments,
|
|
type QueryObjectResult,
|
|
} from "https://deno.land/x/postgres@v0.16.1/query/query.ts?s=QueryArguments";
|
|
|
|
export { PostgresError };
|
|
export { type QueryObjectResult };
|
|
|
|
const databaseUrl = Deno.env.get("DATABASE_URL") ||
|
|
"postgresql://danielflanagan:@127.0.0.1:5432/lyricscreen";
|
|
const pool = new Pool(databaseUrl, 3, true);
|
|
|
|
export async function query<T>(
|
|
sql: string,
|
|
...args: QueryArguments[]
|
|
): Promise<QueryObjectResult<T> | null> {
|
|
let result = null;
|
|
try {
|
|
const connection = await pool.connect();
|
|
try {
|
|
result = connection.queryObject<T>(sql, ...args);
|
|
} catch (err) {
|
|
console.error("Error querying database:", err);
|
|
} finally {
|
|
connection.release();
|
|
}
|
|
} catch (err) {
|
|
console.error("Error connecting to database:", err);
|
|
}
|
|
return result;
|
|
}
|