Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 34 additions & 38 deletions src/db/db.config.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
import mongoose from 'mongoose';
import { logger } from '../logger';
import mongoose from "mongoose";
import { logger } from "../logger";

export class Database {
private static url : string | undefined;

public static connect() : void {
mongoose.connect(this.connectionString);

mongoose.connection.once("open", async () : Promise<void> => {
logger.info("Connected to database");
});

mongoose.connection.on("error", async (e) : Promise<void> => {
logger.error("Error connectiong to database", e);
});
}

public static disconnect() : void {
if (!mongoose.connection) {
return;
}

mongoose.disconnect();

mongoose.connection.close(async () : Promise<void> => {
logger.info("Disconnected from database");
})
}

private static get connectionString() : string {
this.url = process.env.MONGO_CONNECTION_STRING;

if (this.url === undefined) {
throw("MONGO_CONNECTION_STRING can not be found or is not defined");
}

return this.url;
}
}
private static url: string | undefined;

public static async connect(): Promise<void> {
try {
await mongoose.connect(this.connectionString);
logger.info("Connected to database");
} catch(e) {
throw e;
}
}

public static disconnect(): void {
if (!mongoose.connection) {
return;
}

mongoose.disconnect();
mongoose.connection.close((): void => {
logger.info("Disconnected from database");
});
}

private static get connectionString(): string {
this.url = process.env.MONGO_CONNECTION_STRING;

if (this.url === undefined) {
throw "MONGO_CONNECTION_STRING can not be found or is not defined";
}

return this.url;
}
}