omrilitov/mongoose-plugin-seed


Mongoose plugin to seed your models

License: MIT

Language: JavaScript


mongoose-plugin-seed

Mongoose plugin to seed your models

Linux OSX Windows Coverage Dependencies DevDependencies
Coverage Status

Overview

This module will seed your Mongoose models while using dependency management between them. It will empty the collection and create the new given data.

To use:

  1. npm install --save mongoose-plugin-seed
  2. Use the plugin in the desired Models with given seed data
  3. Call seed

Examples

const addSeed = require('mongoose-plugin-seed').addSeed;
const mongooseSeed = require('mongoose-plugin-seed').seed;

// Define Schemas
var UserSchema = new Schema({...});
var RoleSchema = new Schema({...});

// Define Models
var User = mongoose.model('User', UserSchema);
var Role = mongoose.model('Role', RoleSchema);

// Define the seed with dependency to roles
addSeed(User, {
    dependencies: [Role],
    seed: function (roles) {
      return [{
        username: "foo",
        password: "123",
        roles: roles[0]
      }, {
        username: "bar",
        password: "321",
        roles: roles[1]
      }];
    }
  });

// Define roles seed
addSeed(Role, {
    seed: function () {
      return [{
        name: "admin"
      }, {
        name: "user"
      }];
    }
  });

// Seed!
mongooseSeed()
  .then(function () {
    console.log('Success!');
  });

API

addSeed

var addSeed = require('mongoose-plugin-seed').addSeed;
addSeed(Model, options);
  • Model - The mongoose model to seed

The plugin uses the following options:

  • seed - function that returns the seed data (using required dependencies)
  • dependencies (optional) - dependencies to seed the data
  • drop (default=false) - should drop existing collection instead of removing all documents

createSeedModel

var createSeedModel = require('mongoose-plugin-seed').createSeedModel;
createSeedModel(name, Schema, options);
  • name - The name to give to the mongoose model
  • Schema - The mongoose schema to create and seed
  • options - Same options as the addSeed API

This function returns the created model. It is just a short for

var Model = mongoose.model(name, Schema);
addSeed(Model, options);

seed

var mongooseSeed = require('mongoose-plugin-seed').seed;
mongooseSeed();

The function seeds all the data in the correct order. Returns a Promise.

Project Statistics

Sourcerank 2
Repository Size 14.6 KB
Stars 0
Forks 0
Watchers 2
Open issues 0
Dependencies 20
Contributors 2
Tags 5
Created
Last updated
Last pushed

Top Contributors See all

Omri Litov Noam Okman

Packages Referencing this Repo

mongoose-plugin-seed
Mongoose plugin to seed your models
Latest release 0.4.0 - Updated

Recent Tags See all

v0.4.0 March 13, 2016
v0.3.0 February 02, 2016
v0.2.0 December 30, 2015
v0.1.1 December 25, 2015
v0.1.0 December 24, 2015

Something wrong with this page? Make a suggestion

Last synced: 2017-02-11 11:21:05 UTC

Login to resync this repository