UNPKG

umbot

Version:

Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber

93 lines (92 loc) 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DB = void 0; const mmApp_1 = require("../../mmApp"); const mongodb_1 = require("mongodb"); class DB { sql; dbConnect; errors; params; constructor() { this.sql = null; this.errors = []; this.params = null; this.dbConnect = null; } async isConnected() { try { if (!this.sql) { return false; } await this.sql.db().admin().ping(); return true; } catch (err) { this.errors.push(err.message); return false; } } async connect() { this.errors = []; if (this.params) { this.close(); try { const options = { timeoutMS: 3000, serverSelectionTimeoutMS: 2000, connectTimeoutMS: 2000, socketTimeoutMS: 2000, maxPoolSize: 1, ...mmApp_1.mmApp.config.db?.options, serverApi: { version: mongodb_1.ServerApiVersion.v1, strict: true, deprecationErrors: true, ...mmApp_1.mmApp.config.db?.options?.serverApi, }, }; if (this.params.user) { options.auth = { username: this.params.user, password: this.params.pass, }; } this.sql = new mongodb_1.MongoClient(this.params.host, options); this.dbConnect = this.sql.connect(); await this.dbConnect; const isConnected = await this.isConnected(); if (!isConnected) { throw new Error('Failed to verify database connection'); } return true; } catch (err) { this.errors.push(err.message); this.dbConnect = null; this.sql = null; return false; } } else { this.errors.push('Отсутствуют данные для подключения!'); } return false; } async close() { if (this.sql) { try { await this.sql.close(); } catch (err) { this.errors.push(err.message); } this.sql = null; this.dbConnect = null; } } async destroy() { await this.close(); } } exports.DB = DB;