npm

Need private packages and team management tools?Check out npm Orgs. »

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.

install

npm i clever-injector

Downloadsweekly downloads

2

version

1.0.3

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability