From 5bcd290394ec2e6fa2c6523f4e1eeb79bc18b59e Mon Sep 17 00:00:00 2001 From: jared-ivory <26067982+Jared-Ivory@users.noreply.github.com> Date: Thu, 6 Oct 2022 21:40:08 -0400 Subject: [PATCH 1/4] Changed connect function to be async --- src/db/db.config.ts | 80 ++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/src/db/db.config.ts b/src/db/db.config.ts index 22d0eb2..b7626b6 100644 --- a/src/db/db.config.ts +++ b/src/db/db.config.ts @@ -1,40 +1,44 @@ -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 => { - logger.info("Connected to database"); - }); - - mongoose.connection.on("error", async (e) : Promise => { - logger.error("Error connectiong to database", e); - }); - } - - public static disconnect() : void { - if (!mongoose.connection) { - return; - } - - mongoose.disconnect(); - - mongoose.connection.close(async () : Promise => { - 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; - } -} \ No newline at end of file + private static url: string | undefined; + + public static async connect(): Promise { + return new Promise((resolve, reject) => { + mongoose.connect(this.connectionString); + + mongoose.connection.once("open", async (): Promise => { + logger.info("Connected to database"); + resolve(); + }); + + mongoose.connection.on("error", async (e): Promise => { + logger.error("Error connectiong to database", e); + reject(); + }); + }); + } + + public static disconnect(): void { + if (!mongoose.connection) { + return; + } + + mongoose.disconnect(); + + mongoose.connection.close(async (): Promise => { + 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; + } +} From 75b32132866966d7ad9f660bec50bbfc1c14bd63 Mon Sep 17 00:00:00 2001 From: jared-ivory <26067982+Jared-Ivory@users.noreply.github.com> Date: Thu, 6 Oct 2022 21:44:07 -0400 Subject: [PATCH 2/4] Changed from Promise to Promise --- src/db/db.config.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/db/db.config.ts b/src/db/db.config.ts index b7626b6..4eab48f 100644 --- a/src/db/db.config.ts +++ b/src/db/db.config.ts @@ -4,18 +4,18 @@ import { logger } from "../logger"; export class Database { private static url: string | undefined; - public static async connect(): Promise { + public static async connect(): Promise { return new Promise((resolve, reject) => { mongoose.connect(this.connectionString); mongoose.connection.once("open", async (): Promise => { logger.info("Connected to database"); - resolve(); + resolve(true); }); mongoose.connection.on("error", async (e): Promise => { logger.error("Error connectiong to database", e); - reject(); + reject(false); }); }); } From 5b155ec0da438f1cb956f85ee3e218f5f1dc785a Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 7 Oct 2022 09:52:06 -0700 Subject: [PATCH 3/4] Add `try catch` block to `Database.connect` --- src/db/db.config.ts | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/db/db.config.ts b/src/db/db.config.ts index 4eab48f..64279da 100644 --- a/src/db/db.config.ts +++ b/src/db/db.config.ts @@ -4,20 +4,14 @@ import { logger } from "../logger"; export class Database { private static url: string | undefined; - public static async connect(): Promise { - return new Promise((resolve, reject) => { - mongoose.connect(this.connectionString); - - mongoose.connection.once("open", async (): Promise => { - logger.info("Connected to database"); - resolve(true); - }); - - mongoose.connection.on("error", async (e): Promise => { - logger.error("Error connectiong to database", e); - reject(false); - }); - }); + public static async connect(): Promise { + try { + await mongoose.connect(this.connectionString); + logger.info("Connected to database"); + } catch(e) { + logger.error("Error connecting to database", e); + throw e; + } } public static disconnect(): void { @@ -26,8 +20,7 @@ export class Database { } mongoose.disconnect(); - - mongoose.connection.close(async (): Promise => { + mongoose.connection.close((): void => { logger.info("Disconnected from database"); }); } From 34c34373729d9c8059511ce12c011b8c15379b90 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 7 Oct 2022 17:25:25 -0700 Subject: [PATCH 4/4] Remove redundant log call in DB --- src/db/db.config.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/db/db.config.ts b/src/db/db.config.ts index 64279da..065ca6b 100644 --- a/src/db/db.config.ts +++ b/src/db/db.config.ts @@ -9,7 +9,6 @@ export class Database { await mongoose.connect(this.connectionString); logger.info("Connected to database"); } catch(e) { - logger.error("Error connecting to database", e); throw e; } }