Search code examples
modulerabbitmqnestjs

Can you import a NestJS module on condition


I'am creating a microservice in NestJS. Now I want to use RabbitMQ to send messages to another service.

My question is: is it possible to import the RabbitmqModule based on a .env variable? Such as: USE_BROKER=false. If this variable is false, than don't import the module?

RabbitMQ is imported in the GraphQLModule below.

@Module({
  imports: [
    GraphQLFederationModule.forRoot({
      autoSchemaFile: true,
      context: ({ req }) => ({ req }),
    }),
    DatabaseModule,
    AuthModule,
    RabbitmqModule,
  ],
  providers: [UserResolver, FamilyResolver, AuthResolver],
})
export class GraphQLModule {}

RabbitmqModule:

import { Global, Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
import { UserProducer } from './producers/user.producer';

@Global()
@Module({
  imports: [
    RabbitMQModule.forRootAsync(RabbitMQModule, {
      useFactory: async (config: ConfigService) => ({
        exchanges: [
          {
            name: config.get('rabbitMQ.exchange'),
            type: config.get('rabbitMQ.exchangeType'),
          },
        ],
        uri: config.get('rabbitMQ.url'),
        connectionInitOptions: { wait: false },
      }),
      inject: [ConfigService],
    }),
  ],
  providers: [UserProducer],
  exports: [UserProducer],
})
export class RabbitmqModule {}

Solution

  • I think the recommended way to do so is to use the DynamicModule feature from NestJS.

    It is explained here: https://docs.nestjs.com/fundamentals/dynamic-modules

    Simply check your environment variable in the register function and return your Module object. Something like:

    @Module({})
    export class GraphQLModule {
      static register(): DynamicModule {
        const imports = [
          GraphQLFederationModule.forRoot({
            autoSchemaFile: true,
            context: ({ req }) => ({ req }),
          }),
          DatabaseModule,
          AuthModule]
        if (process.env.USE_BROKER) {
          imports.push(RabbitmqModule)
        }
        return {
          imports,
          providers: [UserResolver, FamilyResolver, AuthResolver],
        };
      }
    }