beat
Simple dependency injection for node
Enjoy also beat-conf, lean configuration utility for Beat
Example
var Beat = ;var app = 'app'; appvalue'port' processenvPORT || 3000;appvalue'express' ; app; app;
How to use
Install it with NPM:
npm install --save beat
Then require it:
var Beat = ;
API
constructor(alias): starts a new Beat with an alias (defaults to "unnamed")
To produce the instance, Beat
should be called with new
operator.
var myServer = 'server';
value(token, value): defines a value for injection
Register the final value.
myServervalue'port' 80;
factory(token, factoryFn): defines a factory for injection
To produce the instance, factoryFn
will be called once (with instance context) and its result will be used.
The factoryFn
function can use injection annotation.
myServer;
run(fn): runs a code block, with injection
fn
will be called (with instance context).
The fn
function can use injection annotation.
myServer;
get(token): obtains a property
myServervalue'port' 80;var port = myServer;
load(beatInstance): import properties and factories from an Beat instance
Useful to bind different beats
var config = 'config';configvalue'port' 80; myServer;myServer;
or at different files
var config = moduleexports = 'config';configvalue'port' 80;
myServer;myServer;
Annotation
Methods run
and factory
can recieve annotated functions as arguments, that will be used for injection.
The injector looks up tokens based on argument names:
myServer;
You can also use a array:
myServer;
Dependencies
Beat instantiation can declare modules as dependencies.
Therefore, declare them as array at constructor second parameter:
var Beat = ;var db = moduleexports = 'db' 'mongoose'; db;
You can also use objects for aliasing:
var Beat = ;var db = moduleexports = 'db' 'mongoose' conf:'../config.json'; db;
file paths starting with /
will be relative to process cwd.
If the required module provides a Beat object, their properties will be mixed with local ones:
var Beat = ;var routes = moduleexports = 'routes' '/lib/middlewares' '/lib/models' '/lib/app'; routes;
Is also possible to load dependencies after construction, with the same sintax:
var Beat = ;var routes = moduleexports = 'routes'; routes;