Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 39x 213x 213x 213x 213x 213x 213x 39x 208x 208x 208x 39x | import mongoose from "mongoose";
import { logger } from "server/utils/logger";
import { config } from "shared/config";
const connectToDb = async (
dbConnString: string = config.server().dbConnString,
dbName: string = config.server().dbName,
): Promise<void> => {
logger.info(`MongoDB: Connecting to DB ${dbName}`);
const options = {
dbName,
};
try {
await mongoose.connect(dbConnString, options);
} catch (error) {
// eslint-disable-next-line no-restricted-syntax -- Server startup
throw new Error("MongoDB: Error connecting to DB", { cause: error });
}
logger.info("MongoDB: Connection successful");
mongoose.connection.on("error", (error) => {
logger.error(new Error("MongoDB: Connection error", { cause: error }));
});
};
const gracefulExit = async (): Promise<void> => {
try {
await mongoose.connection.close();
} catch (error) {
// eslint-disable-next-line no-restricted-syntax -- Server startup
throw new Error("MongoDB: Error shutting down db connection", {
cause: error,
});
}
logger.info("MongoDB connection closed");
};
export const db = {
connectToDb,
gracefulExit,
};
|