1.0.12 • Published 7 years ago

mongodb-factory v1.0.12

Weekly downloads
1
License
ISC
Repository
github
Last release
7 years ago

mongodb-factory

Test factory for mongoDB injections.

Getting Started

Getting the Factory constructor object.

var Factory = require('mongodb-factory');

Creating stubs

The argument is a function for possible creation of dynamically generated stubs.

var Stub = Factory.Stub;
/**
 * faker is a library for generating fake data 
 * https://www.npmjs.com/package/faker 
 */
var faker = require('faker'); 
var userStub = new Stub(function () {
    username: faker.internet.username(),
    password: faker.internet.password()
}, 'users'); 

Executing plans

var MONGO_URI = 'mongodb://localhost/test';
var factory = new Factory(MONGO_URI);

factory.add(300, userStub)
    .add(200, animalStub)
    .exec(function (err, docs) { /* docs an array of all inserted documents */
        if (err) 
            /* handle error */
        else 
            /* do something useful */
    });

Getting the database object

For application actions that require insertions to MongoDB, having a database getter can be handy for quick lookups to check they were actually inserted.

factory.getDB(function (err, db) {
    if (err) throw err;
    /* do something with the db object */
});

Cleaning up

Will delete all documents from your database.

factory.cleanUp(function (err) {
  if (err)
    /* handle error */ 
  else 
    /* do some maintenance */
});

Advanced

Dynamic stubs

Arguments defined in stub will be passed into the add() method of factory.

var animalStub = new Stub(function (type, name) {
   return {
     type: type || 'mammal',
     name: name || 'Oski'
   }
}, 'animals');

factory.add(1, animalStub, 'reptile', 'Bob')
    .add(1, animalStub, 'bear')
    .exec(...);

Adding collections to remove all documents from

factory.addToCleanup('otherCollection');

// now it will delete all documents from 'otherCollection' also
factory.cleanup(function (err) {...}); 
1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago