Skip to content

fjodor-rybakov/nestjs-dynamic-providers

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

Package License

🧾 Description

Do you have many classes of the same type and adding each of them to the module is burdensome? This package was created in order to be able to add providers to the module using the glob pattern.

👨🏻‍💻 Installation

$ npm install nestjs-dynamic-providers

Or via yarn

$ yarn add nestjs-dynamic-providers

▶️ Usage

You may notice that files with .ts extension have a glob pattern is set for .js. This example assumes that you are compiling files from typescript to javascript. This note does not apply for ts-node.

⚠️Important! Files are searched from the startup root(process.cwd()) by default. To override this, you can pass startup path in resolveDynamicProviders('my/startup/folder') as argument.

First you need to call the initialization function in bootstrap.

/* main.ts */

import { AppModule } from './app.module';
import { NestFactory } from '@nestjs/core';
import { resolveDynamicProviders } from 'nestjs-dynamic-providers';

async function bootstrap() {
  await resolveDynamicProviders();
  const app = await NestFactory.createApplicationContext(AppModule);
  await app.init();
}

bootstrap();

Then just add @InjectDynamicProviders decorator to the module. The sample below will add to the providers of the module all classes that it finds in files that end in .animal.js.

By default, classes are searched for that are marked with @Injectable() decorator. To override you need to pass filterPredicate as parameters to @InjectDynamicProviders().

  • @InjectDynamicProviders decorator takes list of glob patterns or list of options as parameters.
/* animal.module.ts */

import { Module } from '@nestjs/common';
import { InjectDynamicProviders } from 'nestjs-dynamic-providers';
import { AnyOtherProvider } from './any-other-provider';

@InjectDynamicProviders('dist/**/*.animal.js')
@Module({
  providers: [AnyOtherProvider], // Will be [AnyOtherProvider, Hippo, Lion]
})
export class AnimalModule {}
/* hippo.animal.ts */

import { Injectable } from '@nestjs/common';

@Injectable()
export class Hippo {}
/* lion.animal.ts */

import { Injectable } from '@nestjs/common';

@Injectable()
export class Lion {}
/* app.module.ts */

import { Module } from '@nestjs/common';
import { AnimalModule } from './animal.module';

@Module({
  imports: [AnimalModule],
})
export class AppModule {}

You can also add providers to exports.

/* animal.module.ts */

import { Module } from '@nestjs/common';
import { InjectDynamicProviders } from 'nestjs-dynamic-providers';
import { AnyOtherProvider } from './any-other-provider';

@InjectDynamicProviders({ pattern: 'dist/**/*.animal.js', exportProviders: true })
@Module({
  providers: [AnyOtherProvider], // Will be [AnyOtherProvider, Hippo, Lion]
  exports: [AnyOtherProvider], // Will be [AnyOtherProvider, Hippo, Lion]
})
export class AnimalModule {}

To override the search criteria for class in file, use filterPredicart.

/* bot.module.ts */

import { Module } from '@nestjs/common';
import { InjectDynamicProviders, IsObject } from 'nestjs-dynamic-providers';

@InjectDynamicProviders({
  pattern: '**/*.command.js',
  filterPredicate: (type) => // Filter only object type and class has decorator with metadata key `ANY_METADATA_KEY`
    IsObject(type) && Reflect.hasMetadata('ANY_METADATA_KEY', type.prototype),
})
@Module({})
export class BotModule {}