Code Monkey home page Code Monkey logo

mongo-migrator's Introduction

mongo-migrator

Table of Contents

Note: this library was based on mongodb-migrations

Installation

  npm install mongo-migrator

Common Usage (CLI)

The package installs a single CLI executable — migrate.

When installing locally to your project this executable can be found at ./node_modules/.bin/migrate.

When installing globally the executable should automatically become accessible on your PATH.

Configuration

migrate init

The CLI app will crate a file called configMigrate.json, this file has the default configuration

{
  "migration": {
    "directory": "migrations/",
    "collection": "migrations"
  },
  "mongo": {
    "database": "test",
    "user": "mongo-user",
    "password": "mongo-password",
    "replicaSet": "replicaSet",
    "servers": [{
      "host": "localhost",
      "port": 27017
    }]
  }
}

The configuration object can have the following keys:

  • migration — Contains the configuration to run migrations,
  • mongo - Contains the configuration for MongoDB,
  • migration.directory — the directory (path relative to the current folder) to store migration files in and read them from,
  • migration.collection — The name of the MongoDB collection to track already ran migrations,
  • mongo.database — MongoDB database where migrations will be applied,
  • mongo.user [optional] — MongoDB user name when authentication is required,
  • mongo.password [optional] — MongoDB password when authentication is required,
  • mongo.replicaSet [optional] — MongoDB replicaSet if database has one,
  • mongo.servers - MongoDB servers configuration.
  • mongo.servers[@].host [localhost by default]- MongoDB host of server
  • mongo.servers[@].port [27017 by default]- MongoDB port of server

Creating Migrations

The app simplifies creating migration stubs by providing a command

  migrate create 'migration name'

This creates automatically numbered file ddmmyyyyhhmmssms-migration-name.js inside of the directory defined in the configuration file.

The migration file must be a EcmaScript6 module exporting the following:

  • id — a string that's used to identify the migration (filled automatically when creating migrations through migrate create).
  • upgrade — a function used for forward migration.
  • downgrade — a function used for backward migration.

See Configuration if your config file has non-standard name.

Migration functions

The upgrade and downgrade functions take a single parameter — a Node-style callback:

const upgrade = function(done) {
  // call done() when migration is successfully finished
  // call done(error) in case of error
}

The upgrade and downgrade functions are executed with the scope providing 2 convenient properties:

  • this.db is an open MongoDB native driver connection. Useful if you are not using any ODM library.
  • this.log is a function allowing you to print informative messages during the progress of your migration.

Sample migration file

'use strict';

const id = '${name}';

const upgrade = function(done) {
  // use this.db for MongoDB communication, and this.log() for logging
  done();
};

const downgrade = function(done) {
  // use this.db for MongoDB communication, and this.log() for logging
  done();
};

module.exports = {
  id: id,
  upgrade: upgrade,
  downgrade: downgrade
};

Running migrations

Run all migrations from the directory (specified in Configuration) by calling

For Upgrades

migrate upgrade [null|id|name]

Where

  • null will run all pending migrations,
  • id is only the numeric on id
  • name is tha part of the name on id

For downgrades

migrate downgrade [null|id|name]

Where

  • null will do rollback to all migrations,
  • id is only the numeric on id
  • name is tha part of the name on id

The library only runs migrations that:

  1. have upgrade function defined,
  2. have downgrade function defined,
  3. were not ran before against this database.
  4. were downgraded at any moment

Successfully upgrade migrations are recorded in the collection specified in Configuration and successfully downgrade migrations remove the record from the collection.

The migration process is stopped instantly if some migration fails (returns error in its callback).

See Configuration if your config file has no standard configuration.

Programmatic usage

The library also supports programmatic usage.

Start with require'ing it:

var migrate = require('mongo-migrator');

Using Migrator

Next, you can use migrate as was described before:

migrade.init();
migrate.create(name);
migrate.upgrade(option);
migrate.downgrade(option);

For downgrade and upgrade you have to sent the option in an Array as the second item([null, option]).

mongo-migrator's People

Contributors

jesusx21 avatar wichops avatar

Watchers

James Cloos avatar Luis Argumedo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.