Ridiculously simple Dependency Injection Container for NodeJS.
To improve general code quality thanks to inversion of control, and to avoid using things like proxyquire in your tests.
This is a very simple DIC you can use for small projects, if you don't need much more advanced tools like electrolyte, awilix, or inversifyJS.
No Typescript, no automatic loading. Just a tiny tool to help you build your composition root.
Node version >= 8.
npm add --save @einenlum/jic
or yarn add @einenlum/jic
To register services and dependencies: container.register(arrayOfServices, rootDirectory);
To get a service: container.get(idOfTheService);
Example:
const container = require('@einenlum/jic');
container.register([
{id: 'request', path: 'request'},
{id: 'curryWurst': path: './curryWurst', dependencies: ['sausage', 'ketchup', 'curry']},
{id: 'ketchup', path: './ketchup'},
{id: 'curry', path: './curry', dependencies: ['request']},
{id: 'sausage', path: './sausage'}
], __dirname) // the path that will be used as root for the internal services
// To fetch the service you want (will always return a singleton):
container.get('curryWurst');
container.get('request');
All internal services must be written as functions with dependencies as parameters.
Examples:
// ./curryWurst.js
const curryWurst = function(sausage, ketchup, curry) {
return {
// ...
};
};
module.exports = curryWurst;
// ./ketchup.js
const ketchup = function() {
return {
// ...
};
};
module.exports = ketchup;
MIT License.