From 5b067b17fb3d287995fcefb5a6696b08abe1c67e Mon Sep 17 00:00:00 2001 From: Jipson Minibhavan Date: Thu, 8 Feb 2024 14:15:50 +0100 Subject: [PATCH] initializeDB out of constructor --- backend/src/index.ts | 10 +++++++++- backend/src/server.ts | 3 +-- resolver/src/app.ts | 3 +-- resolver/src/server.ts | 10 +++++++++- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/backend/src/index.ts b/backend/src/index.ts index c9f0a96..a1a980f 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -5,7 +5,15 @@ import linkRoutes from "./routes/linkRoutes"; const app = new App([userRoutes, statusRoutes, linkRoutes]); -app.listen(); +app + .initializeDB() + .then(() => { + app.listen(); + }) + .catch((error) => { + console.error("Failed to initialize the application:", error); + process.exit(1); + }); // import express, { Request, Response } from "express"; // import bodyParser from "body-parser"; diff --git a/backend/src/server.ts b/backend/src/server.ts index 8ff8e93..a56560c 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -19,7 +19,6 @@ class App { this.initializeMiddleware(); this.initializePassport(); this.initializeRoutes(routes); - this.initializeDB(); } private initializeMiddleware(): void { this.app.use(cors()); @@ -48,7 +47,7 @@ class App { }); } - private async initializeDB(): Promise { + public async initializeDB(): Promise { try { await connectToDB(); console.log("Database connection successful"); diff --git a/resolver/src/app.ts b/resolver/src/app.ts index 1a0abf1..df1739f 100644 --- a/resolver/src/app.ts +++ b/resolver/src/app.ts @@ -10,7 +10,6 @@ class App { this.express = express(); this.middleware(); this.initializeRouters(routers); - this.databaseConnection(); } private middleware(): void { @@ -24,7 +23,7 @@ class App { }); } - private async databaseConnection(): Promise { + public async initializeDB(): Promise { try { await connectToDB(); } catch (error) { diff --git a/resolver/src/server.ts b/resolver/src/server.ts index cd2c8c3..8040348 100644 --- a/resolver/src/server.ts +++ b/resolver/src/server.ts @@ -3,4 +3,12 @@ import linkRoutes from "./routes/linkRoutes"; const app = new App([linkRoutes]); -app.listen(); +app + .initializeDB() + .then(() => { + app.listen(); + }) + .catch((error) => { + console.error("Failed to initialize the application:", error); + process.exit(1); + });