I'm test driving the ES7 async/await proposal using this module to emulate it. I'm trying to make knex.js transactions play well with them, as a starting point.
Example code:
async function transaction() {
return new Promise(function(resolve, reject){
knex.transaction(function(err, result){
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
// Start transaction from this call
insert: async (function(db, data) {
const trx = await(transaction());
const idUser = await(user.insertData(trx, data));
return {
idCidUserstomer: idUser
}
})
How can I commit()
or rollback()
if a transaction succeeds or fails?
Building off of this Knex Transaction with Promises, it looks like it should be along these lines:
You might be able to achieve this with something similar to this
You can try this: