I scatered the web for this answer or something similar but with no avail. I did found something that may help explain what am I looking for.
I want to get multiple Mongo connection in a Nestjs project based on a payload received from a RabbitMQ broker. Is there a way to do this in a similar way like in this post with the interception of the request in the declaration of the MongooseModule?
@Module( {
imports: [ RabbitMQModule.forRootAsync( RabbitMQModule, {
imports: [ ConfigModule ],
useClass: AmqpConfig,
inject: [ ConfigService ],
} ) ],
controllers: [ MessagingController ],
providers: [ MessagingService, ConfigService ],
exports: [ MessagingService, RabbitMQModule ]
} )
export class MessagingModule { }
The AmqpConfig class
@Injectable()
export class RabbitConfig {
constructor (
private readonly config: ConfigService,
) { }
createModuleConfig (): RabbitMQConfig | Promise<RabbitMQConfig> {
return {
name: this.name,
uri: this.uri,
exchanges: this.exchanges,
connectionInitOptions: this.connectionInitOptions,
enableControllerDiscovery: true,
};
}
get uri (): string {
const { host, port } = this.config.get( 'amqp' );
return `${ host }:${ port || 5672 }`;
}
get name (): string {
return this.config.get( 'amqp' ).name;
}
get exchanges (): Array<RabbitMQExchangeConfig> {
return Object.values( this.config.get( 'amqp' ).exchanges ) as Array<RabbitMQExchangeConfig>;
}
get connectionInitOptions () {
return this.config.get( 'amqp' ).connectionInitOptions;
}
};
How exactly should I configure the MongooseModule?
@Module( {
imports: [
ConfigModule.forRoot( {
isGlobal: true,
load: [ envConfig ]
} ),
MongooseModule.forRootAsync( {
useClass: MongoConfig,
inject: [ ConfigService ]
} )
],
controllers: [ AppController ],
providers: [ AppService ],
} )
export class AppModule { }
MongoConfig class:
export class MongoConfig implements MongooseOptionsFactory {
constructor ( private readonly config: ConfigService ) { }
createMongooseOptions (): MongooseModuleOptions | Promise<MongooseModuleOptions> {
return {
uri: this.uri
};
}
get uri () {
return this.config.get( 'mongo' ).uri;
}
}