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 = ;
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;
Short demo
app.js
{ /* get your dependencies */ thisdb = db; thisconfig = config;} /* define what dependencies your resource needs */App$depends = 'db' 'config'; /* provide your resource as a class */dm; /* * 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 ;
db.js
{ /* * we return a promise, so whoever depends on 'db', will receive it * once the connection has beed established */ return { MongoClient; };} connectToDb$depends = 'config'; /* * provide your resource as a factory function, that returns what you need * (or the promise of that) */dm;
config.js
var dm = ; var config = mongo: 'mongodb://localhost:27017/mydb'; /* * this resource is a simple value and does not need instantiation; * can also be a promise */dmvalue'config' config;
Api
define your resources:
- as simple values of any kind
var config = this: is: 'config' /* full method */dm;/* shorthand method: */dmvalue'pi' 314; /* promises will also work */var timeIn2Sec = { ;}; /* * whoever depends on this, will receive it * once the promise will be resolved */dmvalue'timeIn2Sec' timeIn2Sec;
- as classes
{ /* you will receive dependencies you requested */ thisconfig = config; thisdb = db;} UserCtrl$depends = 'config' 'db'; /* full method */dm; /* wokrs with ES6 too */ static get return 'config' 'db'; { thisconfig = config; thisdb = db; } /* shorthand method */dm;
- as factory functions
{ return this: is: 'object' } /* full method */dm; /* a function can also return a promise */{ return { MongoClient; };} connectToDb$depends = 'config'; /* shorthand method: */dm;
alternatively, dependencies of a resource can be requested as an object:
{ /* you will receive your dependencies as a map */ thisdeps = deps; thisis = 'awesome parser';} Parserprototype{ ifthisdepsconfigcanIDoStuff thisdepsdb; thisdepsrouter; }; Parser$depends = config: true db: true router: true; /* * or you can use shorthand like this: * Parser.$depends = dm.object('config', 'db', 'router'); */ dm;
you can also just request dependencies anywhere in the code like this:
dm ;
or like this:
dm ;