Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeORM for MongoDB with mongodb+srv: HealthCheck reports DB down though NestJS TypeORM connected successfully #1102

Closed
1 of 5 tasks
SvendDomdey opened this issue Mar 10, 2021 · 9 comments

Comments

@SvendDomdey
Copy link
Contributor

I'm submitting a...

  • Regression
  • Bug report
  • Feature request
  • Documentation issue or request
  • Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

TypeOrmHealthIndicator does not work when MongoDB is used and the TypeORM connection to MongoDB was established by a remote URL.
Sample TypeOrmModuleOptions config:

{
   type: 'mongodb',
   url: `mongodb+srv://${username}:${password}@${hostname}/${DB_NAME}?retryWrites=true`,
   entities: [SomeEntity],
   useUnifiedTopology: true,
   keepConnectionAlive: true,
}

This makes TypeORM successfully connect to the remote database in our use case.
The TypeORM Health check, however, fails. We trigger it with

this.db.pingCheck('database')

From debugging it seems that in method checkMongoDBConnection in typeorm.health.ts the connection options are not working properly to build the right URL.

driver.buildConnectionUrl(connection.options),

During the startup of the app (successful connection to DB over TypeORM) the options.type that is handed over to the MongoDriver is

mongodb+srv 

for the config above. On contrary, for the health check the options.type is only

mongodb

as entered in the TypeOrmModuleOptions config. To our understanding a mapping from type mongodb to mongodb+srv needs to be performed in between.

In the end we get the error

{"database":{"status":"down","message":"connect ECONNREFUSED 127.0.0.1:27017"}}

from the health check - even though our app is up and connected to MongoDB via NestJS TypeORM.

Expected behavior

If NestJS TypeORM connection works, the health check must be able to connect as well and return the correct health result

{"database":{"status":"up"}}

Minimal reproduction of the problem with instructions

Environment

[Nest Information]
terminus version         : 7.1.0
Nest typeorm version     : 7.1.5
common version           : 7.4.2
core version             : 7.4.2

[other]
Typeorm		       : 0.2.30

Thanks for your help & kind regards
Svend

@Tony133
Copy link
Contributor

Tony133 commented Mar 10, 2021

Hi @SvendDomdey, I advise you to leave a minimal reproduction of a clonable git repository so that the core team can evaluate the problem you have reported.

@SvendDomdey
Copy link
Contributor Author

HI @Tony133,
thanks for the quick feedback. We will produce a minimal repository to illustrate the problem.
Unfortunately, reproducing the problem strictly speaking requires DB connection credentials which we obviously cannot share. But everything else we should be able to provide.

@Tony133
Copy link
Contributor

Tony133 commented Mar 10, 2021

@SvendDomdey for database connection maybe you can create a docker compose file, i think it should be fine.

@SvendDomdey
Copy link
Contributor Author

Hi @Tony133,

I have created a minimal repository at https://github.com/SvendDomdey/nestjs-terminus-issue-1102.git.
For the database, I used a free Atlas Mongo instance.

After cloning, you can GET or POST

localhost:3000/photos

to see that the DB connection is up & running. But on

localhost:3000/health/readiness

the health check fails as described.

@Tony133
Copy link
Contributor

Tony133 commented Mar 12, 2021

Hi @SvendDomdey, In your minimal repository by doing

localhost:3000/health/readiness

gives me this:
screenshot
(see screenshot)

does not return this answer to you?

you tried on the configuration of TypeOrmModule in the url to put everything in string without the template strings ( ${ } )

@SvendDomdey
Copy link
Contributor Author

Hi Tony,

with my computer, when I do the following

npm ci
npm run start

and then query my browser (Chrome latest) on

GET localhost:3000/health/readiness

I get
image

That is completely different from what you have. For us, this only happened when we accidentally had a MongoDB still running on our local machine at the very same port 27017. Since it's not relevant to what we configured, it must not be running.

Regarding your comment, I have tried with and without template strings. Does not make a difference - the app connection works (I can read photos from the DB) but the health check fails.

Would it be possible to provide a hint where the URL would actually be used? Then I might do a bit of debugging there.

Thanks & kind regards
Svend

@BrunnerLivio
Copy link
Member

Just released v7.1.1 🎉
Can you check whether @abt0 solution in #1122 works for you? If so please close the issue. Thanks everyone!

@SvendDomdey
Copy link
Contributor Author

Thanks! This worked for us! Thanks @abt0 for providing the fix.

@amo0725
Copy link

amo0725 commented Sep 21, 2023

Hi, my health check isnt working with mongo now

import { DataSourceOptions } from 'typeorm';
const { MONGODB_HOST, MONGODB_DBNAME, MONGODB_USERNAME, MONGODB_PASSWORD } =
  process.env;
export const typeormConfig: DataSourceOptions = {
  type: 'mongodb',
  url: `mongodb+srv://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}/${MONGODB_DBNAME}?retryWrites=true&w=majority`,
  synchronize: false,
  logging: true,
};
() => this.db.pingCheck('database')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants