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

data-elevator-mongodb

v5.1.0

Published

Flexible utility for managing migrations of mongodb data sources

Downloads

91

Readme

DATA ELEVATOR MONGODB

The data elevator mongodb is an easy to use and very flexible utility for migrating data sources based on the NPM module data elevator. The only difference is that data elevator mongodb stores its current migration level in a mongodb database.

Storing the current migration level in a database brings advantages when a project shares its data source with multiple running instances of a project. For example when multiple developers working with one database or the project runs on multiple servers.

QUICKSTART

STEP 1: Install

npm install data-elevator-mongodb

STEP 2: Construct a new data elevator for the project.

node ./node_modules/data-elevator-mongodb construct

STEP 3: Add a new migration.

node ./data-elevator/elevator add "add phone number to users"

STEP 4: Enter you migration code in the generated floor file.

see: ./data-elevator/floors/

STEP 5: Move the elevator to migrate data.

node ./data-elevator/elevator move top
node ./data-elevator/elevator move ground
node ./data-elevator/elevator move 2
node ./data-elevator/elevator move up 2
node ./data-elevator/elevator move down 3

STEP 6: Use help command to get information about additional commands

node ./data-elevator/elevator help

CONFIGURATION

  • levelControllerConfig.collectionName: Name of the collection to store the migration level in
  • levelControllerConfig.connectionOptions: Connection options see MongoDb website
  • levelControllerConfig.connectionUrl: Url for database connection
const config = {
    levelControllerConfig: {
       collectionName: "_data_elevator",
       connectionOptions: null,
       connectionUrl: null
    }
}

MIGRATION EXAMPLES

These migrations use the class MongoClientHelpers, this class provider some easy functions to quickly access you collections and documents.

Updating the content of a property in documents in a collection

const MongoClientHelpers = require('data-elevator-mongodb/lib/helpers/mongo-client-helpers');
const async = require('async');

module.exports = {
    /**
     * Data transformation that need to be performed when migrating the data up
     * @param floorWorkerParameters - instance of FloorWorkerParameters
     * @param callback(error) - If an error is returned then all the subsequent migration will not be handled
     */
    onUp : function(floorWorkerParameters, callback) {
        
        //Get all the documents of a collection in an array
        MongoClientHelpers.getDocuments(floorWorkerParameters, 'dataobjects', function(error, database, collection, documents) {

            //Iterate through the documents
            async.each(documents, 
                function(document, callback) {
                    if(document.objectStatus == 1) {
                        document.objectStatus = 2;
                        
                        //Update the document
                        collection.update({"_id": document._id}, document, null, function(error, document) {
                            return callback(error);
                        })
                    } else {
                        return callback(null);
                    }
                }, 
                function(error) {
                    MongoClientHelpers.closeDatabase();
                    return callback(error);
                });
        });
    }, 
    /**
     * Data transformation that need to be performed when migrating the data down
     * @param floorWorkerParameters - instance of FloorWorkerParameters
     * @param callback(error) - If an error is returned then all the subsequent migration will not be handled
     */
    onDown : function(floorWorkerParameters, callback) {
        MongoClientHelpers.getDocuments(floorWorkerParameters, 'dataobjects', function(error, database, collection, documents) {
            async.each(documents, 
                function(document, callback) {
                    if(document.objectStatus == 2) {
                        document.objectStatus = 1;
                        collection.update({"_id": document._id}, document, null, function(error, document) {
                            return callback(error);
                        })
                    } else {
                        return callback(null);
                    }
                }, 
                function(error) {
                    MongoClientHelpers.closeDatabase();
                    return callback(error);
                });
        });
    }
}

Renaming the names of properties in a collection

const MongoClientHelpers = require('data-elevator-mongodb/lib/helpers/mongo-client-helpers');

module.exports = {
    /**
     * Data transformation that need to be performed when migrating the data up
     * @param floorWorkerParameters - instance of FloorWorkerParameters
     * @param callback(error) - If an error is returned then all the subsequent migration will not be handled
     */
    onUp : function(floorWorkerParameters, callback) {
        
        //Get a specific collection
        MongoClientHelpers.getCollection(floorWorkerParameters, 'dataobjects', function(error, database, collection) {
                const renameFields = {
                    'statText': 'statusText' 
                };

                //Update all the documents in the collection
                collection.update({}, { $rename: renameFields }, {multi: true}, function(error) {
                    MongoClientHelpers.closeDatabase();
                    return callback(error);
                })
        });
    }, 
    /**
     * Data transformation that need to be performed when migrating the data down
     * @param floorWorkerParameters - instance of FloorWorkerParameters
     * @param callback(error) - If an error is returned then all the subsequent migration will not be handled
     */
    onDown : function(floorWorkerParameters, callback) {
        MongoClientHelpers.getCollection(floorWorkerParameters, 'quotes', function(error, database, collection) {
                const renameFields = {
                    'statusText': 'statText' 
                };

                collection.update({}, { $rename: renameFields }, {multi: true}, function(error) {
                    MongoClientHelpers.closeDatabase();
                    return callback(error);
                })
        });
    }
}

FURTHER DOCUMENTATION

For further documenation about commands or customizations see data elevator documentation.

RELATED MODULES

  • data-elevator (npm, github) - store elevator migration levels in a plain file
  • data-elevator-elasticsearch (npm, github) - store elevator migration levels in elasticsearch
  • data-elevator-mongodb (npm, github) - store elevator migration levels in mongodb
  • data-elevator-mysql (npm, github) - store elevator migration levels in mysql
  • data-elevator-postgres (npm, github) - store elevator migration levels in postgres
  • data-elevator-sqlite3 (npm, github) - store elevator migration levels in sqlite3