如何在Nestjs中配置Mongoose连接参数

huangapple go评论67阅读模式
英文:

how config mongoose connect parameters in Nestjs

问题

tls: true,
tlsAllowInvalidHostnames: true,
tlsCAFile: "rds-combined-ca-bundle.pem",
英文:

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

答案1

得分: 1

Sure, here is the translated code snippet:

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

Can you try something like this please:

MongooseModule.forRootAsync({
 imports: [ConfigModule],
 connectionName: &#39;mySampleDB&#39;,
 useFactory: async (config: ConfigService) =&gt; ({
  uri: config.get&lt;string&gt;(&#39;MONGODB_URI&#39;),
  useNewUrlParser: true,
  useUnifiedTopology: true,
 }),
 inject: [ConfigService],
})

huangapple
  • 本文由 发表于 2023年5月7日 15:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定