AlariCode / nestjs-rmq

A custom library for NestJS microservice. It allows you to use RabbitMQ or AMQP.
https://purpleschool.ru
MIT License
285 stars 38 forks source link

Accessing configService in module #17

Closed Saro-DA closed 3 years ago

Saro-DA commented 3 years ago

Hi.

In your example:

import { RMQModule } from 'nestjs-tests';

@Module({
    imports: [
        RMQModule.forRoot({
            exchangeName: configService.get('AMQP_EXCHANGE'),
            connections: [
                {
                    login: configService.get('AMQP_LOGIN'),
                    password: configService.get('AMQP_PASSWORD'),
                    host: configService.get('AMQP_HOST'),
                },
            ],
        }),
    ],
})
export class AppModule {}

Can you please explain how can we access the configService in this module? In the example that you provided you have hardcoded the settings.

Thanks.

AlariCode commented 3 years ago

In this example I used https://github.com/AlariCode/nestjs-dotenv.

import { ConfigModule, ConfigService } from 'nestjs-dotenv';

const configService = new ConfigService();

@Module({
    imports: [
                ConfigModule.forRoot(),
        RMQModule.forRoot({
            exchangeName: configService.get('AMQP_EXCHANGE'),
            connections: [
                {
                    login: configService.get('AMQP_LOGIN'),
                    password: configService.get('AMQP_PASSWORD'),
                    host: configService.get('AMQP_HOST'),
                },
            ],
        }),
    ],
})
export class AppModule {}
m-czyz commented 3 years ago

Imo module should expose forRootAsync method and then you can inject standard injectable service e.g nest/config ConfigService

const configService = new ConfigService(); is not an elegant solution

AlariCode commented 3 years ago

@m-czyz, thanks, I’ll look into adding forRootAsync. Pull request will be appreciated.

AlariCode commented 3 years ago

Added forRootAsync support in 1.11.0 version