Initial commit: OmniAI backend server

This commit is contained in:
stringadmin
2026-06-02 13:14:10 +08:00
commit 56955e32f7
73 changed files with 25834 additions and 0 deletions
+34
View File
@@ -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 };