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

migrate-semver-mongoose

v0.7.0

Published

Mongoose plugin for migrate-semver

Downloads

390

Readme

MongoDb migrations using mongoose and migrate-semver

migrate-semver-mongoose is a plugin for migrate-semver allowing you to do migrations based on SemVer and mongoose.

Installation

npm install --save migrate-semver
npm install --save migrate-semver-mongoose

Usage

migrate-semver just handles the heavy lifting of finding the viable migrations to run based on a SemVer compliant version string passed into it. The parts specific to MongoDb / Mongoose are handled by migrate-semver-mongoose.

The following example runs a migration for version 0.3.0.

const SemVerMigration = require('migrate-semver');
const mongoosePlugin = require('migrate-semver-mongoose');

const migrationsDirectory = path.join(__dirname, 'migrations');
const migrateSemVer = new SemVerMigration({ migrationsDirectory }, mongoosePlugin());
const version = '0.3.0';

migrateSemVer.connect({ mongoServer: 'mongodb://localhost:27017/test' }, err => { 
  migrateSemVer.canMigrate({ version }, (err, canMigrate) => {
    if (canMigrate) {
      migrateSemVer.up({ version }, err => {
        console.log('done');
      });
    }
  })
});

migrate-semver-mongoose handles several scenarios for you based on the example above:

  • If your current database has no migrations at all (so it might be empty at all), migrate-semver runs all available migrations until 0.3.0 (including 0.3.0).
  • If your current database has version 0.2.0 applied and 0.3.0 is the next available migration, 0.3.0 is just applied.
  • If your current database has version 0.1.0 applied and 0.2.0 is also available, both 0.2.0 and 0.3.0 wil be applied.

The scenarios described above can be found in the tests.

migrate-semver-mongoose allows you to specify the base directory which contains all your migration folders and files. Just pass the migrationsDirectory option to the SemVerMigration ctor function as shown above.

The file and folder structure has to follow this convention (the index-up.js can be renamed if you implement your own plugin):

The index-up.js could look like this:

'use strict';
const CustomerSchema = require('./schemas/customer');

/**
 * @param {Object} options
 * @param {Object} options.conn
 * @param {Object} options.model
 * @param {Object} options.customOptions
 * @param continueWith
 */
const up = function (options, callback) {
  const conn = options.conn;
  const Customer = conn.model('Customer', CustomerSchema);
  const customer = new Customer({
    name: 'PDMLab'
  });

  customer.save(err => {
    callback(err);
  });
};

module.exports = {
  up
};

As you can see, you get passed a mongoose connection instance so you don't mess with the mongoose default instance.

customCollectionName

The default name of the collection where the applied migrations are stored is migrations. You can change it by passing a different name into the plugin ctor function:

const customCollectionName = 'custom.migrations';
mongoosePlugin({ migrationsCollectionName: customCollectionName })

customOptions

You can pass custom options to the up function as child object of the options parameter. These customOptions will be passed into migrate-semver-mongoose which handles them appropriately and they'll be passed into every migration as a child object of the up functions options.

Passing the options to migrate-semver:

const customOptions =  { customName: 'Google' };
migrateSemVer.up({ version, customOptions }, err => {
  const Customer = conn.model('Customer', CustomerSchema);
  
  Customer.findOne({ name: 'Google' }, (err, customer) => { 
    assert.notEqual(null, customer);
    done();
  });
});

The migration itself:

/**
 * @param {Object} options
 * @param {Object} options.conn
 * @param {Object} options.model
 * @param {Object} [options.customOptions]
 * @param continueWith
 */
const up = function (options, continueWith) {
  let name = 'PDMLab';

  if (options.customOptions) {
    name = options.customOptions.customName;
  }
  const conn = options.conn;
  const Customer = conn.model('Customer', CustomerSchema);
  const customer = new Customer({ name });

  customer.save(err => {
    continueWith(err);
  });
};

module.exports = {
  up
};

Running the tests

The tests require Docker (version 1.10+) and Docker Compose (version 1.6.0+). Make sure you have installed both.

docker pull mongo:4.2
npm test

Want to help?

This project is just getting off the ground and could use some help with cleaning things up and refactoring.

If you want to contribute - we'd love it! Just open an issue to work against so you get full credit for your fork. You can open the issue first so we can discuss and you can work your fork as we go along.

If you see a bug, please be so kind as to show how it's failing, and we'll do our best to get it fixed quickly.

Before sending a PR, please create an issue to introduce your idea and have a reference for your PR.

Also please add tests and make sure to run npm run eslint.

License

MIT License

Copyright (c) 2016 PDMLab

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.