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

@leopradel/mgdb-migrator

v1.3.5

Published

Mongodb schema and data migration lib

Downloads

63

Readme

A simple migration system for mongodb supporting up/downwards migrations.

Installation

Migrations can be installed through yarn or npm. Type:

$ npm install mgdb-migrator

or

$ yarn add mgdb-migrator

API

Basics

Import and use the migration instance - migrator. User the migrator to configure and setup your migration

import { migrator } from 'migration';

migrator.config({
      // false disables logging
      log: true,
      // null or a function
      logger: (level, ..arg) => console.log(level, ..arg),
      // enable/disable info log "already at latest."
      logIfLatest: true,
      // migrations collection name. Defaults to 'migrations'
      collectionName: 'migrations',
      // mongdb url or mongo Db instance
      db: "your connection string",
}); // Returns a promise

Or ...

Define a new instance of migration and configure it as you see fit

import { Migration } from 'migration';

var migrator = new Migration({
      // false disables logging
      log: true,
      // null or a function
      logger: (level, ..arg) => console.log(level, ..arg),
      // enable/disable info log "already at latest."
      logIfLatest: true,
      // migrations collection name
      collectionName: 'migrations',
      // mongdb url or mongo Db instance
      db: "your connection string",
})
await migrator.config(); // Returns a promise

To write a simple migration, somewhere in the server section of your project define:

migrator.add({
  version: 1,
  up: function(db) {
    // use `db`(mongo driver Db instance) for migration setup to version 1
    // See http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html for db api
  }
});

To run this migration to the latest version:

migrator.migrateTo('latest');

Advanced

A more complete set of migrations might look like:

migrator.add({
  version: 1,
  name: 'Name for this migration',
  up: function(db) {
    // use `db`(mongo driver Db instance) for migration setup to version 1.
    // See http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html for db api
  },
  down: function(db) {
    // use `db`(mongo driver Db instance) for migration setup to version 0
    // See http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html for db api
  }
});

migrator.add({
  version: 2,
  name: 'Name for this migration',
  up: function(db) {
    // use `db`(mongo driver Db instance) for migration setup to version 2
    // See http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html for db api
  },
  down: function(db) {
    // use `db`(mongo driver Db instance) for migration setup to version 1
    // See http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html for db api
  }
});

Control execution flow with promises:

// using bluebird promise lib
import { Promise } from 'bluebird';

migrator.add({
  version: 1,
  name: 'Name for this migration',
  up: Promise.method(function(db) {
    // use `db`(mongo driver Db instance) for migration setup to version 1.
    // See http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html for db api
    return db.collections('someCollection')....
  }),
  down: Promise.method(function(db) {
    // use `db`(mongo driver Db instance) for migration setup to version 0
    // See http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html for db api
    return db.collections('someCollection')....
  })
});

Control execution flow with async/await:

migrator.add({
  version: 2,
  name: 'Name for this migration',
  up: async function(db) {
    // use `db`(mongo driver Db instance) for migration setup to version 2
    // See http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html for db api
     await db.collections('someCollection')....
  },
  down: async function(db) {
    // use `db`(mongo driver Db instance) for migration setup to version 1
    // See http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html for db api
    await db.collections('someCollection')....
  }
});

As in 'Basics', you can migrate to the latest by running:

migrator.migrateTo('latest');

By specifying a version, you can migrate directly to that version (if possible). The migration system will automatically determine which direction to migrate in.

In the above example, you could migrate directly to version 2 by running:

migrator.migrateTo(2);

If you wanted to undo all of your migrations, you could migrate back down to version 0 by running:

migrator.migrateTo(0);

Sometimes (usually when somethings gone awry), you may need to re-run a migration. You can do this with the rerun subcommand, like:

migrator.migrateTo('3,rerun');

To see what version the database is at, call:

migrator.getVersion();

To see what number of migrations configured, call:

migrator.getNumberOfMigrations();

IMPORTANT:

  • You cannot create your own migration at version 0. This version is reserved by migration for a 'vanilla' system, that is, one without any migrations applied.
  • If migrating from vT1 to vTz and migration fails from a vTx to vTy, where vTx & vTy are incremental versions between vT1 to vTz, migration will stop at vTx.
  • Prefer an async function (async | promise) for both up()/down() setup. This will ensure migration completes before version bump during execution.

Configuration

You can configure Migration with the config method. Defaults are:

migrator.config({
  // Log job run details to console
  log: true,
  // Use a custom logger function (level, ...args) => void
  logger: null,
  // Enable/disable logging "Not migrating, already at version {number}"
  logIfLatest: true,
  // migrations collection name to use in the database
  collectionName: "migrations"
  // mongdb url or mongo Db instance
  db: "your connection string",
});

Logging

Migrations uses console by default for logging if not provided. If you want to use your own logger (for sending to other consumers or similar) you can do so by configuring the logger option when calling migrator.config .

Migrations expects a function as logger, and will pass an argument with properties level, message, to it for you to take action on.

var MyLogger = function(opts) {
  console.log('Level', opts.level);
  console.log('Message', opts.message);
}

Migrations.config({
  ...
  logger: MyLogger
  ...
});

The opts object passed to MyLogger above includes level, message, and any other additional info needed.

  • level will be one of info, warn, error, debug.
  • message is something like Finished migrating..

Development

Run docker-compose to execute lib in dev mode

$ npm run docker:dev

Test

Run docker-compose to execute lib in test mode

$ npm run docker:test

Credits

Migration builds on percolatestudio/meteor-migrations with the goal of creating a generic mongodb migration library