I am using the @nestjs/mongoose package to connect to Mongo DB. I have this in app.module.ts:
imports: [
MongooseModule.forRoot(process.env.MONGODB_URI, {
useNewUrlParser: true,
bufferCommands: false,
bufferMaxEntries: 0,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
})]
Now, the NestJS service is running in a Kubernetes cluster with proper health, liveness and readiness checks. Whenever the readiness or health end-point is invoked on the service, i need to check if the hosted Atlas Mongo DB connection is available or if there is an outage. As of now, the following code is executed for readiness and health end-points:
this.mongoDbConnection = await mongoose.connect(process.env.MONGODB_URI, options);
return (this.mongoDbConnection.connection.readyState === 1);
But this creates a new connection with the mongo DB server. What i want is to retrieve the readyState of the existing connection that NestJS makes through MongooseModule.forRoot in app.module.ts.
I am not sure how to retrieve the existing mongoose connection object in the health check service code. Any help would be much appreciated.