51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
|
|
const { Pool } = require("pg");
|
||
|
|
require("dotenv").config();
|
||
|
|
|
||
|
|
// Parse PostgreSQL URI
|
||
|
|
const connectionString =
|
||
|
|
process.env.POSTGRESQL_URI ||
|
||
|
|
"postgresql://songlyric_user:MySecurePass123@192.168.10.130:5432/church_songlyric";
|
||
|
|
|
||
|
|
const pool = new Pool({
|
||
|
|
connectionString,
|
||
|
|
max: 20,
|
||
|
|
idleTimeoutMillis: 30000,
|
||
|
|
connectionTimeoutMillis: 10000,
|
||
|
|
});
|
||
|
|
|
||
|
|
// Test connection on startup
|
||
|
|
pool
|
||
|
|
.connect()
|
||
|
|
.then((client) => {
|
||
|
|
console.log("✅ Connected to PostgreSQL database");
|
||
|
|
client.release();
|
||
|
|
})
|
||
|
|
.catch((err) => {
|
||
|
|
console.error("❌ Failed to connect to PostgreSQL:", err.message);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Query helper
|
||
|
|
const query = async (text, params) => {
|
||
|
|
const start = Date.now();
|
||
|
|
try {
|
||
|
|
const res = await pool.query(text, params);
|
||
|
|
const duration = Date.now() - start;
|
||
|
|
if (duration > 100) {
|
||
|
|
console.log("Slow query:", {
|
||
|
|
text: text.substring(0, 100),
|
||
|
|
duration,
|
||
|
|
rows: res.rowCount,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
return res;
|
||
|
|
} catch (err) {
|
||
|
|
console.error("Query error:", err.message);
|
||
|
|
throw err;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
pool,
|
||
|
|
query,
|
||
|
|
};
|