{"library":"migrate-mongodb-og","title":"migrate-mongodb-og: MongoDB Migration Tool (Fork for MongoDB 7+)","description":"This package, `migrate-mongodb-og`, is a critical fork of the popular `migrate-mongo` database migration tool designed for Node.js environments. It provides a robust CLI and programmatic API for managing schema changes in MongoDB databases. The current stable version, 11.0.1, specifically addresses compatibility challenges by supporting a broad range of MongoDB driver versions, including `^4.4.1`, `^5.0.0`, `^6.0.0`, and notably `^7.0.0`. Its key differentiator is its explicit support for MongoDB 7 and beyond, filling a gap left by the original project. While there isn't a strict release cadence mentioned, as a fork, it typically updates to maintain compatibility with new MongoDB versions and to integrate relevant bug fixes or features from the upstream project. It enables developers to define migration scripts in JavaScript, offering functions to bring the database schema `up` or `down`, create new migration files, and check `status`.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install migrate-mongodb-og"],"cli":{"name":"migrate-mongo","version":null}},"imports":["const migrateMongo = require('migrate-mongodb-og');","import migrateMongo from 'migrate-mongodb-og';","const config = require('./migrate-mongo-config');","const { db, client } = await migrateMongo.database.connect();"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const fs = require('fs');\nconst path = require('path');\nconst { execSync } = require('child_process');\n\nconst projectDir = 'albums-migrations-quickstart';\nconst configFilePath = path.join(projectDir, 'migrate-mongo-config.js');\nconst migrationsDir = path.join(projectDir, 'migrations');\n\n// Clean up previous runs if any\nif (fs.existsSync(projectDir)) {\n  fs.rmSync(projectDir, { recursive: true, force: true });\n}\n\nconsole.log(`Creating project directory: ${projectDir}`);\nexecSync(`mkdir ${projectDir} && cd ${projectDir} && npx migrate-mongodb-og init`, { stdio: 'inherit' });\n\n// Read the generated config file\nlet configContent = fs.readFileSync(configFilePath, 'utf8');\n\n// Update the MongoDB URL and database name with environment variables\nconfigContent = configContent.replace(\n  'url: \"mongodb://localhost:27017\"',\n  `url: process.env.MONGODB_URL ?? \"mongodb://localhost:27017\"`\n);\nconfigContent = configContent.replace(\n  'databaseName: \"YOURDATABASENAME\"',\n  `databaseName: process.env.MONGODB_DB_NAME ?? \"quickstartdb\"`\n);\n// Remove useNewUrlParser if it causes issues with newer drivers, as it's deprecated\nconfigContent = configContent.replace('useNewUrlParser: true', '// useNewUrlParser: true');\n// Also remove useUnifiedTopology which is also deprecated\nconfigContent = configContent.replace('useUnifiedTopology: true', '// useUnifiedTopology: true');\n\nfs.writeFileSync(configFilePath, configContent);\n\nconsole.log(`Updated configuration in ${configFilePath}`);\nconsole.log(`Creating a sample migration...`);\nexecSync(`cd ${projectDir} && npx migrate-mongodb-og create first-migration`, { stdio: 'inherit' });\n\n// Add content to the created migration file\nconst migrationFileName = fs.readdirSync(migrationsDir).find(f => f.includes('first-migration'));\nif (migrationFileName) {\n  const migrationFilePath = path.join(migrationsDir, migrationFileName);\n  const migrationContent = `\nmodule.exports = {\n  async up(db, client) {\n    // TODO write your migration here.\n    // Example: Insert a document into a collection\n    console.log('Running up migration: first-migration');\n    await db.collection('users').insertOne({ name: 'Test User', createdAt: new Date() });\n  },\n\n  async down(db, client) {\n    // TODO write the way to undo your migration (if necessary).\n    // Example: Remove the document inserted in 'up'\n    console.log('Running down migration: first-migration');\n    await db.collection('users').deleteOne({ name: 'Test User' });\n  }\n};\n`;\n  fs.writeFileSync(migrationFilePath, migrationContent);\n  console.log(`Added content to migration file: ${migrationFileName}`);\n}\n\nconsole.log(\"Quickstart complete. To run migrations: cd \" + projectDir + \" && npx migrate-mongodb-og up\");\nconsole.log(\"Set MONGODB_URL and MONGODB_DB_NAME environment variables for your database connection.\");","lang":"javascript","description":"This quickstart demonstrates how to initialize a migration project, configure the connection to MongoDB using environment variables, and create a basic migration file with `up` and `down` functions.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}