small refactor
This commit is contained in:
Generated
+28
@@ -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",
|
||||
|
||||
+2
-1
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<mongoDB.Db> {
|
||||
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<UserService> {
|
||||
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<T>(operation: () => Promise<T>) {
|
||||
try {
|
||||
return await operation();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return null;
|
||||
public static async create(): Promise<UserService> {
|
||||
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<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<User>({}).toArray();
|
||||
});
|
||||
}
|
||||
|
||||
async getUserById(id: string) {
|
||||
return (await this.executeWithExceptionHandling(() => {
|
||||
return this.collection.findOne<User>({
|
||||
_id: new ObjectId(id),
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
private async executeWithExceptionHandling<T>(operation: () => Promise<T>) {
|
||||
try {
|
||||
return await operation();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user