Initial commit: OmniAI backend server
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
const { Pool } = require("pg");
|
||||
require("dotenv").config();
|
||||
|
||||
const pool = new Pool({
|
||||
host: process.env.PG_HOST || "localhost",
|
||||
port: Number(process.env.PG_PORT) || 5432,
|
||||
database: process.env.PG_DATABASE || "omniai",
|
||||
user: process.env.PG_USER || "omniai",
|
||||
password: process.env.PG_PASSWORD || "",
|
||||
max: Number(process.env.PG_POOL_MAX) || 10,
|
||||
idleTimeoutMillis: 30000,
|
||||
connectionTimeoutMillis: 5000,
|
||||
});
|
||||
|
||||
pool.on("error", (err) => {
|
||||
console.error("[db] Unexpected pool error:", err.message);
|
||||
});
|
||||
|
||||
async function withTransaction(fn) {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
const result = await fn(client);
|
||||
await client.query("COMMIT");
|
||||
return result;
|
||||
} catch (err) {
|
||||
await client.query("ROLLBACK").catch(() => {});
|
||||
throw err;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { pool, withTransaction };
|
||||
Reference in New Issue
Block a user