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

node-dm

v2.1.1

Published

Asynchronous Dependency Injection Manager for Node.JS

Downloads

33

Readme

Node DM

####Asynchronous Dependency Manager for Node.JS

A simple library to handle dependencies in your app.

  • resources can be provided in any order and time (hence the asynchronousity)
  • instances are created only when they are needed (being depended upon), so you can require() all the things you have (e.g. in some module) and stuff you won't be using will simply remain dormant
  • internally relies on promises (polyfill is used for node.js < 0.12.x, otherwise native ES6)
  • will notify you of missing dependencies
  • will check for circular dependencies

to start:

npm install node-dm
var dm = require('node-dm');

optional config:

/*
 * will reject dependencies that were not resolved after a period of time
 * (2.5s is the default value, set to `false` to disable)
 */
dm.config({ dependencyTimeout: 2500 });

Short demo

app.js

function App(db, config){
	/* get your dependencies */
	this.db = db;
	this.config = config;
}

/* define what dependencies your resource needs */
App.$depends = ['db', 'config'];

/* provide your resource as a class */
dm.class('app', App);

/* 
 * you can also specify dependencies here directly without $depends: 
 * dm.class('app', App, ['db', 'config']); 
 */

/* 
 * full provide method looks like this ($depends cannot be used here):
 * dm.provide('app', 'class', App, ['db', 'config']); 
 */

/* 
 * bootstrap dependency tree - calling dm.run() is needed to instatiate
 * the root dependency, which nobody depends upon 
 */
dm.run('app')
  .catch(function(err){
    /* 
     * because internally node dm uses promises, all the rejections/exceptions
     * eventually can be caught on the topmost node of dependency tree
     */
    console.error(err.stack);
  });

db.js

function connectToDb(config){
    /* 
     * we return a promise, so whoever depends on 'db', will receive it
     * once the connection has beed established
     */
    return new Promise(function(resolve, reject){
        MongoClient.connect(config.mongo, function(err, db){
            if(err){
                reject(err);
            }
            else{
                resolve(db);
            }
        });
    });
}

connectToDb.$depends = ['config'];

/* 
 * provide your resource as a factory function, that returns what you need
 * (or the promise of that)
 */
dm.factory('db', connectToDb);

config.js

var dm = require('node-dm');

var config = {
	mongo: 'mongodb://localhost:27017/mydb'
};

/* 
 * this resource is a simple value and does not need instantiation; 
 * can also be a promise 
 */
dm.value('config', config);

Api

define your resources:

  • as simple values of any kind
var config = {
	this: {
	    is: 'config'
	}
}

/* full method */
dm.provide('config', 'value', config);
/* shorthand method: */
dm.value('pi', 3.14);

/* promises will also work */
var timeIn2Sec = new Promise(function(resolve){
	setTimeout(function(){
		resolve(new Date());
	}, 2000);
});

/* 
 * whoever depends on this, will receive it 
 * once the promise will be resolved 
 */
dm.value('timeIn2Sec', timeIn2Sec);
  • as classes
function UserCtrl(config, db){
	/* you will receive dependencies you requested */
	this.config = config;
	this.db = db;
}

UserCtrl.$depends = ['config', 'db'];

/* full method */
dm.provide('userCtrl', 'class', UserCtrl, UserCtrl.$depends);

/* wokrs with ES6 too */
class FileParser{
	static get $depends(){ return ['config', 'db']; }

	constructor(config, db){
		this.config = config;
		this.db = db;
	}
}

/* shorthand method */
dm.class('fileParser', FileParser);
  • as factory functions
function createObject(){
	return {
		this: { is: 'object' }
	}
}

/* full method */
dm.provide('someObject', 'factory', createObject);

/* a function can also return a promise */
function connectToDb(config){
	return new Promise(function(resolve, reject){
		MongoClient.connect(config.mongo, function(err, db){
			if(err){
				reject(err);
			}
			else{
				resolve(db);
			}
		});
	});
}

connectToDb.$depends = ['config'];

/* shorthand method: */
dm.factory('db', connectToDb);

alternatively, dependencies of a resource can be requested as an object:

function Parser(deps){
	/* you will receive your dependencies as a map */
	this.deps = deps;
	this.is = 'awesome parser';
}

Parser.prototype.parse = function(){
	if(this.deps.config.canIDoStuff){
		this.deps.db.doSomething();
		this.deps.router.makeEverythingGood();
	}
};

Parser.$depends = { 
	config: true,
	db: true,
	router: true
};

/*
 * or you can use shorthand like this: 
 * Parser.$depends = dm.object('config', 'db', 'router');
 */

dm.class('parser', Parser);

you can also just request dependencies anywhere in the code like this:

dm.resolve(['db', 'config', 'pi'])
	.then(function(result){
		/* result is an array of dependencies */
		var db = result[0],
			config = result[1],
			pi = result[2];
	});

or like this:

dm.resolve({db: true, config: true, pi: true})
	.then(function(result){
		/* result is a map of dependencies */
		var db = result.db,
			config = result.config,
			pi = result.pi;
	});