UNPKG

rashi-discord-bot-lib

Version:

🚀 Powerful Discord bot framework with built-in database, event handling, and utilities

119 lines 4.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DatabaseManager = void 0; const mongodb_1 = require("mongodb"); const VerseDB_1 = require("../database/VerseDB"); const Logger_1 = require("../utils/Logger"); const mongo_1 = require("../utils/mongo"); class DatabaseManager { logger; verse; mongoClient; mongoDb; constructor(logger) { this.logger = logger ?? new Logger_1.Logger(); } /** Initialize configured databases and return handles */ async initialize(config) { const out = {}; // VerseDB (optional) if (config.verse) { out.versedb = await this.initVerse(config.verse); this.verse = out.versedb; } // Native MongoDB (optional) if (config.mongo) { out.mongodb = await this.initMongo(config.mongo); this.mongoDb = out.mongodb; // make globally accessible via getDb() try { (0, mongo_1.setDb)(this.mongoDb ?? null); } catch { } } return out; } /* ------------------------------- VerseDB -------------------------------- */ async initVerse(cfg) { const verse = new VerseDB_1.VerseDB(cfg, this.logger); await verse.initialize(); const adapter = cfg.adapterType; const location = adapter === 'mongo' ? `${cfg.dbName ?? 'verse'}.${cfg.collection ?? 'versedb'}` : (cfg.path ?? '(no path)'); if (cfg.secure?.enable) this.logger.info('🔒 VerseDB encryption enabled'); if (cfg.dev?.enable) this.logger.info('🔧 VerseDB development mode enabled'); return verse; } /* ------------------------------- MongoDB -------------------------------- */ async initMongo(cfg) { const uri = cfg.uri ?? cfg.mongoURI; if (!uri) throw new Error('MongoConfig.uri (or mongoURI) is required'); // Nie używamy już dbName z configu this.mongoClient = new mongodb_1.MongoClient(uri); await this.mongoClient.connect(); const db = this.mongoClient.db(); // 👈 domyślna nazwa z URI (jeśli brak, będzie 'test') await db.command({ ping: 1 }); return db; } /* ------------------------------- Getters -------------------------------- */ getVerse() { return this.verse; } getMongoDb() { return this.mongoDb; } getMongoClient() { return this.mongoClient; } /* ------------------------------ Utilities ------------------------------- */ async disconnect() { // unset global DB handle try { (0, mongo_1.setDb)(null); } catch { } if (this.verse) { await this.verse.close(); this.logger.info('🗃️ VerseDB disconnected'); } if (this.mongoClient) { await this.mongoClient.close(); this.logger.info('🍃 MongoDB disconnected'); } } async healthCheck() { const status = { verse: false, mongo: false }; try { status.verse = !!(this.verse && (await this.verse.healthCheck())); } catch { } try { if (this.mongoDb) { await this.mongoDb.command({ ping: 1 }); status.mongo = true; } } catch { } return status; } async getStats() { const stats = {}; try { if (this.verse) stats.verse = await this.verse.getStats(); if (this.mongoDb) { const db = this.mongoDb; const collections = await db.listCollections().toArray(); let totalDocs = 0; for (const c of collections) { totalDocs += await db.collection(c.name).countDocuments(); } stats.mongo = { collections: collections.length, documents: totalDocs }; } } catch (err) { this.logger.error('Failed to gather DB stats:', err); } return stats; } } exports.DatabaseManager = DatabaseManager; //# sourceMappingURL=DatabaseManager.js.map