扫一扫分享
Fawn提供了在mongoDB数据库上执行编辑的能力,作为一系列步骤。如果在任何步骤上发生错误,则数据库将返回其初始状态(事务开始之前的状态)。它基于MongoDB文档中描述的两阶段提交系统。
npm install fawn
var task = Fawn.Task();
//assuming "Accounts" is the Accounts collection
task.update("Accounts", {firstName: "John", lastName: "Smith"}, {$inc: {balance: -20}})
.update("Accounts", {firstName: "Broke", lastName: "Ass"}, {$inc: {balance: 20}})
.run()
.then(function(results){
// task is complete
// result from first operation
var firstUpdateResult = results[0];
// result from second operation
var secondUpdateResult = results[1];
})
.catch(function(err){
// Everything has been rolled back.
// log the error which caused the failure
console.log(err);
});
手机预览