mongoose-data-migrations

0.1.1 • Public • Published

Mongoose Schema Migrations

Performs mongoose data migrations on document init.

Brief Example

var schema = new mongoose.Schema({ /* ... */ });
 
var currentSchemaVersion = 2;
var opts = {
    versionKey: '__sv',   // The default
    saveOnMigration: true // Also the default
};
 
migrations(schema, currentSchemaVersion, opts).add({
    version: 1,
    up: function(data) {
        var nameParts = data.name.split(' ', 2);
        data.firstName = nameParts[0];
        data.lastName = nameParts[1];
        delete data.name;
    },
    down: function(data) {
        data.name = data.firstName + ' ' + data.lastName;
        delete data.firstName;
        delete data.lastName;
    }
}).add({
    version: 2,
    up: function(data) {
        var lastNameParts = data.lastName.split(',', 2);
        data.suffix = null;
        if (2 == lastNameParts.length) {
            data.lastName = lastNameParts[0];
            data.suffix = lastNameParts[1].replace(/^\s/, '');
        }
    },
    down: function(data) {
        if (data.suffix) {
            data.lastName += '' + data.suffix;
            delete data.suffix;
        }
    }
});

Readme

Keywords

none

Package Sidebar

Install

npm i mongoose-data-migrations

Weekly Downloads

2

Version

0.1.1

License

MIT

Last publish

Collaborators

  • inxilpro