I'm building a simple webapp using NodeJS/Postgres that needs to make 3 insertions on database.
To control the chain of statements I'm using pg-transaction.
My problem is that I have to always run the 2 first INSERTS, but I have a condition to run the 3rd one.
Maybe my code could be build in a better manner (suggestions are welcome).
Here's a pseudo code:
function(req, res) {
var tx = new Transaction(client);
tx.on('error', die);
tx.begin();
tx.query('INSERT_1 VALUES(...) RETURNING id', paramValues, function(err, result) {
if (err) {
tx.rollback();
res.send("Something was wrong!");
return;
}
var paramValues2 = result.rows[0].id;
tx.query('INSERT_2 VALUES(...)', paramValues2, function(err2, result2) {
if (err) {
tx.rollback();
res.send("Something was wrong!");
return;
}
// HERE'S THE PROBLEM (I don't want to run it always this last statement)
// If I don't run it, I will miss tx.commit()
if (req.body.value != null) {
tx.query('INSERT_3 VALUES(...)', paramValues3, function(err3, result3) {
if (err) {
tx.rollback();
res.send("Something was wrong!");
return;
}
tx.commit();
res.send("Everything fine!");
});
}
});
});
}
It seems so ugly to repeat three times the same if (err) {} after each query.
Trying to check some options I found Sequelize, but couldn't see a way to solve this problem with it.
Any suggestions are welcome!
Thanks!
Manual transaction management is a treacherous path, try to steer away from that! ;)
Here's how to do it properly, with the help of pg-promise:
Or, if you prefer ES6 generators: