@byndyusoft/nest-dynamic-module
v1.0.0
Published
DynamicModule helper for NestJS
Downloads
334
Keywords
Readme
nest-dynamic-module
DynamicModule helper for NestJS
Requirements
- Node.js v12 LTS or later
- npm or yarn
Install
npm install @byndyusoft/nest-dynamic-module
or
yarn add @byndyusoft/nest-dynamic-module
Usage
Example usage:
import {
DynamicModuleHelper,
TRegisterAsyncOptions,
} from "@byndyusoft/nest-dynamic-module";
import { DynamicModule, Inject, Injectable, Module } from "@nestjs/common";
class ExampleOptionsDto {
public readonly connectionString!: string;
}
const EXAMPLE_OPTIONS_TOKEN = Symbol("EXAMPLE_OPTIONS_TOKEN");
@Injectable()
class ExampleProvider {
public constructor(
@Inject(EXAMPLE_OPTIONS_TOKEN)
options: ExampleOptionsDto,
) {
console.log(options.connectionString);
}
}
@Module({})
class ExampleModule {
public static registerAsync(
options?: TRegisterAsyncOptions<ExampleOptionsDto>,
): DynamicModule {
return DynamicModuleHelper.registerAsync(
{
module: ExampleModule,
global: true,
providers: [ExampleProvider],
exports: [ExampleProvider],
},
EXAMPLE_OPTIONS_TOKEN,
options,
);
}
}
class ConfigDto {
public readonly exampleOptions!: ExampleOptionsDto;
}
@Module({
imports: [
ExampleModule.registerAsync({
inject: [ConfigDto],
useFactory: (config: ConfigDto) => config.exampleOptions,
}),
],
})
class AppModule {}
Maintainers
License
This repository is released under version 2.0 of the Apache License.