Copyright (C) 2022 Toranoana Lab Inc. All Rights Reserved.
import { QueryObjectResult } from "postgres-query";
import { Pool } from "postgres";
const pool = new Pool(
Deno.env.get("DATABASE_URL") ||
"postgresql://postgres:postgres@localhost:5432/mydb?schema=public",
parseInt(Deno.env.get("DATABASE_POOL_SIZE") || "20"),
);
export async function dbInit() {
return await runQuery(`SELECT 1;`);
}
export async function runQuery(
query: string,
args?: unknown[],
): Promise | null> {
let result = null;
let client = null;
try {
client = await pool.connect();
result = await client.queryObject({
camelcase: true,
text: query,
args,
});
} finally {
client?.release();
}
return result;
}
dbInitは起動確認用
→poolの初期化もこちらの方が良い可能性あり
(現状だと環境変数の読み込みが先行している必要がある)