Skip to content

hackergrrl/depj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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

About

very simple dependency injection

Resources

Stars

Watchers

Forks

Packages

No packages published