UNPKG

@devcodes-sdk/mongo-clone

Version:

@devcodes-sdk/mongo-clone is a powerful CLI tool that simplifies cloning MongoDB databases. With a single command, you can easily replicate collections and documents across databases using npx.

80 lines (79 loc) 3.76 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const mongodb_1 = require("mongodb"); const commander_1 = require("commander"); const cli_progress_1 = __importDefault(require("cli-progress")); const logger_1 = require("./utils/logger"); const program = new commander_1.Command(); const mongoRegex = /^mongodb:\/\/([a-zA-Z0-9-_]+):([a-zA-Z0-9-_]+)@([a-zA-Z0-9.-]+):(\d+)\/([a-zA-Z0-9-_]+)$/; program .usage("-s <SOURCE_MONGO_DB_URL> -d <DEST_MONGO_DB_URL>") .option("-s, --source <uri>", "Source MongoDB URI") .option("-d, --destination <uri>", "Destination MongoDB URI") .action((options) => { if (!options.source || !options.destination) { logger_1.logger.error("Missing required options"); logger_1.logger.info("USAGE: mongo-clone -s <SOURCE_MONGO_DB_URL> -d <DEST_MONGO_DB_URL>"); logger_1.logger.info("MongoURL: mongodb://USER:PASS@HOST:PORT/DBNAME"); process.exit(1); } if (!mongoRegex.test(options.source) || !mongoRegex.test(options.destination)) { logger_1.logger.error("Invalid MongoDB URI"); logger_1.logger.info("MongoURL: mongodb://USER:PASS@HOST:PORT/DBNAME"); process.exit(1); } }) .parse(process.argv); const { source, destination } = program.opts(); async function cloneDatabase(sourceUri, destinationUri) { const sourceClient = new mongodb_1.MongoClient(parseMongoConnectionString(sourceUri).url); const destinationClient = new mongodb_1.MongoClient(parseMongoConnectionString(destinationUri).url); try { logger_1.logger.info("Connecting to the source database..."); await sourceClient.connect(); const sourceDb = sourceClient.db(parseMongoConnectionString(sourceUri).name); logger_1.logger.info("Fetching collections..."); const collections = await sourceDb.listCollections().toArray(); logger_1.logger.info("Connecting to the destination database..."); await destinationClient.connect(); const destinationDb = destinationClient.db(parseMongoConnectionString(destinationUri).name); const progressBar = new cli_progress_1.default.Bar({ format: "Cloning Progress | {bar} | {percentage}% | {value}/{total} collections", }, cli_progress_1.default.Presets.rect); progressBar.start(collections.length, 0); for (const [index, { name: collectionName }] of collections.entries()) { const sourceCollection = sourceDb.collection(collectionName); const destinationCollection = destinationDb.collection(collectionName); const docs = await sourceCollection.find().toArray(); if (docs.length) { await destinationCollection.insertMany(docs); } progressBar.update(index + 1); } progressBar.stop(); logger_1.logger.success("Database cloned successfully!"); } catch (error) { logger_1.logger.error("Error: ", error.message); process.exit(1); } finally { await sourceClient.close(); await destinationClient.close(); } } function parseMongoConnectionString(connectionString) { const urlEndIndex = connectionString.lastIndexOf("/"); if (urlEndIndex === -1 || urlEndIndex === connectionString.length - 1) { throw new Error("Connection string invalid sau lipsește numele bazei de date."); } const url = connectionString.substring(0, urlEndIndex); const name = connectionString.substring(urlEndIndex + 1); return { url, name }; } cloneDatabase(source, destination);