Add remaining templates elements

This commit is contained in:
2025-02-24 00:02:27 -05:00
parent f628774267
commit 441a2c74ad
10 changed files with 1446 additions and 331 deletions

View File

@@ -1,41 +1,47 @@
const { Pool } = require('pg');
const { Pool, Client } = require('pg');
const { Client: SSHClient } = require('ssh2');
let pool;
async function initPool(config) {
function initPool(config) {
// Log config without sensitive data
const safeConfig = {
host: config.host,
user: config.user,
database: config.database,
port: config.port,
max: config.max,
idleTimeoutMillis: config.idleTimeoutMillis,
connectionTimeoutMillis: config.connectionTimeoutMillis,
ssl: config.ssl,
password: config.password ? '[password set]' : '[no password]'
host: config.host || process.env.DB_HOST,
user: config.user || process.env.DB_USER,
database: config.database || process.env.DB_NAME,
port: config.port || process.env.DB_PORT || 5432,
max: config.max || 10,
idleTimeoutMillis: config.idleTimeoutMillis || 30000,
connectionTimeoutMillis: config.connectionTimeoutMillis || 2000,
ssl: config.ssl || false,
password: (config.password || process.env.DB_PASSWORD) ? '[password set]' : '[no password]'
};
console.log('[Database] Initializing pool with config:', safeConfig);
try {
// Create the pool
pool = new Pool(config);
// Create the pool with the configuration
pool = new Pool({
host: config.host || process.env.DB_HOST,
user: config.user || process.env.DB_USER,
password: config.password || process.env.DB_PASSWORD,
database: config.database || process.env.DB_NAME,
port: config.port || process.env.DB_PORT || 5432,
max: config.max || 10,
idleTimeoutMillis: config.idleTimeoutMillis || 30000,
connectionTimeoutMillis: config.connectionTimeoutMillis || 2000,
ssl: config.ssl || false
});
// Test the connection
const client = await pool.connect();
try {
await client.query('SELECT NOW()');
console.log('[Database] Pool connection test successful');
} finally {
// Test the pool connection
return pool.connect()
.then(client => {
console.log('[Database] Pool connection successful');
client.release();
}
return pool;
} catch (err) {
console.error('[Database] Connection failed:', err);
throw err;
}
return pool;
})
.catch(err => {
console.error('[Database] Connection failed:', err);
throw err;
});
}
async function getConnection() {
@@ -45,24 +51,8 @@ async function getConnection() {
return pool.connect();
}
// Helper function to execute a query with error handling
async function query(text, params = []) {
if (!pool) {
throw new Error('Database pool not initialized');
}
try {
const result = await pool.query(text, params);
return result;
} catch (err) {
console.error('[Database] Query error:', err);
throw err;
}
}
module.exports = {
initPool,
getConnection,
getPool: () => pool,
query
getPool: () => pool
};