Search code examples
javascriptmongoosenestjs

how config mongoose connect parameters in Nestjs


in my nestjs application i want to change some connect parameters, with mongoose itself i can do it like this :

await mongoose.connect(process.env.DB, {
      readPreference: "primary",
      tls: true,
      tlsAllowInvalidHostnames: true,
      tlsCAFile: "rds-combined-ca-bundle.pem",
});

this is my nest database module how can i add those params to this?

@Module({
  imports: [
    MongooseModule.forRootAsync({
      useFactory: async (configService: ConfigService) => ({
        uri: configService.get('MONGODB_URI'),
      }),
      inject: [ConfigService],
    }),
  ],
})
export class DatabaseModule {
  static forFeature(models: ModelDefinition[]) {
    return MongooseModule.forFeature(models);
  }
}

i need set those params so i can connect to my AWS document db


Solution

  • Can you try something like this please:

    MongooseModule.forRootAsync({
     imports: [ConfigModule],
     connectionName: 'mySampleDB',
     useFactory: async (config: ConfigService) => ({
      uri: config.get<string>('MONGODB_URI'),
      useNewUrlParser: true,
      useUnifiedTopology: true,
     }),
     inject: [ConfigService],
    })