mongoose-seed-plus lets you populate and clear MongoDB documents with all the benefits of Mongoose validation
npm install mongoose-seed-plus --save-dev
// 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);
});
Create file .json
in folder migrations
.
├── seed.js
└─┬ migrations
├── users.json
├── products.json
// 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"
}
]
}
Initializes connection to MongoDB via Mongoose singleton.
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);