added another API for db connection
This commit is contained in:
@@ -11,4 +11,26 @@ const connectDB = async (): Promise<void> => {
|
||||
}
|
||||
}
|
||||
|
||||
export default connectDB;
|
||||
// const readyState = (): string => {
|
||||
// if (mongoose.connection.readyState === 0) {
|
||||
// return "Disconnected"
|
||||
// } else if (mongoose.connection.readyState === 1) {
|
||||
// return "Connected"
|
||||
// } else if (mongoose.connection.readyState === 2) {
|
||||
// return "Connecting"
|
||||
// } else if (mongoose.connection.readyState === 3) {
|
||||
// return "Disconnecting"
|
||||
// }
|
||||
// }
|
||||
|
||||
const readyState = () : string => {
|
||||
const states: Record<number, string> = {
|
||||
0: 'Disconnected',
|
||||
1: 'Connected',
|
||||
2: 'Connecting',
|
||||
3: 'Disconnecting',
|
||||
};
|
||||
return states[mongoose.connection.readyState] ?? 'Unknown';
|
||||
};
|
||||
|
||||
export {connectDB, readyState}
|
||||
@@ -1,7 +1,7 @@
|
||||
import express, {Application, Request, Response} from 'express';
|
||||
import cors from 'cors';
|
||||
import {ENV} from './config/env';
|
||||
import connectDB from './config/db';
|
||||
import {connectDB, readyState} from './config/db';
|
||||
|
||||
const app: Application = express()
|
||||
|
||||
@@ -16,6 +16,16 @@ app.get('/health', (_req: Request, res: Response)=> {
|
||||
});
|
||||
});
|
||||
|
||||
//for checking database connection
|
||||
|
||||
app.get('/health/db', (req: Request, res: Response)=> {
|
||||
res.json({
|
||||
db_status: readyState(),
|
||||
environment: ENV.NODE_ENV,
|
||||
timestamp: new Date().toISOString()
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
//start Server
|
||||
|
||||
|
||||
Reference in New Issue
Block a user