Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fjodor-rybakov/nestjs-dynamic-providers

๐Ÿ’‰ Inject providers into module by glob pattern
https://github.com/fjodor-rybakov/nestjs-dynamic-providers

decorator dynamic glob inject module nest nestjs pattern providers typescript

Last synced: 3 months ago
JSON representation

๐Ÿ’‰ Inject providers into module by glob pattern

Awesome Lists containing this project

README

        


Nest Logo

[travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
[travis-url]: https://travis-ci.org/nestjs/nest
[linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
[linux-url]: https://travis-ci.org/nestjs/nest

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

```bash
$ npm install nestjs-dynamic-providers
```

Or via yarn

```bash
$ 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.

```typescript
/* 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.

```typescript
/* 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 {}
```

```typescript
/* hippo.animal.ts */

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

@Injectable()
export class Hippo {}
```

```typescript
/* lion.animal.ts */

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

@Injectable()
export class Lion {}
```

```typescript
/* 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.

```typescript
/* 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`.

```typescript
/* 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 {}
```