I am getting the correct output, and indeed, these two operations are being treated as a single transactional unit; where if one fails, both fail.
In this code example: i am doing a transaction of
(1) insert
(2) update
The way I approach it is to nest my db operations inside the .then.
My question is if this code is correct by accident? i am new to promises and knex.
knex.transaction(function(t) {
knex('foo')
.transacting(t)
.insert({id:"asdfk", username:"barry", email:"[email protected]"})
.then(function() {
knex('foo')
.where('username','=','bob')
.update({email:"[email protected]"})
.then(t.commit, t.rollback)
})
})
.then(function() {
// it worked
},
function() {
// it failed
});
This works, but I feel like I am doing something wrong still. Looking for comments.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…