Skip to content

Instantly share code, notes, and snippets.

@kevboutin
Last active August 26, 2024 03:13
Show Gist options
  • Save kevboutin/1d49a215f07676b50749d63d6b2e825e to your computer and use it in GitHub Desktop.
Save kevboutin/1d49a215f07676b50749d63d6b2e825e to your computer and use it in GitHub Desktop.
Determines if mongoose connection is good
// 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