Skip to content

Instantly share code, notes, and snippets.

@evolkmann
Created January 4, 2019 15:34
Show Gist options
  • Save evolkmann/1019721d46304df3d8ff7adbca87d8ec to your computer and use it in GitHub Desktop.
Save evolkmann/1019721d46304df3d8ff7adbca87d8ec to your computer and use it in GitHub Desktop.
Create Nest.js modules with custom config
import { Module } from '@nestjs/common';
import { MyLibModule } from './my-lib.module';
@Module({
imports: [
MyLibModule.register({ name: 'Enzo' }),
]
})
export class ImportingModule {}
import { Module, DynamicModule } from '@nestjs/common';
import { MyLibService } from './my-lib.service';
export interface Config {
name: string;
}
@Module({})
export class MyLibModule {
static register(options: Config): DynamicModule {
return {
module: MyLibModule,
providers: [
{
provide: MyLibService,
useValue: new MyLibService(options)
}
],
exports: [
MyLibService
]
};
}
}
import { Injectable } from '@nestjs/common';
import { Config } from './my-lib.module';
@Injectable()
export class MyLibService {
private readonly config: Config;
constructor(config: Config) {
this.config = config;
}
greet() {
console.log(`Hello ${this.config.name}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment