bridge-node-mongo-migration v2.0.0
bridge-node-mongo-migration
A Node.JS Promised based library intends to provide a migration framework based on MongoDb.
TODO
[] Test with mongodb replicasSet management
How it works
This library manage the following cases:
- Do not run already run migrations (based on mongoDb store)
- Alert on altered already run migrations
- Locking feature while processing (based on mongoDb store)
- Rollback management (Up/Down)
- Conditional migration on custom condition
Migration Process
- Acquire the lock (if not just do nothing)
- List all migrations in the migration folder (order based on natural file sort)
- Filter
Not Runnablemigrations (custom condition filtering) - Filter already run migrations
- Run sequentially all pending migrations
- If an error occurred while running a migration rollback all newly run migrations (including the failed migration)
Installation
npm i bridge-node-mongo-migration
Usage
This library is a Promise based library
Import this package
import {Configuration, Runner} from 'bridge-node-mongo-migration';
You need to provide a configuration.
The configuration Object needs the following keys :
mongo: Global mongo client configurationurl: Mongo connection URI string (http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html)database: Mongo database to useclientOptions: Mongo client options. See Official mongo documentation for futher details
- optional
migrationCollectionName: The name of the MongoDB collection to track already ran migrations, default to_migrations - optional
lockingCollectionName: The name of the MongoDB collection used during migration process, default to_migration_lock - optional
log: Custom logging function with this signature (level: string, message: string, payload: any) => void, default none
Create your configuration
Then, instantiate migration runner :
const runner = new Runner(config, <directory to scan>);
The second parameter is the directory that store your migrations scripts (relative to your working directory)
Define your migrations.
Each migrations must be defined in a specific folder (according to your configuration object).
You must define ONE file per migration. This file should :
- Respect the following naming convention :
{orderNumber}_{name}which is resolved by the following regex - Export a
defaultclass that extends theMigrationbase class Available withimport {Migration} from 'bridge-node-mongo-migration';
A migration could be ignored on custom condition, to achieve that you need to override isRunnable(db: Db): Promise<boolean> migration method.
A migration could be run each times you start a migration process (useful for seeds).
To achieve this you need to override alwaysRun(db: Db): Promise<boolean> method on your Migration class.
And finally run the migration process with :
runner.executePendingMigrations();