Skip to content

hellivan/mongoose-model-migration

Repository files navigation

mongoose-model-migration

Build Status Code Coverage MIT License semantic-release Renovate enabled NPM Package NPM Package Downloads

Utility library that provides some basic mechanisms for versioning and upgrading mongoose models in Node.js

Installation

Using npm:

npm install --save mongoose-model-migration

Using yarn

yarn add mongoose-model-migration

Usage

In ES6 / Typescript

import { migrateModel, migrateCollection } from 'mongoose-model-migration';

Basic Collection migration

import { migrateCollection, CollectionMigrationHandler } from 'mongoose-model-migration';

const migrationHandler: CollectionMigrationHandler = {
    up: async (db, collection, fromVersion, toVersion) => {
        // ... call upgrate operations for collection
    },
    down: async (db, collection, fromVersion, toVersion) => {
        // ... call downgrade operations for collection
    }
};

await migrateCollection('users', 2, migrationHandler);

Collection migration options

migrateCollection accepts an optional options object as 4th parameter:

option Description
db Optionally specify the mongodb database that should be used during migration. Defaults to the global mongoose connection.db
versionCollectionName Optionally specify the collection name that should be used to store version information. Defaults to collectionName + '.version'