Files
Checkmate/server/src/db/MongoDB.ts
T

94 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-05-26 13:31:13 -07:00
import { Mongoose } from "mongoose";
2025-04-20 11:29:53 -07:00
import mongoose from "mongoose";
2026-03-05 14:46:28 -08:00
import AppSettings from "@/db/models/AppSettings.js";
import { runMigrations } from "@/db/migration/index.js";
2026-01-22 21:49:49 +00:00
import { ILogger } from "@/utils/logger.js";
import { EnvConfig } from "@/service/system/settingsService.js";
const SERVICE_NAME = "MongoDB";
2026-03-05 14:46:28 -08:00
import { IDb } from "@/db/IDb.js";
2026-05-26 13:31:13 -07:00
import { AppError } from "@/utils/AppError.js";
2026-03-05 14:46:28 -08:00
2026-05-26 13:31:13 -07:00
class MongoDB implements IDb<Mongoose> {
2026-01-22 21:49:49 +00:00
static SERVICE_NAME = SERVICE_NAME;
2025-04-20 11:29:53 -07:00
2026-01-22 21:49:49 +00:00
private logger: ILogger;
private envSettings: EnvConfig;
2026-05-26 13:31:13 -07:00
private db: Mongoose | null;
2026-01-22 21:49:49 +00:00
constructor(logger: ILogger, envSettings: EnvConfig) {
2025-07-28 15:03:28 -07:00
this.logger = logger;
2025-07-28 14:37:18 -07:00
this.envSettings = envSettings;
2026-05-26 13:31:13 -07:00
this.db = null;
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";
2026-05-26 13:31:13 -07:00
this.db = 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,
}
);
// Run migrations BEFORE syncIndexes. The 0006 dedupe migration must run before
// the unique index on MonitorStats.monitorId is enforced, otherwise databases
// with stray duplicates (caused by the pre-fix race in updateRunningStats)
// will crash at startup when the unique index fails to build.
await runMigrations();
2025-04-20 11:29:53 -07:00
// 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",
2026-01-22 21:49:49 +00:00
service: SERVICE_NAME,
2025-04-20 11:29:53 -07:00
method: "connect",
});
2026-03-05 14:46:28 -08:00
} catch (error: unknown) {
2025-07-28 15:03:28 -07:00
this.logger.error({
2026-03-05 14:46:28 -08:00
message: error instanceof Error ? error.message : "Unknown error",
2026-01-22 21:49:49 +00:00
service: SERVICE_NAME,
2025-04-20 11:29:53 -07:00
method: "connect",
2026-03-05 14:46:28 -08:00
stack: error instanceof Error ? error.stack : undefined,
2025-04-20 11:29:53 -07:00
});
throw error;
}
};
2026-05-26 13:31:13 -07:00
getConnection = async (): Promise<Mongoose> => {
if (this.db === null) {
throw new AppError({ message: "Database not connected", service: SERVICE_NAME, method: "getConnection" });
}
return this.db;
};
2025-04-20 11:29:53 -07:00
disconnect = async () => {
try {
2026-01-22 21:49:49 +00:00
this.logger.info({ message: "Disconnecting from MongoDB", service: SERVICE_NAME, method: "disconnect" });
2025-04-20 11:29:53 -07:00
await mongoose.disconnect();
2026-01-22 21:49:49 +00:00
this.logger.info({ message: "Disconnected from MongoDB", service: SERVICE_NAME, method: "disconnect" });
2025-04-20 11:29:53 -07:00
return;
2026-03-05 14:46:28 -08:00
} catch (error: unknown) {
2025-07-28 15:03:28 -07:00
this.logger.error({
2026-03-05 14:46:28 -08:00
message: error instanceof Error ? error.message : "Unknown error",
2026-01-22 21:49:49 +00:00
service: SERVICE_NAME,
2025-04-20 11:29:53 -07:00
method: "disconnect",
2026-03-05 14:46:28 -08:00
stack: error instanceof Error ? error.stack : undefined,
2025-04-20 11:29:53 -07:00
});
}
};
}
export default MongoDB;