UNPKG

@sigiljs-community/mongo-plugin

Version:

Plugin for SigilJS framework that provides MongoDB interactions

2 lines (1 loc) 1.2 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});class s{constructor(t,n,e){this.client=t,this._dbName=n,this.log=e}_database;_connectionStatus=1;get connectionStatus(){return this._connectionStatus}get databaseName(){return this._dbName}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.client.close().catch(()=>null)===null?!1:(this._connectionStatus=1,this.log({level:"info",message:`Connection to database ${this._dbName} closed`,json:{milestone:"disconnect",ok:!0}}),!0)}async reconnect(){if(this._connectionStatus===0)return!0;const t=performance.now();if(!await this.client.connect().catch(()=>null))return!1;this._database=this.client.db(this._dbName);const e=performance.now()-t;return this._connectionStatus=0,this.log({level:"info",message:`Connection to database ${this._dbName} established in ${e}ms`,json:{milestone:"connect",ok:!0,time:e,db:this._dbName}}),!0}collection(t){return this.checkConnection(),this.unsafeDB.collection(t)}}exports.default=s;