npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-migrator

v1.1.1

Published

Provides for the creation, management, and invocation of change management scripts for MongoDB

Downloads

15

Readme

mongo-migrator

Table of Contents

Note: this library was based on mongodb-migrations

Installation

$ npm install mongo-migrator -g

Common Usage (CLI)

The package installs a single CLI executable — mograte.

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

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

Configuration

$ mograte 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

$ mograte 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 mograte 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

$ mograte 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

$ mograte 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 mograte = require('mongo-migrator');

Using Migrator

Next, you can use mograte as was described before:

mograte.init();
mograte.create(name);
mograte.upgrade(option);
mograte.downgrade(option);

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