feat: Add environment utility and MongoDB services

Two new classes have been added: EnvUtils and MongodbService. EnvUtils provides functionality for fetching environment variables and logging them. MongodbService implements connection to MongoDB with user credentials fetched from environment variables. Additionally, some updates have been made to logs.util.ts to improve logging style and biome.json for better code standards adherence.
This commit is contained in:
Mathis H (Avnyr) 2024-05-13 16:54:01 +02:00
parent 2b53168dd9
commit 02224e0727
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
8 changed files with 100 additions and 12 deletions

View File

@ -20,6 +20,9 @@
"recommended": true,
"noDelete": "off"
},
"suspicious": {
"noExplicitAny": "warn"
},
"complexity": {
"useLiteralKeys": "off"
}

BIN
db.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

View File

View File

@ -0,0 +1,48 @@
import { EnvUtils } from "@utils/env.util";
import { LogsUtils } from "@utils/logs.util";
import { MongoClient } from "mongodb";
export class MongodbService {
private envs: EnvUtils;
private readonly client: MongoClient;
private logs: LogsUtils;
constructor(contextName: string) {
this.envs = new EnvUtils(`MongoDB >> ${contextName}`);
this.logs = new LogsUtils(`MongoDB >> ${contextName}`);
try {
const uri = `mongodb://${this.envs.get("MONGO_USERNAME")}:${this.envs.get("MONGO_PASSWORD")}@localhost:${this.envs.get("MONGO_PORT")}/`;
this.logs.trace("MongoDB URI:", uri);
this.client = new MongoClient(uri);
} catch (error) {
this.logs.error(`Error connecting to MongoDB:`, error);
throw new Error();
}
}
connect() {
return this.client
.connect()
.then(() => {
this.logs.info("Connected to MongoDB");
})
.catch((error) => {
this.logs.error("Error connecting to MongoDB:", error);
//throw error;
});
}
use() {
try {
return this.client.db(`${this.envs.get("MONGO_DATABASE")}`);
} catch (err) {
this.logs.error("Error using MongoDB:", err);
throw err;
}
}
close() {
this.client.close().then(() => {
this.logs.info("Connection to MongoDB closed");
});
}
}

18
src/utils/env.util.ts Normal file
View File

@ -0,0 +1,18 @@
import * as process from "node:process";
import { LogsUtils } from "@utils/logs.util";
export class EnvUtils {
private _envs: NodeJS.ProcessEnv;
private log: LogsUtils;
constructor(contextName: string) {
this.log = new LogsUtils(contextName);
this._envs = process.env;
}
get(envName: string) {
const value = this._envs[`${envName.toString()}`]?.toString();
this.log.debug(`Getting environment variable..`, `${envName} = "${value}"`);
return value || null;
}
}

View File

@ -1,39 +1,58 @@
import * as process from "node:process";
import {ILogObj, Logger} from "tslog";
import { type ILogObj, Logger } from "tslog";
export class LogsUtils {
private Logger: Logger<ILogObj>;
constructor(contextName: string) {
this.Logger = new Logger({name: contextName})
this.Logger = new Logger({
name: contextName,
prettyLogTimeZone: `local`
});
}
info(value: any, value2?: any) {
this.Logger.info(`\n\n(i) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
this.Logger.info(
`\n\n(i) > ${value}\n${value2 ? "> " + value2 + "\n" : ""}`,
);
}
warn(value: any, value2?: any) {
this.Logger.warn(`\n\n(?) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
this.Logger.warn(
`\n\n(?) > ${value}\n${value2 ? "> " + value2 + "\n" : ""}`,
);
}
error(value: any, value2?: any) {
this.Logger.error(`\n\n(!) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
if (process.env["CONTEXT"] === 'prod') process.exit(-1)
this.Logger.error(
`\n\n(!) > ${value}\n${value2 ? "> " + value2 + "\n" : ""}`,
);
if (process.env["CONTEXT"] === "prod") process.exit(-1);
}
softError(value: any, value2?: any) {
this.Logger.error(`\n\n(!) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
this.Logger.error(
`\n\n(!) > ${value}\n${value2 ? "> " + value2 + "\n" : ""}`,
);
}
trace(value: any, value2?: any) {
if (process.env["DEBUG"] === 'true') this.Logger.trace(`\n\n(*) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
if (process.env["DEBUG"] === "true")
this.Logger.trace(
`\n\n(*) > ${value}\n${value2 ? "> " + value2 + "\n" : ""}`,
);
}
fatal(value: any, value2?: any) {
this.Logger.fatal(`\n\n(X) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
if (process.env["CONTEXT"] === 'prod') process.exit(-1)
this.Logger.fatal(
`\n\n(X) > ${value}\n${value2 ? "> " + value2 + "\n" : ""}`,
);
if (process.env["CONTEXT"] === "prod") process.exit(-1);
}
debug(value: any, value2?: any) {
if (process.env["DEBUG"] === 'true') this.Logger.debug(`\n\n(~) |> ${value}\n${value2 ? '|> ' + value2 + '\n' : ''}`)
if (process.env["DEBUG"] === "true")
this.Logger.debug(
`\n\n(~) > ${value}\n${value2 ? "> " + value2 + "\n" : ""}`,
);
}
}
}