East MongoDB Adapter
east-mongo is an adapter for the `east` Node.js database migration tool, specifically designed for MongoDB. It leverages the official `mongodb` native driver (peer dependency on versions 2.x.x or 3.x.x) to manage schema changes and data migrations. When migrations are executed, their names are recorded in a `_migrations` collection within the target database. The adapter provides helper functions like `dropIndexIfExists` and includes template options for promises-based or async/await migrations. This package, currently at version 1.0.0, appears to be abandoned, with no releases or significant activity since 2017, meaning it is not actively maintained and may have unpatched vulnerabilities or compatibility issues with modern MongoDB versions or Node.js runtimes. Its primary function is to provide the `db` instance from the native driver to migration scripts.
Common errors
-
Error: Cannot find module 'mongodb'
cause The 'mongodb' package, a peer dependency, was not installed alongside 'east-mongo'.fixInstall the peer dependency: `npm install mongodb@^3` (or `mongodb@^2` if preferred) in your project. -
Error: Adapter 'east-mongo' not found. Check your .eastrc configuration.
cause The 'east-mongo' package itself is not installed, or the 'adapter' path in your `.eastrc` file is incorrect.fixEnsure 'east-mongo' is installed (`npm install east-mongo`) and that your `.eastrc` has `"adapter": "east-mongo"`. -
MongoError: Topology was destroyed
cause This often occurs with older MongoDB drivers when connection options or lifecycle management are not handled robustly, or when the MongoDB server is unavailable/restarts during operations.fixVerify your MongoDB connection URL and options in `.eastrc`. Ensure the MongoDB server is running and accessible. Consider adding more robust error handling and connection retry logic in your migration scripts, though the underlying driver version may be a limiting factor.
Warnings
- breaking This package is specifically designed for `east` version 1.x and `mongodb` native driver versions 2.x or 3.x. Using it with `east` versions prior to 1.x, or `mongodb` versions 4.x or newer, will likely lead to compatibility issues or errors.
- breaking The `east-mongo` package is abandoned, with no updates or activity since 2017. This means it will not receive bug fixes, security patches, or compatibility updates for newer Node.js versions, MongoDB server versions, or `mongodb` driver updates.
- gotcha Node.js compatibility is limited to versions 4.0.0 and above. Running `east-mongo` on modern Node.js versions (e.g., Node.js 14, 16, 18, 20+) has not been tested and may encounter unexpected behavior or errors due to breaking changes in Node.js core.
- gotcha The package's Snyk badge indicates 'Known Vulnerabilities'. Given its abandonment, these vulnerabilities are likely unpatched, posing a security risk to applications using this package.
Install
-
npm install east-mongo -
yarn add east-mongo -
pnpm add east-mongo
Imports
- Adapter Configuration
{ "adapter": "east-mongo" } - Async Migration Template
import { async } from 'east-mongo/lib/migrationTemplates/async.js'require.resolve('east-mongo/lib/migrationTemplates/async.js') - Promises Migration Template (Default)
// No explicit import needed if 'template' is not set in .eastrc
Quickstart
/* .eastrc file */
module.exports = {
"adapter": "east-mongo",
"url": "mongodb://localhost:27017/my_database",
"options": {
"server": {
"socketOptions": {
"socketTimeoutMS": 3600000
}
}
}
};
/* Example migration file (e.g., 20230101000000-initial-setup.js) */
exports.tags = [];
exports.migrate = async function(params) {
const { db, dropIndexIfExists } = params;
// Example: Create a collection and an index
await db.collection('users').createIndex({ email: 1 }, { unique: true });
console.log('Created unique index on users.email');
// Example: Insert some initial data
await db.collection('settings').insertOne({ key: 'appVersion', value: '1.0.0' });
console.log('Inserted initial app version setting');
return Promise.resolve();
};
exports.rollback = async function(params) {
const { db, dropIndexIfExists } = params;
// Example: Rollback by dropping the collection and index
await dropIndexIfExists('users', 'email_1'); // 'email_1' is the default index name
await db.collection('users').drop();
console.log('Dropped users collection and index');
await db.collection('settings').deleteOne({ key: 'appVersion' });
console.log('Removed app version setting');
return Promise.resolve();
};