Registry / database / migrate-mongodb-og

migrate-mongodb-og

JSON →
library11.0.1jsnpmunverified

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

npm install migrate-mongodb-og
INSTALL
IMPORT
SIG · MIGRATE-MONGODB-OG
M
migrate-mongodb-og
databasejavascriptv11.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

migrateMongo
const migrateMongo = require('migrate-mongodb-og');
import { up, down } from 'migrate-mongodb-og';
The library's primary programmatic interface is a CommonJS module exporting a single object containing `up`, `down`, `create`, `status`, `init`, and `database` methods. Named imports are generally not supported at the top-level.
migrateMongo (ESM)
import migrateMongo from 'migrate-mongodb-og';
import { up, down } from 'migrate-mongodb-og';
For ESM projects, use a default import. Ensure `type: 'module'` is set in `package.json` or use a `.mjs` file extension. Named imports for individual functions are generally not supported at the top-level.
config
const config = require('./migrate-mongo-config');
import { config } from 'migrate-mongodb-og';
The configuration object is typically loaded from a local `migrate-mongo-config.js` file created by `migrate-mongo init`. This object is then passed to `migrateMongo.config.set()` before running migrations programmatically.
db, client
const { db, client } = await migrateMongo.database.connect();
The `database` utility, available via `migrateMongo.database`, provides a `connect()` method that returns the native `mongodb.Db` and `mongodb.MongoClient` instances for use in migration scripts. These are typically passed to `up` and `down` functions.

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.");
migrate-mongo --version
Debug
Known issues
breakingThis package (`migrate-mongodb-og`) is a fork of the original `migrate-mongo`. Existing projects using `migrate-mongo` must update their `package.json` and import paths to switch to this fork.
fix
Run `npm uninstall migrate-mongo` then `npm install migrate-mongodb-og`. Update all `require()` or `import` statements from `'migrate-mongo'` to `'migrate-mongodb-og'`.
affects: >=1.0.0
gotchaThe package has peer dependencies on `mongodb` driver versions `^4.4.1 || ^5.0.0 || ^6.0.0 || ^7.0.0`. Ensure your project's `mongodb` dependency matches one of these versions to avoid compatibility issues.
fix
Check your `package.json` for the `mongodb` dependency and update it to a compatible version, e.g., `npm install mongodb@^7`.
affects: >=11.0.0
deprecatedMongoDB connection options like `useNewUrlParser: true` and `useUnifiedTopology: true` are deprecated and no longer needed (and may cause warnings) with `mongodb` driver versions 4.x and higher. They should be removed from `migrate-mongo-config.js`.
fix
Remove `useNewUrlParser: true` and `useUnifiedTopology: true` from the `options` object within your `migrate-mongo-config.js` file.
affects: >=11.0.0
gotchaThe `migrate-mongo-config.js` file requires either a `url` property with the database name encoded, or both `url` and `databaseName` properties. Misconfiguration here is a common source of connection errors.
fix
Carefully review the `mongodb` section in `migrate-mongo-config.js`. Ensure `url` points to your MongoDB instance and either includes the database name (e.g., `mongodb://localhost:27017/MYDB`) or `databaseName` is explicitly set.
affects: >=1.0.0
Errors
Common errors & fixes
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
The MongoDB server is not running or the connection URL in `migrate-mongo-config.js` is incorrect.
fix
Ensure your MongoDB server is running. Verify the `url` in `migrate-mongo-config.js` matches your MongoDB instance's address and port.
Error: EACCES: permission denied, mkdir 'migrations'
The command `migrate-mongo init` or `migrate-mongo create` was run in a directory where the user lacks write permissions to create the `migrations` folder.
fix
Run the command from a directory where your user has write permissions, or use `sudo` (not recommended for general use) or fix directory permissions.
The 'url' or 'databaseName' property is missing or empty in the config.
The `migrate-mongo-config.js` file is missing the required MongoDB connection details.
fix
Edit `migrate-mongo-config.js` and provide a valid `url` and `databaseName` within the `mongodb` object. Remember `databaseName` can also be part of the `url` string.
TypeError: migrateMongo.up is not a function
Attempting to use named imports for functions (`import { up } from 'migrate-mongodb-og';`) when the module primarily exports a single object with methods, or using an incorrect CommonJS destructuring.
fix
For CommonJS, use `const migrateMongo = require('migrate-mongodb-og');` then `migrateMongo.up()`. For ESM, use `import migrateMongo from 'migrate-mongodb-og';` then `migrateMongo.up()`.
Error: Migration "20230101000000-my-migration.js" already applied, but its content has changed.
The `useFileHash` option is enabled in `migrate-mongo-config.js`, and a migration file was modified after it was already applied to the database.
fix
Either revert the changes to the migration file to its original state, or, if the changes are intentional and idempotent, set `useFileHash: false` in `migrate-mongo-config.js` (use with caution), or create a new migration for the additional changes.
Upgrade
Version history
11.0.1latest on npm
Audit
Dependencies
mongodbrequiredCore driver for interacting with MongoDB. Required as a peer dependency for database operations.
Agent activity
9 hits · last 30 days
node
8
Resources
migrate-mongodb-og — npm install migrate-mongodb-og · libregistry