Giter Site home page Giter Site logo

sharvit / mongoose-data-seed Goto Github PK

View Code? Open in Web Editor NEW
110.0 4.0 21.0 4.79 MB

Seed mongodb with data using mongoose models

Home Page: https://sharvit.github.io/mongoose-data-seed/

License: MIT License

JavaScript 99.33% HTML 0.67%
mongoose-data-seed seed-mongodb mongoose-seed

mongoose-data-seed's Introduction

Package Version semantic-release Downloads Status Build Status: Linux Coverage Status PRs Welcome dependencies Status devDependencies Status Greenkeeper badge code style: prettier FOSSA Status MIT License

mongoose-data-seed

Seed mongodb with data using mongoose models

cli example using md-seed run

Install

npm install --save mongoose-data-seed
md-seed init

md-seed init will ask you to choose a folder for your seeders.

md-seed init will create the seeders folder, generate md-seed-config.js and update your package.json.

Use

Generate seeder file

md-seed g users

Run all seeders

md-seed run

Or run specific seeders

md-seed run users posts comments

Options

Drop the database before seeding

md-seed run --dropdb

Seeder Example

import { Seeder } from 'mongoose-data-seed';
import { User } from '../server/models';

const data = [
  {
    email: '[email protected]',
    password: '123123',
    passwordConfirmation: '123123',
    isAdmin: true
  },
  {
    email: '[email protected]',
    password: '123123',
    passwordConfirmation: '123123',
    isAdmin: false
  }
];

class UsersSeeder extends Seeder {
  async shouldRun() {
    return User.countDocuments()
      .exec()
      .then(count => count === 0);
  }

  async run() {
    return User.create(data);
  }
}

export default UsersSeeder;

md-seed-config.js

md-seed expecting to get 3 values from md-seed-config.js

  1. seedersList - A key/value list of all your seeders, md-seed will run your seeders as they ordered in the list.
  2. connect - Connect to mongodb implementation (should return promise).
  3. dropdb - Drop/Clear the database implementation (should return promise).

Example

import mongoose from 'mongoose';

import Users from './seeders/users.seeder';
import Posts from './seeders/posts.seeder';
import Comments from './seeders/comments.seeder';

const mongoURL = process.env.MONGO_URL || 'mongodb://localhost:27017/dbname';

/**
 * Seeders List
 * order is important
 * @type {Object}
 */
export const seedersList = {
  Users,
  Posts,
  Comments,
};
/**
 * Connect to mongodb implementation
 * @return {Promise}
 */
export const connect = async () => await mongoose.connect(mongoURL, { useNewUrlParser: true });
/**
 * Drop/Clear the database implementation
 * @return {Promise}
 */
export const dropdb = async () => mongoose.connection.db.dropDatabase();

Configurations

mongoose-data-seed configurations will get loaded from the mdSeed field in your package.json file.

Field Default Value Description
seedersFolder './seeders' Path for your seeders-folder, seeders will be generated into this folder.
customSeederTemplate undefined Path to a custom template file to generate your seeders from.

Examples

  1. md-seed-example

License

MIT

FOSSA Status MIT License

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.