UNPKG

@laiyon/create-wasapi

Version:

CLI to create WhatsApp bot projects with Wasapi and BuilderBot

82 lines (81 loc) 2.21 kB
export class MongoDBProvider { createDatabase(config) { return new MongoDBDatabase(config); } getRequiredEnvVars() { return ["MONGODB_URI"]; } validateConfig(config) { return !!(config.url); } // Optimized method for quick validation async validateConnection(config) { let client = null; try { // Dynamic import - only load mongodb when needed const { MongoClient } = await import("mongodb"); client = new MongoClient(config.url, { serverSelectionTimeoutMS: 5000, connectTimeoutMS: 5000 }); await client.connect(); await client.db().admin().ping(); return true; } catch (error) { return false; } finally { if (client) { await client.close(); } } } } class MongoDBDatabase { constructor(config) { this.client = null; this.connected = false; this.config = config; } async connect() { try { // Dynamic import - only load mongodb when needed const { MongoClient } = await import("mongodb"); this.client = new MongoClient(this.config.url); await this.client.connect(); this.connected = true; console.log(`✅ Successfully connected to MongoDB`); } catch (error) { this.connected = false; throw new Error(`Error connecting to MongoDB: ${error}`); } } async disconnect() { if (this.client) { await this.client.close(); this.client = null; } this.connected = false; console.log("🔌 Disconnected from MongoDB"); } isConnected() { return this.connected; } getConfig() { return { ...this.config }; } async testConnection() { try { if (this.client) { await this.client.db().admin().ping(); return true; } return false; } catch { return false; } } }