inversify-config-injection
npm install inversify-config-injectionSee the node-config documentation for how the configuration files need to be named and various other options like loading them from a yaml file instead of a json.
Assuming this is your config/default.json
``json`
{
"app": {
"db": {
"host": "localhost",
"port": 1234,
"seeds": ["8.8.8.8","8.8.4.4"]
}
},
"other": {
"foo":"bar"
}
}
you can annotate your classes like this
`typescript`
@injectable(
class DefaultDatabase implements Database{
public constructor(
@inject("app.db.host") public host: string,
@inject("app.db.port") public port: number
){};
}
When you initialise your container also import the default binder module and load it into the container:
`typescript
import {defaultEagerBinderModule} from 'inversify-config-injection';
const container = new Container();
container.load(defaultBinderModule);
container.bind
const db = container.get
expect(db.host).to.equal("localhost");
`
Various configuration options for the binder are described below. For this you should instantiate an EagerBinder instead of using defaultEagerBinderModule
`typescript`
import {EagerBinder} from 'inversify-config-injection';
const container = new Container();
const configBinder = new EagerBinder({
log: true,
root: 'app',
prefix: 'cfg',
objects: true
});
container.load(configBinder.module());
Use the root configuration parameter of the eager binder. This will only load children of this particular path. For our above example:
`typescript`
new EagerBinder({
root: 'app'
}); app
only loads the breanch of the configuration. The keys necessary for injecting are also shortened.
`typescript`
@inject("db.host") public host: string,
@inject("db.port") public port: number
In order to avoid collisions you can add a prefix to the binding keys. For our above example:
`typescript`
new EagerBinder({
root: 'app',
prefix: 'cfg'
});
This makes correct biding:
`typescript`
@inject("cfg.db.host") public host: string,
@inject("cfg.db.port") public port: number
Note that there is no cfg key in the configuration json.
In addition to binding every leaf entry of the configuration, you can also bind the intermediary object by turning on objects in the EagerBinder settings.
`typescript`
new EagerBinder({
root: 'app',
objects: true
});
This will bind to constants db.host, db.port, db.seeds but also db as the constant object`js`
{
host: "localhost",
port: 1234,
seeds: ["8.8.8.8","8.8.4.4"]
}
For debugging purposes, you can turn on binding logs
`typescript`
new EagerBinder({
log: true
});
This allows you to get an array of logs with binder.getBindingLog()
`typescript`
console.log( binder.getBindingLog().join("\n") );
```
Binding 'cfg.db.host' to string 'localhost'
Binding 'cfg.db.port' to number '1234'
Binding 'cfg.db.seeds' to string[] '8.8.8.8,8.8.4.4'