diff --git a/biome.json b/biome.json index 82c7743..89eb673 100644 --- a/biome.json +++ b/biome.json @@ -20,6 +20,9 @@ "recommended": true, "noDelete": "off" }, + "suspicious": { + "noExplicitAny": "warn" + }, "complexity": { "useLiteralKeys": "off" } diff --git a/db.png b/db.png new file mode 100644 index 0000000..16ef007 Binary files /dev/null and b/db.png differ diff --git a/src/services/contents/comments.service.ts b/src/services/contents/comments.service.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/services/contents/posts.service.ts b/src/services/contents/posts.service.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/services/databases/mariadb.service.ts b/src/services/databases/mariadb.service.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/services/databases/mongodb.service.ts b/src/services/databases/mongodb.service.ts new file mode 100644 index 0000000..90b6f75 --- /dev/null +++ b/src/services/databases/mongodb.service.ts @@ -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"); + }); + } +} diff --git a/src/utils/env.util.ts b/src/utils/env.util.ts new file mode 100644 index 0000000..ecbf893 --- /dev/null +++ b/src/utils/env.util.ts @@ -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; + } +} diff --git a/src/utils/logs.util.ts b/src/utils/logs.util.ts index 9fd9e7a..2e976f7 100644 --- a/src/utils/logs.util.ts +++ b/src/utils/logs.util.ts @@ -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; 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" : ""}`, + ); } -} \ No newline at end of file +}