enable ssl for pg connection
This commit is contained in:
@@ -15,7 +15,6 @@ app.use(express.json({ limit: '10mb' }));
|
||||
|
||||
// PostgreSQL connection pool
|
||||
function handleHeaders(headers) {
|
||||
|
||||
const alias = (headers['db-alias'] || 'default').toUpperCase().replace(/-/g, '_');
|
||||
|
||||
let pool = {
|
||||
@@ -29,14 +28,14 @@ function handleHeaders(headers) {
|
||||
connectionTimeoutMillis: 2000,
|
||||
};
|
||||
|
||||
const allUndefined = pool.host=== undefined && pool.port=== undefined && pool.database=== undefined && pool.user=== undefined && pool.password=== undefined;
|
||||
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) {
|
||||
} else if (someUndefined && someDefined) {
|
||||
console.log(pool);
|
||||
if (pool.host === '') {
|
||||
throw new Error('Host is not configured for the specified db alias');
|
||||
@@ -53,9 +52,7 @@ function handleHeaders(headers) {
|
||||
if (pool.password === '') {
|
||||
throw new Error('Password is not configured for the specified db alias');
|
||||
}
|
||||
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
return new Pool({
|
||||
host: process.env[`DB_${alias}_HOST`],
|
||||
port: process.env[`DB_${alias}_PORT`],
|
||||
@@ -65,10 +62,9 @@ function handleHeaders(headers) {
|
||||
max: 20,
|
||||
idleTimeoutMillis: 30000,
|
||||
connectionTimeoutMillis: 2000,
|
||||
ssl: { rejectUnauthorized: false },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Health check endpoint
|
||||
@@ -76,7 +72,7 @@ app.get('/api/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
timestamp: new Date().toISOString(),
|
||||
node_version: process.version
|
||||
node_version: process.version,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -87,7 +83,7 @@ app.post('/api/postgres/select', async (req, res) => {
|
||||
if (!query) {
|
||||
return res.status(422).json({
|
||||
success: false,
|
||||
error: 'Query is required'
|
||||
error: 'Query is required',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -99,13 +95,13 @@ app.post('/api/postgres/select', async (req, res) => {
|
||||
res.json({
|
||||
success: true,
|
||||
data: result.rows,
|
||||
count: result.rowCount
|
||||
count: result.rowCount,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('SELECT Error:', error);
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
error: error.message,
|
||||
});
|
||||
} finally {
|
||||
if (pool) await pool.end();
|
||||
@@ -119,7 +115,7 @@ app.post('/api/postgres/execute', async (req, res) => {
|
||||
if (!query) {
|
||||
return res.status(422).json({
|
||||
success: false,
|
||||
error: 'Query is required'
|
||||
error: 'Query is required',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -130,13 +126,13 @@ app.post('/api/postgres/execute', async (req, res) => {
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
affected_rows: result.rowCount
|
||||
affected_rows: result.rowCount,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('EXECUTE Error:', error);
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
error: error.message,
|
||||
});
|
||||
} finally {
|
||||
if (pool) await pool.end();
|
||||
@@ -150,7 +146,7 @@ app.post('/api/postgres/transaction', async (req, res) => {
|
||||
if (!queries || !Array.isArray(queries)) {
|
||||
return res.status(422).json({
|
||||
success: false,
|
||||
error: 'Queries array is required'
|
||||
error: 'Queries array is required',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -167,7 +163,7 @@ app.post('/api/postgres/transaction', async (req, res) => {
|
||||
const result = await client.query(item.query, item.bindings || []);
|
||||
results.push({
|
||||
rowCount: result.rowCount,
|
||||
success: true
|
||||
success: true,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -175,14 +171,14 @@ app.post('/api/postgres/transaction', async (req, res) => {
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
results: results
|
||||
results: results,
|
||||
});
|
||||
} catch (error) {
|
||||
if (client) await client.query('ROLLBACK');
|
||||
console.error('TRANSACTION Error:', error);
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
error: error.message,
|
||||
});
|
||||
} finally {
|
||||
if (client) client.release();
|
||||
@@ -197,7 +193,7 @@ app.post('/api/postgres/insert', async (req, res) => {
|
||||
if (!table || !data) {
|
||||
return res.status(422).json({
|
||||
success: false,
|
||||
error: 'Table and data are required'
|
||||
error: 'Table and data are required',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -209,7 +205,7 @@ app.post('/api/postgres/insert', async (req, res) => {
|
||||
const placeholders = values.map((_, i) => `$${i + 1}`).join(', ');
|
||||
|
||||
const query = `
|
||||
INSERT INTO "${table}" (${columns.map(c => `"${c}"`).join(', ')})
|
||||
INSERT INTO "${table}" (${columns.map((c) => `"${c}"`).join(', ')})
|
||||
VALUES (${placeholders})
|
||||
RETURNING *
|
||||
`;
|
||||
@@ -218,13 +214,13 @@ app.post('/api/postgres/insert', async (req, res) => {
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: result.rows[0]
|
||||
data: result.rows[0],
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('INSERT Error:', error);
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
error: error.message,
|
||||
});
|
||||
} finally {
|
||||
if (pool) await pool.end();
|
||||
@@ -238,7 +234,7 @@ app.post('/api/postgres/update', async (req, res) => {
|
||||
if (!table || !data || !where) {
|
||||
return res.status(422).json({
|
||||
success: false,
|
||||
error: 'Table, data and where are required'
|
||||
error: 'Table, data and where are required',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -250,13 +246,9 @@ app.post('/api/postgres/update', async (req, res) => {
|
||||
const whereColumns = Object.keys(where);
|
||||
const whereValues = Object.values(where);
|
||||
|
||||
const setClause = setColumns
|
||||
.map((col, i) => `"${col}" = $${i + 1}`)
|
||||
.join(', ');
|
||||
const setClause = setColumns.map((col, i) => `"${col}" = $${i + 1}`).join(', ');
|
||||
|
||||
const whereClause = whereColumns
|
||||
.map((col, i) => `"${col}" = $${setValues.length + i + 1}`)
|
||||
.join(' AND ');
|
||||
const whereClause = whereColumns.map((col, i) => `"${col}" = $${setValues.length + i + 1}`).join(' AND ');
|
||||
|
||||
const query = `
|
||||
UPDATE "${table}"
|
||||
@@ -268,13 +260,13 @@ app.post('/api/postgres/update', async (req, res) => {
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
affected_rows: result.rowCount
|
||||
affected_rows: result.rowCount,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('UPDATE Error:', error);
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
error: error.message,
|
||||
});
|
||||
} finally {
|
||||
if (pool) await pool.end();
|
||||
@@ -288,7 +280,7 @@ app.post('/api/postgres/delete', async (req, res) => {
|
||||
if (!table || !where) {
|
||||
return res.status(422).json({
|
||||
success: false,
|
||||
error: 'Table and where are required'
|
||||
error: 'Table and where are required',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -298,9 +290,7 @@ app.post('/api/postgres/delete', async (req, res) => {
|
||||
const whereColumns = Object.keys(where);
|
||||
const whereValues = Object.values(where);
|
||||
|
||||
const whereClause = whereColumns
|
||||
.map((col, i) => `"${col}" = $${i + 1}`)
|
||||
.join(' AND ');
|
||||
const whereClause = whereColumns.map((col, i) => `"${col}" = $${i + 1}`).join(' AND ');
|
||||
|
||||
const query = `DELETE FROM "${table}" WHERE ${whereClause}`;
|
||||
|
||||
@@ -308,13 +298,13 @@ app.post('/api/postgres/delete', async (req, res) => {
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
affected_rows: result.rowCount
|
||||
affected_rows: result.rowCount,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('DELETE Error:', error);
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
error: error.message,
|
||||
});
|
||||
} finally {
|
||||
if (pool) await pool.end();
|
||||
|
||||
Reference in New Issue
Block a user