npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-dynamic-providers

v0.3.4

Published

Inject dynamic providers

Downloads

814

Readme

🧾 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 {}