From d84c07211adcd8243441ffaf3c0595edabafa806 Mon Sep 17 00:00:00 2001 From: StarAppeal Date: Fri, 22 Nov 2024 16:58:44 +0100 Subject: [PATCH] small refactor --- package-lock.json | 28 ++++++++ package.json | 3 +- src/db/services/database.service.ts | 102 ++++++++++++++-------------- 3 files changed, 81 insertions(+), 52 deletions(-) diff --git a/package-lock.json b/package-lock.json index 44c5f29..ac28f19 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,6 +24,7 @@ }, "devDependencies": { "@types/cors": "^2.8.17", + "cross-env": "^7.0.3", "prettier": "^3.2.5" } }, @@ -427,6 +428,24 @@ "node": ">= 0.10" } }, + "node_modules/cross-env": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz", + "integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==", + "dev": true, + "dependencies": { + "cross-spawn": "^7.0.1" + }, + "bin": { + "cross-env": "src/bin/cross-env.js", + "cross-env-shell": "src/bin/cross-env-shell.js" + }, + "engines": { + "node": ">=10.14", + "npm": ">=6", + "yarn": ">=1" + } + }, "node_modules/cross-spawn": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", @@ -2217,6 +2236,15 @@ "vary": "^1" } }, + "cross-env": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz", + "integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==", + "dev": true, + "requires": { + "cross-spawn": "^7.0.1" + } + }, "cross-spawn": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", diff --git a/package.json b/package.json index a78b925..d584955 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "main": "index.js", "scripts": { "start": "node dist/index.js", - "start-local": "tsc && NODE_ENV=development node dist/index.js", + "start-local": "tsc && cross-env NODE_ENV=development node dist/index.js", "clean": "rimraf dist", "build": "npm run clean & tsc", "test": "echo \"Error: no test specified, please add them later\"" @@ -29,6 +29,7 @@ }, "devDependencies": { "@types/cors": "^2.8.17", + "cross-env": "^7.0.3", "prettier": "^3.2.5" } } diff --git a/src/db/services/database.service.ts b/src/db/services/database.service.ts index 22507a0..65da213 100644 --- a/src/db/services/database.service.ts +++ b/src/db/services/database.service.ts @@ -1,68 +1,68 @@ import "dotenv/config"; import * as mongoDB from "mongodb"; -import { ObjectId, ReturnDocument } from "mongodb"; +import {ObjectId, ReturnDocument} from "mongodb"; import User from "../models/user"; let mongoDb: mongoDB.Db; async function getDatabase(): Promise { - if (mongoDb) { + if (mongoDb) { + return mongoDb; + } + const client = new mongoDB.MongoClient(process.env.DB_CONN_STRING!); + await client.connect(); + mongoDb = client.db(process.env.DB_NAME!); return mongoDb; - } - const client = new mongoDB.MongoClient(process.env.DB_CONN_STRING!); - await client.connect(); - mongoDb = client.db(process.env.DB_NAME!); - return mongoDb; } export class UserService { - private readonly collection: mongoDB.Collection; - private static _instance: UserService; + private static _instance: UserService; + private readonly collection: mongoDB.Collection; - private constructor(db: mongoDB.Db) { - this.collection = db.collection(process.env.USER_COLLECTION_NAME!); - } - - public static async create(): Promise { - if (this._instance) { - return this._instance; + private constructor(db: mongoDB.Db) { + this.collection = db.collection(process.env.USER_COLLECTION_NAME!); } - const db = await getDatabase(); - this._instance = new UserService(db); - return this._instance; - } - public async updateUser(id: string, user: User) { - return (await this.executeWithExceptionHandling(() => { - return this.collection.findOneAndUpdate( - { _id: new ObjectId(id) }, - { $set: user }, - { returnDocument: ReturnDocument.AFTER }, - ); - })) as unknown as User; - } - - public async getAllUsers() { - return (await this.executeWithExceptionHandling(() => { - return this.collection.find().toArray(); - })) as unknown as User[]; - } - - async getUserById(id: string) { - return (await this.executeWithExceptionHandling(() => { - return this.collection.findOne({ - _id: new ObjectId(id), - }); - })) as unknown as User; - } - - private async executeWithExceptionHandling(operation: () => Promise) { - try { - return await operation(); - } catch (e) { - console.error(e); - return null; + public static async create(): Promise { + if (this._instance) { + return this._instance; + } + const db = await getDatabase(); + this._instance = new UserService(db); + return this._instance; + } + + public async updateUser(id: string, user: User): Promise { + return await this.executeWithExceptionHandling(() => { + return this.collection.findOneAndUpdate( + {_id: new ObjectId(id)}, + {$set: user}, + {returnDocument: ReturnDocument.AFTER} + ); + }) as unknown as User; + } + + public async getAllUsers() { + return await this.executeWithExceptionHandling(() => { + return this.collection.find({}).toArray(); + }); + } + + async getUserById(id: string) { + return (await this.executeWithExceptionHandling(() => { + return this.collection.findOne({ + _id: new ObjectId(id), + }); + })); + } + + private async executeWithExceptionHandling(operation: () => Promise) { + try { + return await operation(); + } catch (e) { + console.error(e); + return null; + } } - } }