Skip to main content

Transaction

fawn

  • Fawn uses 2 phase commit to mimic a transaction
  • Provides the ability to carry out edits on a mongoDB database as a series of steps
$ npm install fawn
var Fawn = require("fawn");
var mongoose = require("mongoose");
mongoose.connect("mongodb://127.0.0.1:27017/testDB");

Fawn.init(mongoose);

//Create a transaction task
var task = Fawn.Task();
task.update("Accounts", {firstName: "John", lastName: "Smith"}, {$inc: {balance: -20}})
.update("Accounts", {firstName: "Henry", lastName: "Miller"}, {$inc: {balance: 20}})
.run()
.then(function(results){
var firstUpdateResult = results[0];
var secondUpdateResult = results[1];
res.status(200).send('Operation Successful');
})
.catch(function(err){
// if there is any error,roll back entirely
// log the error which caused the failure
console.log(err);
res.status(500).send('Operation Failed', err);
});