some improvements from claude

This commit is contained in:
MLH
2025-04-06 16:20:42 +02:00
parent 467d27834c
commit 4dfdb17b1e
7 changed files with 288 additions and 26 deletions

29
db.js
View File

@ -12,12 +12,22 @@ const pool = new Pool({
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
// Add connection pool settings for better performance
max: 20, // Maximum number of clients
idleTimeoutMillis: 30000, // How long a client is allowed to remain idle before being closed
connectionTimeoutMillis: 2000, // How long to wait for a connection
// Optional: Add SSL configuration if required by your database provider
// ssl: {
// rejectUnauthorized: false // Adjust as needed
// }
});
// Listen for errors on the pool
pool.on('error', (err) => {
console.error('Unexpected error on idle client', err);
process.exit(-1);
});
// Test the database connection on startup
pool.connect((err, client, release) => {
if (err) {
@ -31,9 +41,22 @@ pool.connect((err, client, release) => {
}
});
// Export a query function to interact with the database
// Export functions to interact with the database
module.exports = {
query: (text, params) => pool.query(text, params),
// You can add more specific database functions here if needed
// Example: getClient: () => pool.connect()
// Add transaction helper
withTransaction: async (callback) => {
const client = await pool.connect();
try {
await client.query('BEGIN');
const result = await callback(client);
await client.query('COMMIT');
return result;
} catch (e) {
await client.query('ROLLBACK');
throw e;
} finally {
client.release();
}
}
};