This is not in the documentation. I am wondering how to properly run transactions with pg-pool.
Here is what I have come up with. Suppose there is a function that needs to run an insert and an update in a transaction.
function foo(){
return new Promise((resolve, reject)=>{
var iq = 'insert into foo ("id", "data") values (1, "foo")';
var uq = 'update foo set data = "foo1" where id=1';
pool.connect().then(client => {
var q = client.query('begin transaction')
.then(() => {
return client.query(insert_query);
}).then(qresult => {
return client.query(update_query);
}).then(qresult => {
return client.query('commit');
}).then(qresult => {
client.release();
resolve();
}).catch(err => {
return client.query('rollback');
.then(qresult => {
// rilascia il client
client.release();
reject(err);
}).catch(err=>{
reject(err);
});
});
}).catch(err => {
reject(err);
});
});
}
I am wondering if this is the correct way to handle transactions and all the possible events and errors that could occur while running in order to properly rolling back the transaction and releasing the client to the pool.
If this is ok, maybe the doc can be updated to include this example?
This is not in the documentation. I am wondering how to properly run transactions with pg-pool.
Here is what I have come up with. Suppose there is a function that needs to run an insert and an update in a transaction.
I am wondering if this is the correct way to handle transactions and all the possible events and errors that could occur while running in order to properly rolling back the transaction and releasing the client to the pool.
If this is ok, maybe the doc can be updated to include this example?