import mongoose from "mongoose"; import AppSettings from "../models/AppSettings.js"; //**************************************** // Recovery Operations //**************************************** import * as recoveryModule from "./modules/recoveryModule.js"; //**************************************** // Monitors //**************************************** import * as monitorModule from "./modules/monitorModule.js"; //**************************************** // Page Speed Checks //**************************************** import * as pageSpeedCheckModule from "./modules/pageSpeedCheckModule.js"; //**************************************** // Hardware Checks //**************************************** import * as hardwareCheckModule from "./modules/hardwareCheckModule.js"; //**************************************** // Maintenance Window //**************************************** import * as maintenanceWindowModule from "./modules/maintenanceWindowModule.js"; //**************************************** // Notifications //**************************************** import * as notificationModule from "./modules/notificationModule.js"; //**************************************** // AppSettings //**************************************** import * as settingsModule from "./modules/settingsModule.js"; //**************************************** // Diagnostic //**************************************** import * as diagnosticModule from "./modules/diagnosticModule.js"; class MongoDB { static SERVICE_NAME = "MongoDB"; constructor({ logger, envSettings, checkModule, inviteModule, statusPageModule, userModule }) { this.logger = logger; this.envSettings = envSettings; this.userModule = userModule; this.inviteModule = inviteModule; Object.assign(this, recoveryModule); Object.assign(this, monitorModule); Object.assign(this, pageSpeedCheckModule); Object.assign(this, hardwareCheckModule); this.checkModule = checkModule; Object.assign(this, maintenanceWindowModule); Object.assign(this, notificationModule); Object.assign(this, settingsModule); this.statusPageModule = statusPageModule; Object.assign(this, diagnosticModule); } get serviceName() { return MongoDB.SERVICE_NAME; } connect = async () => { try { const connectionString = this.envSettings.dbConnectionString || "mongodb://localhost:27017/uptime_db"; await mongoose.connect(connectionString); // If there are no AppSettings, create one // TODO why is this here? await AppSettings.findOneAndUpdate( {}, // empty filter to match any document {}, // empty update { new: true, setDefaultsOnInsert: true, } ); // Sync indexes const models = mongoose.modelNames(); for (const modelName of models) { const model = mongoose.model(modelName); await model.syncIndexes(); } this.logger.info({ message: "Connected to MongoDB", service: this.SERVICE_NAME, method: "connect", }); } catch (error) { this.logger.error({ message: error.message, service: this.SERVICE_NAME, method: "connect", stack: error.stack, }); throw error; } }; disconnect = async () => { try { this.logger.info({ message: "Disconnecting from MongoDB" }); await mongoose.disconnect(); this.logger.info({ message: "Disconnected from MongoDB" }); return; } catch (error) { this.logger.error({ message: error.message, service: this.SERVICE_NAME, method: "disconnect", stack: error.stack, }); } }; } export default MongoDB;