mongoose-seed-db

0.0.10 • Public • Published

mongoose-seed-db

Build Status npm Version

mongoose-seed-db lets you populate and clear MongoDB collections with all the benefits of Mongooses model validation

Basic example

var MongooseSeed = require('mongoose-seed-db');
//import MongooseSeed from 'mongoose-seed-db'; //ES6
 
MongooseSeed.connect('mongodb://localhost:27017/mongoose-seed').then(() => {
    MongooseSeed.loadModels(__dirname + '/../src/models');
    MongooseSeed.clearAll().then(() => {
        MongooseSeed.populate(__dirname + '/data').then(() => {
            process.exit();
        });
    });
});

Mongoose Model example

var mongoose = require('mongoose');
//import mongoose from 'mongoose'; //ES6
 
var test = new mongoose.Schema({
    name: { type: String }
});
 
export default mongoose.model('Test', test);
 

Data example (.js)

module.exports = {
    model: 'Test',
    data: [
        { name: 'js-example-entry-1' },
        { name: 'js-example-entry-1' }
    ]
};

Data example (.json)

{
    "model": "Test",
    "data": [
        { "name": "json-example-entry-1" },
        { "name": "json-example-entry-1" }
    ]
}

Functions

MongooseSeed.connect(db)

Initializes connection to MongoDB via Mongoose.


MongooseSeed.loadModels(path)

Loads mongoose models from path, use: __dirname + '/path_to_models'


MongooseSeed.populate(path, options)

Populate MongoDB with data in js/json files from path, use: __dirname + '/path_to_data'

Options:

populateExisting: true/false
    - defaults to true
    - false = only populate models without existing documents

MongooseSeed.clearAll()

Clears all data from loaded models


MongooseSeed.clearModels(array)

Clears all data from list of models provided in array

Package Sidebar

Install

npm i mongoose-seed-db

Weekly Downloads

12

Version

0.0.10

License

MIT

Last publish

Collaborators

  • bellstrand