|
| 1 | +### Discovery Service |
| 2 | + |
| 3 | +The `DiscoveryService` is a utility provided by `@nestjs/core` that allows developers to dynamically discover providers, controllers, and other metadata within a NestJS application. This can be particularly useful for building plugins, decorators, or features that rely on runtime introspection. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +`DiscoveryService` is part of `@nestjs/core`, so it does not require separate installation. You can use it directly within your NestJS application. |
| 8 | + |
| 9 | +## Importing DiscoveryService and DiscoveryModule |
| 10 | + |
| 11 | +Before using the `DiscoveryService`, you need to import the `DiscoveryModule` in your module: |
| 12 | + |
| 13 | +```typescript |
| 14 | +@@filename(example.module) |
| 15 | +import { Module } from '@nestjs/common'; |
| 16 | +import { DiscoveryModule } from '@nestjs/core'; |
| 17 | +import { ExampleService } from './example.service'; |
| 18 | + |
| 19 | +@Module({ |
| 20 | + imports: [DiscoveryModule], |
| 21 | + providers: [ExampleService], |
| 22 | +}) |
| 23 | +export class ExampleModule {} |
| 24 | +``` |
| 25 | + |
| 26 | +Then, inject `DiscoveryService` into a provider or service: |
| 27 | + |
| 28 | +```typescript |
| 29 | +@@filename(example.service) |
| 30 | +@Injectable() |
| 31 | +export class ExampleService { |
| 32 | + constructor(private readonly discoveryService: DiscoveryService) {} |
| 33 | +} |
| 34 | +@@switch |
| 35 | +@Injectable() |
| 36 | +@Dependencies(DiscoveryService) |
| 37 | +export class ExampleService { |
| 38 | + constructor(discoveryService) { |
| 39 | + this.discoveryService = discoveryService; |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +> info **Hint** The `DiscoveryService` class is imported from the `@nestjs/core` package. |
| 45 | +
|
| 46 | +## Use Cases |
| 47 | + |
| 48 | +### 1. Discovering Providers |
| 49 | + |
| 50 | +You can retrieve all registered providers in the application: |
| 51 | + |
| 52 | +```typescript |
| 53 | +const providers = this.discoveryService.getProviders(); |
| 54 | +console.log(providers); |
| 55 | +``` |
| 56 | + |
| 57 | +Each provider object contains information about the instance, token, and metadata. |
| 58 | + |
| 59 | +### 2. Discovering Controllers |
| 60 | + |
| 61 | +Retrieve all registered controllers: |
| 62 | + |
| 63 | +```typescript |
| 64 | +const controllers = this.discoveryService.getControllers(); |
| 65 | +console.log(controllers); |
| 66 | +``` |
| 67 | + |
| 68 | +### 3. Finding Metadata |
| 69 | + |
| 70 | +`DiscoveryService` can help find metadata attached to providers or controllers. This is useful when working with decorators that add metadata. |
| 71 | + |
| 72 | +```typescript |
| 73 | +const providers = this.discoveryService.getProviders(); |
| 74 | + |
| 75 | +for (const provider of providers) { |
| 76 | + const metadata = this.reflector.get('custom:metadataKey', provider.instance.constructor); |
| 77 | + if (metadata) { |
| 78 | + console.log(`Metadata found:`, metadata); |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Example: Custom Decorator with DiscoveryService |
| 84 | + |
| 85 | +Suppose you have a custom decorator that adds metadata to a provider: |
| 86 | + |
| 87 | +```typescript |
| 88 | +import { SetMetadata } from '@nestjs/common'; |
| 89 | + |
| 90 | +export const CustomMetadata = (value: string) => SetMetadata('custom:metadataKey', value); |
| 91 | +``` |
| 92 | + |
| 93 | +And you use it in a service: |
| 94 | + |
| 95 | +```typescript |
| 96 | +import { Injectable } from '@nestjs/common'; |
| 97 | +import { CustomMetadata } from './custom-metadata.decorator'; |
| 98 | + |
| 99 | +@Injectable() |
| 100 | +@CustomMetadata('example-value') |
| 101 | +export class CustomService {} |
| 102 | +``` |
| 103 | + |
| 104 | +Now, you can use `DiscoveryService` to find all providers with this metadata: |
| 105 | + |
| 106 | +```typescript |
| 107 | +const providers = this.discoveryService.getProviders(); |
| 108 | + |
| 109 | +const filteredProviders = providers.filter((provider) => { |
| 110 | + if (!provider.instance) return null; |
| 111 | + return !!this.reflector.get(metadataPathToken, provider.instance.constructor); |
| 112 | +}); |
| 113 | + |
| 114 | +console.log('Providers with custom metadata:', filteredProviders); |
| 115 | +``` |
| 116 | + |
| 117 | +## Conclusion |
| 118 | + |
| 119 | +`DiscoveryService` is a powerful tool for runtime introspection in NestJS applications. It allows you to discover providers, controllers, and metadata dynamically, making it useful for plugin development, custom decorators, and advanced framework-level features. |
0 commit comments