migrate-mongodb-og: MongoDB Migration Tool (Fork for MongoDB 7+)

11.0.1 · active · verified Wed Apr 22

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`.

Common errors

Warnings

Install

Imports

Quickstart

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.

const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

const projectDir = 'albums-migrations-quickstart';
const configFilePath = path.join(projectDir, 'migrate-mongo-config.js');
const migrationsDir = path.join(projectDir, 'migrations');

// Clean up previous runs if any
if (fs.existsSync(projectDir)) {
  fs.rmSync(projectDir, { recursive: true, force: true });
}

console.log(`Creating project directory: ${projectDir}`);
execSync(`mkdir ${projectDir} && cd ${projectDir} && npx migrate-mongodb-og init`, { stdio: 'inherit' });

// Read the generated config file
let configContent = fs.readFileSync(configFilePath, 'utf8');

// Update the MongoDB URL and database name with environment variables
configContent = configContent.replace(
  'url: "mongodb://localhost:27017"',
  `url: process.env.MONGODB_URL ?? "mongodb://localhost:27017"`
);
configContent = configContent.replace(
  'databaseName: "YOURDATABASENAME"',
  `databaseName: process.env.MONGODB_DB_NAME ?? "quickstartdb"`
);
// Remove useNewUrlParser if it causes issues with newer drivers, as it's deprecated
configContent = configContent.replace('useNewUrlParser: true', '// useNewUrlParser: true');
// Also remove useUnifiedTopology which is also deprecated
configContent = configContent.replace('useUnifiedTopology: true', '// useUnifiedTopology: true');

fs.writeFileSync(configFilePath, configContent);

console.log(`Updated configuration in ${configFilePath}`);
console.log(`Creating a sample migration...`);
execSync(`cd ${projectDir} && npx migrate-mongodb-og create first-migration`, { stdio: 'inherit' });

// Add content to the created migration file
const migrationFileName = fs.readdirSync(migrationsDir).find(f => f.includes('first-migration'));
if (migrationFileName) {
  const migrationFilePath = path.join(migrationsDir, migrationFileName);
  const migrationContent = `
module.exports = {
  async up(db, client) {
    // TODO write your migration here.
    // Example: Insert a document into a collection
    console.log('Running up migration: first-migration');
    await db.collection('users').insertOne({ name: 'Test User', createdAt: new Date() });
  },

  async down(db, client) {
    // TODO write the way to undo your migration (if necessary).
    // Example: Remove the document inserted in 'up'
    console.log('Running down migration: first-migration');
    await db.collection('users').deleteOne({ name: 'Test User' });
  }
};
`;
  fs.writeFileSync(migrationFilePath, migrationContent);
  console.log(`Added content to migration file: ${migrationFileName}`);
}

console.log("Quickstart complete. To run migrations: cd " + projectDir + " && npx migrate-mongodb-og up");
console.log("Set MONGODB_URL and MONGODB_DB_NAME environment variables for your database connection.");

view raw JSON →