Implementation of Saga Pattern in Node
npm install nodesagajavascript
import { Saga } from "nodesaga";
//Suppose your app is having following services task1, task2, task3 doing different different operations
const task1 = function(a, b, c, callback){
if(true){ //true here if the task executed is successfull.
return callback(null, "Task one executed successfully.");
}else{
return callback(new Error("Task one could not be executed successfully."), null);
}
}
const task2 = function(a, b, c, callback){
if(true){ //true here if the task executed is successfull.
return callback(null, "Task two executed successfully.");
}else{ //fasle here if the task executed not successfull.
return callback(new Error("Task two could not be executed successfully."), null);
}
}
const task3 = function(a, b, c, callback){
if(false){ //true here if the task executed is successfull.
return callback(null, "Task three executed successfully.");
}else{ //fasle here if the task executed not successfull.
return callback(new Error("Task three could not be executed successfully."), null);
}
}
//fallback1 is service if task1 fails, to revert changes
const fallback1 = function(a, b, c){
console.log("Fallback one called");
}
//fallback2 is service if task2 fails, to revert changes
const fallback2 = function(a, b, c){
console.log("Fallback two called");
}
//fallback3 is service if task3 fails, to revert changes
const fallback3 = function(a, b, c){
console.log("Fallback three called");
}
const saga = new Saga();
saga.StartTransaction([
{task : task1, fallback : fallback1, args : {task : ['a', 'b', 'c'], fallback : ['d', 'e', 'f']}},
{task : task2, fallback : fallback2, args : {task : ['g', 'h', 'i'], fallback : ['j', 'k', 'l']}},
{task : task3, fallback : fallback3, args : {task : ['g', 'h', 'i'], fallback : ['j', 'k', 'l']}}
], function(err, done){
if(err){
console.log("Err is : ", err);
}else{
console.log("Message is : ", done);
}
});
`
$3
`javascript
//above is a transaction pipeline
args : {
task : ['a', 'b', 'c'], // 'a', 'b', 'c', are the arguments you want to pass in task function, they can be n.
fallback : ['d', 'e', 'f'] //'d', 'e', 'f', are the arguments you want to pass in the fallback function, they can be n.
}
`
#### task1 is the service function, fallback1 is it's fallback service function
`javascript
const task1 = function(a, b, c, callback){
if(true){
/true here if the task executed is successfull. You have to write your logic instead of this and then return the callback like this. /
return callback(null, "Task one executed successfully.");
}else{
return callback(new Error("Task one could not be executed successfully."), null);
}
}
//fallback1 is service if task1 fails, to revert changes
const fallback1 = function(a, b, c){
console.log("Fallback one called"); //Fallback logic here
}
``