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

mongoose-db-populator

v0.0.1

Published

Populate db for integration testing using your mongoose models

Downloads

7

Readme

mongoose-db-populator

This module helps to populate the MongoDB with documents for testing purposes. It also cleans the database (absolutely all collections).

  describe('models', function () {
    beforeEach( function (done) {
        // you are responsible for the connection to mongodb instance!!!
        db.mongoose.connect('mongodb://localhost:27017/mdp', done);
    });

    afterEach( function (done) {
        // you are responsible for disconnection from mongodb instance!!!
        db.mongoose.connection.close(done);
    });

    beforeEach( function (done) {
        // populate db
        var pop = new Populator();

        // use these model factories to create documents
        pop.model(require('./support/model'));

        // use these suite factories to populate db
        pop.suite(require('./support/suite'));

        // remove absolutely all docs from db
        // and populate db using "main" suite
        this.suite = pop.populate('main', done);
    });

    it('should populate db with 7 authors', function (done) {
        db.Author.find({}, function (err, authors) {
            if (err) return done(err);
            expect(authors).to.have.length(7);
            done();
        });
    });
  });

Please, refer to test/integration/test.js for details!

model factory

It's goal is to create a valid mongoose document using your options or some defaults

// ./support/model/Book.js
//
// ./support/model exports { Book : require('./Book')}
// e.g. using require-directory module
//

// here your models are defined
// db.Book === mongoose.model('Book')
var db = require('../../../models/db');

var i = 1;

module.exports = function bookFactory (options) {
    options = options || {};

    var bookDoc = {
        title:      options.title || 'Book title #' + i,
        authors:    options.authors || []
    };

    i++;

    return new db.Book(bookDoc);
};

suite factory

It's purpose to create all the required documents. Internally model factories are used

// ./support/suite/main
//
// ./support/suite exports { main : require('./main')}
// e.g. using require-directory module
//
module.exports = function mainSuite () {
    // this refers to the instance of Suite
    var suite = this;

    var authors1 = [0,1,2,3].map( function () {
        return suite.create('Author');
    });

    var authors2 = [0,1,2].map( function () {
        return suite.create('Author');
    });

    var book1 = suite.create('Book', {
        title: 'First book by four authors',
        authors: authors1
    });

    var book2 = suite.create('Book', {
        title: 'Second book by three authors',
        authors: authors2
    });

    var book3 = suite.create('Book', {
        title: 'Last book - no authors'
    });
};