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)

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

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)

(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:

dm . config ( { dependencyTimeout : 2500 } ) ;

Short demo

app.js

function App ( db , config ) { this . db = db ; this . config = config ; } App . $depends = [ ' db ' , ' config ' ] ; dm . class ( ' app ' , App ) ; dm . run ( ' app ' ) . catch ( function ( err ) { console . error ( err . stack ) ; } ) ;

db.js

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 ' ] ; dm . factory ( ' db ' , connectToDb ) ;

config.js

var dm = require ( ' node-dm ' ) ; var config = { mongo : ' mongodb://localhost:27017/mydb ' } ; dm . value ( ' config ' , config ) ;

Api

define your resources:

as simple values of any kind

var config = { this : { is : ' config ' } } dm . provide ( ' config ' , ' value ' , config ) ; dm . value ( ' pi ' , 3 . 14 ) ; var timeIn2Sec = new Promise ( function ( resolve ) { setTimeout ( function ( ) { resolve ( new Date ( ) ) ; } , 2000 ) ; } ) ; dm . value ( ' timeIn2Sec ' , timeIn2Sec ) ;

as classes

function UserCtrl ( config , db ) { this . config = config ; this . db = db ; } UserCtrl . $depends = [ ' config ' , ' db ' ] ; dm . provide ( ' userCtrl ' , ' class ' , UserCtrl , UserCtrl . $depends ) ; class FileParser { static get $depends ( ) { return [ ' config ' , ' db ' ] ; } constructor ( config , db ) { this . config = config ; this . db = db ; } } dm . class ( ' fileParser ' , FileParser ) ;

as factory functions

function createObject ( ) { return { this : { is : ' object ' } } } dm . provide ( ' someObject ' , ' factory ' , createObject ) ; 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 ' ] ; dm . factory ( ' db ' , connectToDb ) ;

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

function Parser ( deps ) { 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 } ; dm . class ( ' parser ' , Parser ) ;

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

dm . resolve ( [ ' db ' , ' config ' , ' pi ' ] ) . then ( function ( result ) { var db = result [ 0 ] , config = result [ 1 ] , pi = result [ 2 ] ; } ) ;

or like this: