rob3000 / nestjs-kafka

NestJS integration with KafkaJS
The Unlicense
130 stars 44 forks source link

Feature Request: Provide async module initialization #10

Closed Shamshiel closed 3 years ago

Shamshiel commented 3 years ago

FIrst of all thank you for providing this package. I tried to use this package today and it seems to miss a critical feature.

I need to load the username, password and broker list on application start up dynamically. Other NestJS packages provide a "forRootAsync" or "registerAsync" method for this. I can use this method to inject some services and load config values.

Example from the HttpModule

HttpModule.registerAsync({
  useFactory: async (someService: SomeService) => {},
  inject: [SomeService]
}),

Sorry if this is already possible but I didn't find any method that would provide this. (Is there another way to achieve this that I'm not aware of?)

rob3000 commented 3 years ago

Thanks! I agree it does need the forRootAysnc method and it has been on my list to get too. If you were just wanting to load config you can do that using the nestjs config ConfigModule.forRoot(), the via:

import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    MicroserviceHealthIndicator,
    ConfigModule.forRoot(),
    KafkaModule.register([
      {
        name: 'KAFKA_SERVICE',
        options: {
          client: {
            clientId: 'my-client',
            brokers: [process.env.KAFKA_BROKERS || 'localhost:9092'],
            connectionTimeout: 3000,
            ssl: true,
            sasl: {
              mechanism: 'plain',
              username: process.env.KAFKA_USERNAME,
              password: process.env.KAFKA_PASSWORD
            }
          },
        }
     }]
]})
Shamshiel commented 3 years ago

@rob3000 I created a branch with asynchronous module initialization. I could make a pull request if you want but currently I'm not able to push the branch.

rob3000 commented 3 years ago

@Shamshiel a PR would be perfect! 👌

rob3000 commented 3 years ago

Released in 1.4.0