NodeJS Plugin <fl-node-di>

NodeJS Plugin <fl-node-di>

Published on: 9/14/2018

This package has been renamed to ts-nest.
ts-nest build badge

Installation

This package requires a TypeScript setup for NodeJS since decorators are a TypeScript feature. Add the package located at npmjs to your project
npm i --save fl-node-di # or yarn add fl-node-di
Listing: Installation using npm or yarn

Usage

If everything went properly, import the decorators with
import { FlModule, Component, Injectable, Inject } from 'fl-node-di'
Listing: Importing the parts in your application
It provides a hierarchical NodeJS dependency injection module on top of inversify for using backend DI in a way similar to Angulars DI, but with some significant differences I will explain in the following sections. E.g. you can use the following snippet throughout your complete application
@FlModule({ imports: [ AuthModule, Http2Module ], declarations: [ AppComponent ], providers: [ AuthService ], exports: [ ] }) export class AppModule {}
Listing: Creating modules
This for example will import an AuthModule and an Http2Module which are decorated with the @FlModule() decorator by themselfes. The other two items are decorated differently. The first one is the AppComponent
@Component() export class AppComponent {}
Listing: Component decorator instatiates classes directly
which treats the decorator as an inversify @injectable and directly creates an instance when the parent module gets instanciated. The other one is the @Injectable() decorator
@Injectable() export class AuthService {}
Listing: @Injectable decorator instatiates classes when used with @Inject
which returns an inversify @injectable decorator, but does not directly create an instance but instanciates itself when an @Inject(serviceIdentifier) is used as a constructor parameter in the same or a childs containers class.
The DI system is hierarchical.

Cheatsheet

Creates a container class member object where the declared and provided classes are stored and imports other @FlModules as children. This modules declarations and providers are visible to the childs.
Parametersconfig: { imports?: any[], declarations?: any[], providers?: any[], exports?: any[] }
ReturnsReturns a custom decorator where a container object is created in the classes constructor
The four configuration parameters are described in detail in the following table.
Input parameterDescription
importsCreates an instance of the imported FlModule(), reads the exports parameter of the instantiated object and stores the exports in its own container. The instance is handled as a child of this module, so Inject()s will work in the child even if the child does not contain the instance itself, but its parent.
declarationsBinds @Component() decorated classes to the container and after creating all imports it directly creates an instance.
providersBinds @Injectable() decorated classes to the container
exportsBinds @Injectable() or @Component() decorated classes to the parents container
Table: FlModule input parameters in detail

Dependencies

inversify reflect-metadata

Like that library?

twitter github stackoverflow
If you like that library, please consider to follow me on twitter or give it a star on github. Thanks and cheers!