1.0.0 • Published 7 years ago

mongoose-seed-plus v1.0.0

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

Mongoose Seed Plus

Mongoose-Seed-Plus lets you populate and clear MongoDB documents with all the benefits of Mongoose validation

■ Installation

Use NPM or Yarn

$ npm install mongoose-seed-plus

■ Basic usage

var seeder = require('../index.js');
var chalk  = require('chalk');
var path   = require('path');

var options = {
  mongodb: {
    host: "localhost",
    port: 27017,
    dbname: "mongoose-seed-plus-dev"
  },
  dump: {
    enable: true
  },
  models: [
    { path: 'models/User.js', name: 'User', clear: true },
    { path: 'models/Product.js', name: 'Product', clear: true }
  ],
  path: path.join(__dirname, '/migrations')
};

new seeder(options, function(err, result) {
  if (err) {
    throw err.message;
  }

  console.log(`Successfully connected to MongoDB: ${chalk.grey(result.db)}\n`);

  if (result.cleared) {
    console.log(`Cleared models: ${chalk.grey(result.cleared)}\n`);
  }

  console.log(chalk.cyan('Seeding Results'));
  for (var prop in result.populate) {
    console.log(`${prop}: ${chalk.grey(result.populate[prop])}`);
  }
  process.exit(1);
});

■ Data files

Create file .json in folder migrations.

Structure

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

Example

Data object containing seed data - documents organized by Model

{
    "model": "User",
    "documents": [
        {
            "firstname": "John",
            "lastname": "Doe",
            "email": "john.doe@awesome.com",
            "password": "123456"
        }
    ]
}

■ Methods

seeder(options, callback)

OptionsDescriptionDefaultTypeRequired
pathPath to folder migrations JSON files""(string)true
modelsList of models[](array)true
models[].pathPath to a model""(string)true
models[].nameName a model""(string)true
models[].clearClear collection mongotrue(boolean)false
dumpOption dump{}(object)false
dump.binPath to bin mongodump"/usr/local/bin/mongodump"(string)false
dump.argsArguments for mongodump '--db', "mongoose-seed-plus-dev", '--out', "", '--quiet' (array)false
dump.enableEnable dumpfalse(boolean)false
mongodbOption mongodb{}(object)false
mongodb.hostHost to MongoDB"localhost"(string)false
mongodb.portPort to MongoDB27017(integer)false
mongodb.dbnameDB name to MongoDB"mongoose-seed-plus-dev"(string)false