Giter Site home page Giter Site logo

mongoose-plugin-seed's Introduction

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.

mongoose-plugin-seed's People

Contributors

noamokman avatar omrilitov avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.