0.0.6 • Published 9 years ago
kontainer-di-config v0.0.6
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"]},
...
}
- You can register a
string
directly from the config - Or a
boolean
... - Or an
Array
. - And of course an
object
! - Here, this won't load/require the
json
file - Here yes! (because it's wrapped between paranthesis, this is the way to
require
something) - You can even
require
Node modules, as long as they follow the factory pattern - 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.