Skip to content

Latest commit

 

History

History
84 lines (66 loc) · 1.82 KB

README.md

File metadata and controls

84 lines (66 loc) · 1.82 KB

mongoose-seed-plus

mongoose-seed-plus lets you populate and clear MongoDB documents with all the benefits of Mongoose validation

npm install mongoose-seed-plus --save-dev

Basic example

// open => seed.js
var seeder = require('mongoose-seed-plus');

// Connect to MongoDB via Mongoose
seeder.connect('mongodb://localhost:27017/mongoose-seed-plus-dev', function() {

    // Start processing...
    seeder.start(__dirname + '/migrations', [
        { path: 'models/Product.js', name: 'Product', clear: true },
        { path: 'models/User.js', name: 'User', clear: true },
    ], true);
});

Data files

Create file .json in folder migrations.

Structure

├── seed.js
└─┬ migrations
  ├── users.json
  ├── products.json

Example

// open => migrations/users.json
// Data object containing seed data - documents organized by Model
{
    "model": "User",
    "documents": [
        {
            "firstname": "Christopher",
            "lastname": "Fouquier",
            "email": "[email protected]",
            "password": "root",
            "role": "admin"
        },
        {
            "firstname": "John",
            "lastname": "Doe",
            "email": "[email protected]",
            "password": "123"
        }
    ]
}

Methods

seeder.connect(db, [callback])

Initializes connection to MongoDB via Mongoose singleton.

seeder.start(path, models, [dump])

seeder.start(__dirname + '/migrations',
[
    /**
     * @path (string, required) : path to model
     * @name (string, required) : name of model
     * @clear (boolean, required) : clear DB collection
     */
    { path: 'models/User.js', name: 'User', clear: true },
    { path: 'models/Product.js', name: 'Product', clear: false },
], true);