clever-injector

1.0.3 • Public • Published

Cleverstack Injector Module

NPM version GitHub version Dependency Status devDependency Status Code Climate Build Status Coverage NPM downloads

CleverStack Node Seed

NodeJS Dependency Injection for the CleverStack ecosystem, designed to work with or without CleverStack.

Important

If you are using the CleverStack framework you do not need to install or setup the injector, CleverStack comes out of the box with the injector ready to use.

Features

  1. Non-blocking and fully async
  2. Load's resources based upon name for you and injects dependencies
  3. Will never load the same resource more than once, it uses the one singular instance for all dependencies

Install

npm i clever-injector

Setup injector instance

var CleverInjector = require( 'clever-injector' );

// You can add directories so that the injector can try to load the resources by name,
// this example presumes you have a folder called src with a file named Example.js
var injector = CleverInjector( __dirname + '/src', __dirname + '/config );

// You can add instances to the injector like this
injector.instance( 'config', config );
injector.instance( 'models', models );
injector.instance( 'db', db );

// You can get instances like this
var config = injector.getInstance( 'config' );

// You can inject functions and name them like this
injector.inject( 
    function( config, models ) {
        return {}; // Return whatever resource you are defining
    },
    function( Resource ) {
        injector.instance( 'Resource', Resource );
    }
);

Writing files

// src/Example.js
module.exports = function( db ) {
   return {};
};

Use injector

injector.inject( function( Example, models, config ) {
   // this function will be called asynchronously after all required modules are initialized and/or loaded.
});

For more details take a look at provided testsuite. There are all possible use cases.

Package Sidebar

Install

npm i clever-injector

Weekly Downloads

1

Version

1.0.3

License

MIT

Last publish

Collaborators

  • pilsy