2025-04-20 11:29:53 -07:00
|
|
|
import mongoose from "mongoose";
|
|
|
|
|
import AppSettings from "../models/AppSettings.js";
|
|
|
|
|
|
|
|
|
|
//****************************************
|
|
|
|
|
// Recovery Operations
|
|
|
|
|
//****************************************
|
|
|
|
|
import * as recoveryModule from "./modules/recoveryModule.js";
|
|
|
|
|
|
|
|
|
|
//****************************************
|
|
|
|
|
// Page Speed Checks
|
|
|
|
|
//****************************************
|
|
|
|
|
|
|
|
|
|
import * as pageSpeedCheckModule from "./modules/pageSpeedCheckModule.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";
|
|
|
|
|
|
2025-07-30 12:25:12 -07:00
|
|
|
constructor({
|
|
|
|
|
logger,
|
|
|
|
|
envSettings,
|
|
|
|
|
checkModule,
|
|
|
|
|
inviteModule,
|
|
|
|
|
statusPageModule,
|
|
|
|
|
userModule,
|
|
|
|
|
hardwareCheckModule,
|
|
|
|
|
maintenanceWindowModule,
|
|
|
|
|
monitorModule,
|
2025-07-30 15:06:19 -07:00
|
|
|
networkCheckModule,
|
2025-07-30 12:25:12 -07:00
|
|
|
}) {
|
2025-07-28 15:03:28 -07:00
|
|
|
this.logger = logger;
|
2025-07-28 14:37:18 -07:00
|
|
|
this.envSettings = envSettings;
|
2025-07-30 10:41:53 -07:00
|
|
|
this.userModule = userModule;
|
2025-07-29 19:53:11 -07:00
|
|
|
this.inviteModule = inviteModule;
|
2025-04-20 11:29:53 -07:00
|
|
|
Object.assign(this, recoveryModule);
|
|
|
|
|
Object.assign(this, monitorModule);
|
|
|
|
|
Object.assign(this, pageSpeedCheckModule);
|
2025-07-30 11:27:29 -07:00
|
|
|
this.hardwareCheckModule = hardwareCheckModule;
|
2025-07-29 11:51:06 -07:00
|
|
|
this.checkModule = checkModule;
|
2025-07-30 11:36:19 -07:00
|
|
|
this.maintenanceWindowModule = maintenanceWindowModule;
|
2025-07-30 12:25:12 -07:00
|
|
|
this.monitorModule = monitorModule;
|
2025-04-20 11:29:53 -07:00
|
|
|
Object.assign(this, notificationModule);
|
|
|
|
|
Object.assign(this, settingsModule);
|
2025-07-30 10:20:33 -07:00
|
|
|
this.statusPageModule = statusPageModule;
|
2025-04-20 11:29:53 -07:00
|
|
|
Object.assign(this, diagnosticModule);
|
2025-07-30 15:06:19 -07:00
|
|
|
this.networkCheckModule = networkCheckModule;
|
2025-04-20 11:29:53 -07:00
|
|
|
}
|
|
|
|
|
|
2025-07-25 15:15:14 -07:00
|
|
|
get serviceName() {
|
|
|
|
|
return MongoDB.SERVICE_NAME;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-20 11:29:53 -07:00
|
|
|
connect = async () => {
|
|
|
|
|
try {
|
2025-07-28 14:37:18 -07:00
|
|
|
const connectionString = this.envSettings.dbConnectionString || "mongodb://localhost:27017/uptime_db";
|
2025-04-20 11:29:53 -07:00
|
|
|
await mongoose.connect(connectionString);
|
2025-07-28 14:37:18 -07:00
|
|
|
// If there are no AppSettings, create one // TODO why is this here?
|
2025-04-20 11:29:53 -07:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 15:03:28 -07:00
|
|
|
this.logger.info({
|
2025-04-20 11:29:53 -07:00
|
|
|
message: "Connected to MongoDB",
|
|
|
|
|
service: this.SERVICE_NAME,
|
|
|
|
|
method: "connect",
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
2025-07-28 15:03:28 -07:00
|
|
|
this.logger.error({
|
2025-04-20 11:29:53 -07:00
|
|
|
message: error.message,
|
|
|
|
|
service: this.SERVICE_NAME,
|
|
|
|
|
method: "connect",
|
|
|
|
|
stack: error.stack,
|
|
|
|
|
});
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
disconnect = async () => {
|
|
|
|
|
try {
|
2025-07-28 15:03:28 -07:00
|
|
|
this.logger.info({ message: "Disconnecting from MongoDB" });
|
2025-04-20 11:29:53 -07:00
|
|
|
await mongoose.disconnect();
|
2025-07-28 15:03:28 -07:00
|
|
|
this.logger.info({ message: "Disconnected from MongoDB" });
|
2025-04-20 11:29:53 -07:00
|
|
|
return;
|
|
|
|
|
} catch (error) {
|
2025-07-28 15:03:28 -07:00
|
|
|
this.logger.error({
|
2025-04-20 11:29:53 -07:00
|
|
|
message: error.message,
|
|
|
|
|
service: this.SERVICE_NAME,
|
|
|
|
|
method: "disconnect",
|
|
|
|
|
stack: error.stack,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MongoDB;
|