very simple dependency injection
Inspired heavily by depject. Accepts dependencies that are any type of javascript object.
var combine = require('depj')
var hyperdb = require('hyperdb')
var ram = require('random-access-memory')
var memdb = require('memdb')
var hyper = {
gives: 'hyperdb',
needs: [],
create: function (api) { return hyperdb(ram, { valueEncoding: 'json' }) }
}
var level = {
gives: 'leveldb',
needs: [],
create: function (api) { return memdb() }
}
var osm = {
gives: 'osm',
needs: [ 'hyperdb', 'leveldb' ],
create: function (api) {
return {
put: function (key, value, cb) { api.hyperdb.put(key, value, cb) },
// ...
}
}
}
var api = combine([hyper, osm, level])
console.log('api', Object.keys(api))
api.osm.put('foo', 'bar', function (err) { console.log(err || 'done') })
outputs
api [ 'hyperdb', 'leveldb', 'osm' ]
done
var combine = require('depj')
Creates the object api
with properties for all of the dependencies in the
tree. deps
is an array with objects of the form
{
gives: 'name',
needs: {
need1: true,
need2: true
},
create: function (api) { return ... }
}
create
can return an object, a function -- anything you'd like.
With npm installed, run
$ npm install depj
ISC