@kurai-io/mongo
Version:
Abstraction layer for Mongo database
2 lines (1 loc) • 944 B
JavaScript
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});class e{constructor(t,n){this.mongoClient=t,this.dbName=n}_database;_connectionStatus=1;get connectionStatus(){return this._connectionStatus}get databaseName(){return this.mongoClient.db.name||null}get db(){return this._database}get unsafeDB(){return this._database}checkConnection(){if(this._connectionStatus!==0)throw new Error("Not connected to database")}async disconnect(){return this._connectionStatus===1?!0:await this.mongoClient.close().catch(()=>null)===null?!1:(this._connectionStatus=1,!0)}async connect(){if(this._connectionStatus===0)return this;if(!await this.mongoClient.connect().catch(()=>null))throw new Error("Cannot connect to database");return this._database=this.mongoClient.db(this.dbName),this._connectionStatus=0,this}collection(t){return this.checkConnection(),this.unsafeDB.collection(t)}}exports.default=e;