0.0.6 • Published 9 years ago

kontainer-di-config v0.0.6

Weekly downloads
1
License
ISC
Repository
github
Last release
9 years ago

kontainer-di-config

Setup kontainer-di through configuration

Installation

$ npm install kontainer-di-config --save

Usage

var di = require('kontainer-di-config')(require('kontainer-di'), diConfig, dirname);

Sample diConfig object (required)

{
  "env": "prod", /* [1] */
  "isDev": false, /* [2] */
  "roles": ["admin", "user"], /* [3] */
  "knex.config": {"client": "pg", ... }, /* [4] */
  
  "config.path": "./config/prod.json", /* [5] */
  "config": "(./config/prod.json)", /* [6] */

  "knex": {"(knex)": ["knex.config"]}, /* [7] */
  "bookshelf": {"(bookshelf)": ["knex"]},

  "user.model": {"(./models/user)": ["bookshelf", "dep2", "dep3", ...]}, /* [8] */
  "user.service": {"(./services/user)": ["user.model"]},
  ...
}
  1. You can register a string directly from the config
  2. Or a boolean...
  3. Or an Array.
  4. And of course an object!
  5. Here, this won't load/require the json file
  6. Here yes! (because it's wrapped between paranthesis, this is the way to require something)
  7. You can even require Node modules, as long as they follow the factory pattern
  8. Etc...

dirname option (required)

All "requirable" relative paths (between paranthesis) given in config, are resolved from the dirname option. Meaning you may need to adjust the dirname option regarding from where you want to declare you dependencies in the config file/object:

var config = {
  "user": {"./src/models/user": ["bookshelf"]}
};
var di = konfigure(kontainer, config, __dirname);

// -- OR --

var config = {
  "user": {"./models/user": ["bookshelf"]}
};
var di = konfigure(kontainer, config, path.resolve(__dirname, 'src'));

You can also browse the source code to get a better idea of this.

Example

var kontainer = require('kontainer-di');
var konfigure = require('kontainer-di-config');
var config    = require('./config/di.json');

var di = konfigure(kontainer, config, __dirname);
var userService;

userService = di.get('user.service');
userService.findAll().then( ... );

// ...

See the example folder for the complete example.

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago