Skip to content
/ ts-di Public

Hierarchical NodeJS dependency injection inspired by Angular

License

Notifications You must be signed in to change notification settings

ngfelixl/ts-di

Repository files navigation

Hierarchical NodeJS dependency injection inspired by Angular

Super lightweight (2kb) library for creating dependency trees in NodeJS applications inpired by Angular. It is build on top of inversify. See this library in action with this stackblitz demo.

Build Status Coverage Status npm version code style: prettier Gitter chat

Installation

Add the package to your project

npm i --save ts-nest
# or
yarn add ts-nest

Import the decorators with

import { NestModule, Component, Injectable, Inject } from 'ts-nest'

Usage

Create a NestModule as application entrypoint and import all the wanted child modules, which are NestModules themselves.

@NestModule({
  imports: [ AuthModule, HttpModule ],
  declarations: [ AppComponent ],
  providers: [ AuthService ],
  exports: [ ]
})
export class AppModule {}

Bootstrap your application by simply contructing it

const app = new AppModule();

There are two other types of objects in this dependency tree, Components and Injectables. Components can be created like follows.

@Component()
export class AppComponent {
  constructor (@Inject(AuthService) authService: AuthService) {}
}

As you may have recognized, the constructor receives an Injected parameter. This parameter can be provided in your NestModule, like it is done in the first listing. The AuthService is decorated by an @Injectable() decorator

@Injectable()
export class AuthService {}

When it is provided, the application knows about it, but does not directly create an instance. It gets instanciated when an @Inject(serviceIdentifier) is used as a constructor parameter in the same or a childs containers class.

The DI system is hierarchical. So the class provided in the parent module will also be available in all its children (with a single object instance).

Cheatsheet

Decorators

Decorator Description Parameters Return value
@NestModule() Creates a container where the classes are stored, imports child-NestModules config: { imports?: any[], declarations?: any[], providers?: any[], exports?: any[] } Returns a custom decorator where a container object is created in the constructor
@Injectable() Make class bindable to an NestModules container - Inversify @injectable()
@Component() Make class bindable to an NestModules container - Inversify @injectable()
@Inject(serviceIdentifier) Let the DI know that a class instance is needed, if not exist, create class serviceIdentifier -

NestModule parameters

Input parameter Description
imports Creates an instance of the imported NestModule(), 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.
declarations Binds @Component() decorated classes to the container and after creating all imports it directly creates an instance.
providers Binds @Injectable() decorated classes to the container
exports Binds @Injectable() or @Component() decorated classes to the parents container

Dependencies

Get in touch

twitter github stackoverflow

Hi, I am Felix, Angular developer and NgRX contributor

avatar

If you like this library, think about giving it a star or follow me on twitter or github.

About

Hierarchical NodeJS dependency injection inspired by Angular

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published