Skip to content

Einenlum/jic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jic

Ridiculously simple Dependency Injection Container for NodeJS.

Why a DIC?

To improve general code quality thanks to inversion of control, and to avoid using things like proxyquire in your tests.

Why jic?

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.

Requirements

Node version >= 8.

Install

npm add --save @einenlum/jic or yarn add @einenlum/jic

Usage

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;

License

MIT License.