Last active
August 26, 2024 03:13
-
-
Save kevboutin/1d49a215f07676b50749d63d6b2e825e to your computer and use it in GitHub Desktop.
Determines if mongoose connection is good
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// As stated before "readyState" is good. "ping" is also good admin utility for doing so as well. | |
// It will return { ok: 1 } if it can accept commands. | |
const mongoose = require('mongoose'); | |
// Create the database connection. | |
const connection = await mongoose.createConnection( | |
CONNECT_URI, | |
CONNECT_OPTS | |
); | |
const connectionIsUp = async () => { | |
try { | |
const adminUtil = connection.db.admin(); | |
const result = await adminUtil.ping(); | |
console.log('result: ', result); // { ok: 1 } | |
return !!result?.ok === 1; | |
} catch (err) { | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment