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

mongo-seed

v0.6.0

Published

Built with testing MEAN applications in mind. Gives a developer a way to populate mongo database from different data sources; functions, files, directories of JSON files.

Downloads

148

Readme

mongo-seed

Built with testing MEAN applications in mind. Gives a developer a way to populate mongo databases from different data sources; functions, files, directories of JSON files. Also allows the developer to clean up the databases after the test.

Also open to anyone who wants to build this out with more cool things.

Types of Seeds

Directory Seed

This is where you have a directory filled with JSON files that were created from a mongoexport.

NOTE: make sure to use --jsonArray when exporting data from tables. Similar mongoexport --db PetShop --collection Food --out Food.json --jsonArray

mongoSeed.load("localhost",27017, "<name_of_database>", "<seed_directory>", "dir", function (err) {
  //..do what ever you need
});

File

This is similar to the directory seed. You have a file with an object in it where each property is a collection name and the value is an array of documents for that collection. This also supports two different data formats. The MongoDB Extended JSON(EXTENDED) or the JSON recognized by the node mongodb driver(DRIVER).

{
    "dataFormat": "<Type-of-json>",// supported types EXTENDED or DRIVER
    "table_Name": [/*Each document as an individual object in the array*/]
    /*...*/
    "table_Name_n": [/*Each document as an individual object in the array*/]
}
mongoSeed.load("localhost",27017, "<name_of_database>", "<path_to_file>", "file", function (err) {
  //..do what ever you need
});

Load from Function

So loading from a function means you have a node module somewhere that returns JSON in the same format the node mongodb driver accepts. This came about because I wanted to use some of the mongoDB client helpers to set up data sets.

module.exports = function(){
    return {
       "table_Name": [/*Each document as an individual object in the array*/]
       /*...*/
       "table_Name_n": [/*Each document as an individual object in the array*/]
    };
};
mongoSeed.load("localhost",27017, "<name_of_database>", "<path_function_def>", "function", function (err) {
  //..do what ever you need
});

Mong Dump file

COMING SOON

Self-explanatory. Back up a database that causes certain test cases load it in the test environment then tear it down. Well when this is added....

REST ENDPOINT

COMING SOON

Ability to load JSON from a REST endpoint or maybe a JSON file stored on an S3 Bucket.

Examples

Single Database

Lets say you have the following directory structure:

├── seeds
│   └── functionSeed.js
└── test
    └── generica.test.js

The file functionSeed.js might look like this:

module.exports = function(){
    return {
       "table_Name": [
         {
           "_id": new ObjectId(“some id here”), "Name": "Person"
         }
       ]
    };
};

The in the test file, lets say you are using mocha for testing:

var async = require('async'),
  mongoSeed = require('mongo-seed');

describe("testing some functionality", function(){

  var mongo = {
    "host": "",
    "port": "",
    "db": ""
  };

  before(function (done) {
    async.waterfall([
        function (callback) {
          mongoSeed.clear(mongo.host, mongo.port, mongo.db, function (err) {
            callback(err);
          });
        },
        function (callback) {
          var seedPath = path.resolve(__dirname + "/../seeds/functionSeed.js");
          mongoSeed.load(mongo.host, mongo.port, mongo.db, seedPath, "function", function (err) {
            callback(err);
          });
        }
      ],
      function (err, results) {
        if(err) throw err;
        done();
      });
  });

  it("Do some testing here", function(done){
    // test here
    done();
  });

});

Multiple databases

Lets say you need to seed multiple databases for testing here is a quick example of how you might do that.


var async = require('async'),
  mongoSeed = require('mongo-seed');

describe("testing some functionality", function(){

  var mongo = {
    "host": "",
    "port": "",
    "db": ""
  };

  var mongo2 = {
    "host": "",
    "port": "",
    "db": ""
  };

  before(function (done) {
    async.waterfall([
        function (callback) {
          mongoSeed.clear(mongo.host, mongo.port, mongo.db, function (err) {
            callback(err);
          });
        },
        function (callback) {
          mongoSeed.clear(mongo2.host, mongo2.port, mongo2.db, function (err) {
            callback(err);
          });
        },
        
        function (callback) {
          var seedPath = path.resolve(__dirname + "/../seeds/functionSeed.js");
          mongoSeed.load(mongo.host, mongo.port, mongo.db, seedPath, "function", function (err) {
            callback(err);
          });
        },
        function (callback) {
          var seedPath = path.resolve(__dirname + "/../seeds/functionSeed2.js");
          mongoSeed.load(mongo2.host, mongo2.port, mongo2.db, seedPath, "function", function (err) {
            callback(err);
          });
        }
      ],
      function (err, results) {
        if(err) throw err;
        done();
      });
  });

  it("Do some testing here", function(done){
    // test here
    done();
  });

});