diff --git a/.gitignore b/.gitignore index f292003..96b3958 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -.node_modules/ +/node_modules .env .idea/ \ No newline at end of file diff --git a/server.js b/server.js index 7e7a96d..7ea1cfb 100644 --- a/server.js +++ b/server.js @@ -14,16 +14,62 @@ app.use(cors()); app.use(express.json()); // PostgreSQL connection pool -const pool = new Pool({ - host: process.env.DB_HOST, - port: process.env.DB_PORT, - database: process.env.DB_DATABASE, - user: process.env.DB_USER, - password: process.env.DB_PASSWORD, - max: 20, - idleTimeoutMillis: 30000, - connectionTimeoutMillis: 2000, -}); +function handleHeaders(headers) { + + const alias = (headers['db-alias'] || 'default').toUpperCase().replace(/-/g, '_'); + + let pool={ + host: process.env[`DB_${alias}_HOST`], + port: process.env[`DB_${alias}_PORT`], + database: process.env[`DB_${alias}_DATABASE`], + user: process.env[`DB_${alias}_USER`], + password: process.env[`DB_${alias}_PASSWORD`], + max: 20, + idleTimeoutMillis: 30000, + connectionTimeoutMillis: 2000, + }; + + const allUndefined = pool.host=== undefined && pool.port=== undefined && pool.database=== undefined && pool.user=== undefined && pool.password=== undefined; + const someUndefined = pool.host=== '' || pool.port=== '' || pool.database=== '' || pool.user=== '' || pool.password=== ''; + const someDefined = pool.host != undefined || pool.port != undefined || pool.database != undefined || pool.user != undefined || pool.password != undefined; + + if (allUndefined) { + throw new Error('Invalid db alias'); + } + else if(someUndefined && someDefined) { + console.log(pool); + if(pool.host === '') { + throw new Error('Host is not configured for the specified db alias'); + } + if(pool.port === '') { + throw new Error('Port is not configured for the specified db alias'); + } + if(pool.database === '') { + throw new Error('Database is not configured for the specified db alias'); + } + if(pool.user === '') { + throw new Error('User is not configured for the specified db alias'); + } + if(pool.password === '') { + throw new Error('Password is not configured for the specified db alias'); + } + + } + else{ + return new Pool({ + host: process.env[`DB_${alias}_HOST`], + port: process.env[`DB_${alias}_PORT`], + database: process.env[`DB_${alias}_DATABASE`], + user: process.env[`DB_${alias}_USER`], + password: process.env[`DB_${alias}_PASSWORD`], + max: 20, + idleTimeoutMillis: 30000, + connectionTimeoutMillis: 2000, + }); + } + + +} // Health check endpoint app.get('/api/health', (req, res) => { @@ -45,7 +91,9 @@ app.post('/api/postgres/select', async (req, res) => { }); } + let pool; try { + pool = handleHeaders(req.headers); const result = await pool.query(query, bindings); res.json({ @@ -55,10 +103,12 @@ app.post('/api/postgres/select', async (req, res) => { }); } catch (error) { console.error('SELECT Error:', error); - res.status(500).json({ + res.status(400).json({ success: false, error: error.message }); + } finally { + if (pool) await pool.end(); } }); @@ -73,7 +123,9 @@ app.post('/api/postgres/execute', async (req, res) => { }); } + let pool; try { + pool = handleHeaders(req.headers); const result = await pool.query(query, bindings); res.json({ @@ -82,10 +134,12 @@ app.post('/api/postgres/execute', async (req, res) => { }); } catch (error) { console.error('EXECUTE Error:', error); - res.status(500).json({ + res.status(400).json({ success: false, error: error.message }); + } finally { + if (pool) await pool.end(); } }); @@ -100,9 +154,11 @@ app.post('/api/postgres/transaction', async (req, res) => { }); } + let pool; const client = await pool.connect(); try { + pool = handleHeaders(req.headers); await client.query('BEGIN'); const results = []; @@ -124,12 +180,13 @@ app.post('/api/postgres/transaction', async (req, res) => { await client.query('ROLLBACK'); console.error('TRANSACTION Error:', error); - res.status(500).json({ + res.status(400).json({ success: false, error: error.message }); } finally { client.release(); + if (pool) await pool.end(); } }); @@ -144,7 +201,9 @@ app.post('/api/postgres/insert', async (req, res) => { }); } + let pool; try { + pool = handleHeaders(req.headers); const columns = Object.keys(data); const values = Object.values(data); const placeholders = values.map((_, i) => `$${i + 1}`).join(', '); @@ -163,10 +222,12 @@ app.post('/api/postgres/insert', async (req, res) => { }); } catch (error) { console.error('INSERT Error:', error); - res.status(500).json({ + res.status(400).json({ success: false, error: error.message }); + } finally { + if (pool) await pool.end(); } }); @@ -181,7 +242,9 @@ app.post('/api/postgres/update', async (req, res) => { }); } + let pool; try { + pool = handleHeaders(req.headers); const setColumns = Object.keys(data); const setValues = Object.values(data); const whereColumns = Object.keys(where); @@ -209,10 +272,12 @@ app.post('/api/postgres/update', async (req, res) => { }); } catch (error) { console.error('UPDATE Error:', error); - res.status(500).json({ + res.status(400).json({ success: false, error: error.message }); + } finally { + if (pool) await pool.end(); } }); @@ -227,7 +292,9 @@ app.post('/api/postgres/delete', async (req, res) => { }); } + let pool; try { + pool = handleHeaders(req.headers); const whereColumns = Object.keys(where); const whereValues = Object.values(where); @@ -245,10 +312,12 @@ app.post('/api/postgres/delete', async (req, res) => { }); } catch (error) { console.error('DELETE Error:', error); - res.status(500).json({ + res.status(400).json({ success: false, error: error.message }); + } finally { + if (pool) await pool.end(); } }); @@ -261,6 +330,5 @@ app.listen(PORT, () => { // Handle shutdown gracefully process.on('SIGINT', async () => { console.log('\nShutting down gracefully...'); - await pool.end(); process.exit(0); }); \ No newline at end of file