UNPKG

@kitapp-developers/mongo-import-export

Version:

A modern CLI tool to import/export MongoDB collections to and from JSON or CSV — great for backups, migrations, and local development.

32 lines (31 loc) 996 B
import { MongoClient } from 'mongodb'; import { logger } from '../utils/logger.js'; import { config } from '../config.js'; export class MongoDBClient { constructor(uri) { this.client = new MongoClient(uri || config.mongo.uri); } async connect() { await this.client.connect(); logger.info('Connected to MongoDB'); const db = this.client.db(config.mongo.dbName); return db; } async listDatabases() { try { const adminDb = this.client.db('admin').admin(); const dbs = await adminDb.listDatabases(); return dbs.databases .map((db) => db.name) .filter(name => !['admin', 'local', 'config'].includes(name)); } catch (error) { logger.error(`Failed to list databases: ${error.message}`); return []; } } async close() { await this.client.close(); logger.info('MongoDB connection closed'); } }