UNPKG

men-pack

Version:

men-pack is a collection of funcationalities that can helps developers to easily develop MongoDB Express Node Applications/Backend

46 lines (45 loc) 1.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MongoDBClient = void 0; const mongodb_1 = require("mongodb"); class MongoDBClient { constructor() { this.client = null; } async connect(uri) { this.client = new mongodb_1.MongoClient(uri); await this.client.connect(); console.log("Connected to MongoDB"); } async disconnect() { if (this.client) { await this.client.close(); console.log("Disconnected from MongoDB"); } } async create(collection, data) { if (!this.client) throw new Error("Database not connected"); const db = this.client.db(); return await db.collection(collection).insertOne(data); } async read(collection, query) { if (!this.client) throw new Error("Database not connected"); const db = this.client.db(); return await db.collection(collection).find(query).toArray(); } async update(collection, query, updates) { if (!this.client) throw new Error("Database not connected"); const db = this.client.db(); return await db.collection(collection).updateMany(query, { $set: updates }); } async delete(collection, query) { if (!this.client) throw new Error("Database not connected"); const db = this.client.db(); return await db.collection(collection).deleteMany(query); } } exports.MongoDBClient = MongoDBClient;