From 0f1c4060773682402f335703e8ae4ca178022e67 Mon Sep 17 00:00:00 2001 From: Smultar Date: Sat, 6 Jan 2024 20:10:40 -0600 Subject: [PATCH] Obtained process values from node itself. --- src/services/mongoService.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/services/mongoService.ts b/src/services/mongoService.ts index 0ef0c82d..99c9a07e 100644 --- a/src/services/mongoService.ts +++ b/src/services/mongoService.ts @@ -1,18 +1,32 @@ import { logger } from "@/src/utils/logger"; -import * as dotenv from "dotenv"; +import { env } from "node:process"; import mongoose from "mongoose"; -dotenv.config(); +const { MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_URL, MONGODB_DATABASE, MONGODB_PORT } = env; -const url = process.env.MONGODB_URL; +console.log(process.env); -if (url === undefined) { +if (MONGODB_USERNAME === undefined) { + throw new Error("MONGODB_USERNAME not found. Make sure you have a .env file in the root of the project!"); +} +if (MONGODB_PASSWORD === undefined) { + throw new Error("MONGODB_PASSWORD not found. Make sure you have a .env file in the root of the project!"); +} +if (MONGODB_URL === undefined) { throw new Error("MONGODB_URL not found. Make sure you have a .env file in the root of the project!"); } +if (MONGODB_DATABASE === undefined) { + throw new Error("MONGODB_DATABASE not found. Make sure you have a .env file in the root of the project!"); +} +if (MONGODB_PORT === undefined) { + throw new Error("MONGOD_PORT not found. Make sure you have a .env file in the root of the project!"); +} const connectDatabase = async () => { try { - await mongoose.connect(`${url}`); + await mongoose.connect( + `mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_URL}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=admin` + ); logger.info("connected to MongoDB"); } catch (error: unknown) { if (error instanceof Error) {