2.0.4 • Published 7 years ago

devhouse-fixtures v2.0.4

Weekly downloads
1
License
MIT
Repository
-
Last release
7 years ago

devhouse-fixtures

NPM  

This is the clone of the original repo mongoose-fixtures except several things:

  1. Db dumping support (could be combined with your owner migration tool to do full fixture migration)
  2. No async unnecessary dependencies
  3. No callback, returns Promise instead
  4. Consequent models loading
  5. Connection must be specified (you almost always have db connection in your project, isn't it?)

Fixtures can be in one file, or divided up into separate files for organisation (e.g. one file per model)

The fixture files must export objects which are keyed by the Mongoose model name, each containing the data for documents within that.

NOTE: Loading fixtures will clear the existing contents of a collection!

Examples

With the file below, 3 documents will be inserted into the 'User' collection and 2 into the 'Business' collection:

//fixtures.js
exports.User = [
    { name: 'Gob' },
    { name: 'Buster' },
    { name: 'Steve Holt' }
];

exports.Business = [
    { name: 'The Banana Stand' },
    { name: 'Bluth Homes' }
];

You can also load fixtures as an object where each document is keyed, in case you want to reference another document:

//users.js
var ObjectId = require('mongodb').BSONNative.ObjectID;

exports.User = {
    user1: {
        _id: new ObjectId(),
        name: 'Michael'
    },
    user2: {
        _id: new ObjectId(),
        name: 'George Michael',
        father: exports.User.user1._id
    }
}

Usage

let db = require('../data-model/db'),
fixtures = require('devhouse-fixtures');

describe('tescases', function () {
    before(done => {
        fixtures.load(__dirname + '/fixtures/', db.connection)
            .then(() => done());
    });
    it('Check database count', done => {
        ...
    })

})

Installation

npm install devhouse-fixtures --save-dev
2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.0

7 years ago