Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/fjodor-rybakov/nestjs-dynamic-providers
- Owner: fjodor-rybakov
- Created: 2021-11-08T20:59:41.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-04-19T21:52:26.000Z (9 months ago)
- Last Synced: 2024-04-23T02:58:15.843Z (9 months ago)
- Topics: decorator, dynamic, glob, inject, module, nest, nestjs, pattern, providers, typescript
- Language: TypeScript
- Homepage:
- Size: 1.38 MB
- Stars: 14
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[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.
## ๐งพ 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
```> 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 {}
```