Obtained process values from node itself.

This commit is contained in:
Smultar 2024-01-06 20:10:40 -06:00
parent 2e183163d4
commit 0f1c406077

View File

@ -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) {