Commit/rollback A Knex Transaction Using Async/await
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
Solution 1:
You might be able to achieve this with something similar to this
functioncreateTransaction() {
returnnewPromise((resolve) => {
return knex.transaction(resolve);
});
}
asyncfunction() {
const trx = awaitcreateTransaction();
...
trx.commit();
}
Solution 2:
Building off of this Knex Transaction with Promises, it looks like it should be along these lines:
// assume `db` is a knex instanceinsert: async (function(db, data) {
const trx = db.transaction();
try {
const idUser = await(user.insertData(trx, data));
trx.commit();
} catch (error) {
trx.rollback();
throw error;
}
return {
idUser: idUser
}
})
Post a Comment for "Commit/rollback A Knex Transaction Using Async/await"