npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodesaga

v0.1.2

Published

Implementation of Saga Pattern in Node

Downloads

23

Readme

nodesaga

Saga implementation in Node.JS. It's done to prevent overhead for long transaction processes and call functions of constious microservices related to it depending on each other in a queue execution and run fallbacks if one of them is unsuccessful.

You can define your custom transaction and their respective fallbacks. Please read below carefully. For better understanding you can reach me out. Thanks!

Saga design

HOW TO INSTALL

npm install nodesaga --save

HOW TO USE


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);
	}
});

{task : task1, fallback : fallback1, args : {task : ['a', 'b', 'c'], fallback : ['d', 'e', 'f']}}


//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

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
}

While writing the tasks last argument must be a callback with err as first argument and message or data as second argument as per Node.JS convention, in fallback functions I have not implemented callback because it's not needed.

Here we can give a transaction in form of pipelines. All will run one after another :)
Here in the above figure the transaction has tasks and their specific fallbacks.

It's all maintained by a central system. If all goes "yes" the whole transaction is successfull. If anyone of the inbetween task or pipeline fails all the previous fallbacks are called and data get's reverted as if nothing happened.