depj

1.0.3 • Public • Published

depj

very simple dependency injection

Inspired heavily by depject. Accepts dependencies that are any type of javascript object.

Usage

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

API

var combine = require('depj')

var api = combine(deps)

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.

Install

With npm installed, run

$ npm install depj

License

ISC

Readme

Keywords

none

Package Sidebar

Install

npm i depj

Weekly Downloads

4

Version

1.0.3

License

ISC

Last publish

Collaborators

  • noffle