UNPKG

vmagic

Version:

vMagic is a RESTFul framework for NodeJS applications.

126 lines (98 loc) 3.87 kB
/*eslint max-statements: ["error", 100], max-depth: ["error", 10], class-methods-use-this: ["error", { "exceptMethods": ["_getWhereSQL","_getSelectSQL"] }] */ "use strict"; import mongodb, { ObjectId } from "mongodb"; const { MongoClient } = mongodb; import Logger from "../Logger.js"; class MongoDB { constructor(dbConfig) { this.dbConfig = dbConfig; this.logger = new Logger(); } async createPool() { const user = encodeURIComponent(this.dbConfig.user || ""); const password = encodeURIComponent(this.dbConfig.password || ""); let url = null; if (this.dbConfig.user) { url = `mongodb://${user}:${password}@${this.dbConfig.host}:${this.dbConfig.port}?authMechanism=DEFAULT`; } else { url = `mongodb://${this.dbConfig.host}:${this.dbConfig.port}`; } if (!Reflect.has(this.dbConfig, "options")) { throw new Error("The datasource needs the options property."); } try { const client = new MongoClient(url, this.dbConfig.options); const connection = await client.connect(); this.db = connection.db(this.dbConfig.database); this.client = connection; this.logger.success("[Magic] MongoDB connection established successfully."); } catch (err) { this.logger.error("[Magic] MongoDB connection was not established."); throw err; } } save(collectionName, data) { const collection = this.db.collection(collectionName); return collection.insertOne(data); } async saveMany(collectionName, data) { const collection = this.db.collection(collectionName); const res = await collection.insertMany(data); const rows = res.ops; const insertedCount = res.insertedCount; const insertedIds = res.insertedIds; return { rows, insertedCount, insertedIds }; } update(collectionName, data, conditions) { const collection = this.db.collection(collectionName); return collection.updateOne(conditions, { $set: data }); } delete(collectionName, conditions) { const collection = this.db.collection(collectionName); return collection.deleteOne(conditions); } deleteMany(collectionName, conditions) { const collection = this.db.collection(collectionName); return collection.deleteMany(conditions); } async findBy(collectionName, conditions) { const collection = this.db.collection(collectionName); const cursor = collection.find(conditions); const rows = await cursor.toArray(); return { rows }; } async findAll(collectionName, params) { const collection = this.db.collection(collectionName); let cursor = null; if (!params.conditions) { throw new Error("Please, sets the conditions."); } cursor = collection.find(params.conditions); if (Reflect.has(params, "fields")) { cursor = cursor.project(params.fields); } if (Reflect.has(params, "order")) { cursor = cursor.sort(params.order); } if (Reflect.has(params, "limit")) { cursor = cursor.limit(params.limit); } const rows = await cursor.toArray(); return { rows }; } objectId(_id) { try { return ObjectId.createFromHexString(_id); } catch (err) { this.logger.error("Invalid ObjectId format:", err); throw new Error("Invalid ObjectId format"); } } collection(collectionName) { if (!collectionName) { throw new Error("collectionName is required."); } return this.db.collection(collectionName); } } export default MongoDB;