Coder Social home page Coder Social logo

mongoose-seed's Introduction

mongoose-seed

mongoose-seed lets you populate and clear MongoDB documents with all the benefits of Mongoose validation

Basic example

var seeder = require('mongoose-seed');

// Connect to MongoDB via Mongoose
seeder.connect('mongodb://localhost/sample-dev', function() {

  // Load Mongoose models
  seeder.loadModels([
    'app/model1File.js',
    'app/model2File.js'
  ]);

  // Clear specified collections
  seeder.clearModels(['Model1', 'Model2'], function() {

    // Callback to populate DB once collections have been cleared
    seeder.populateModels(data, function() {
      seeder.disconnect();
    });

  });
});

// Data array containing seed data - documents organized by Model
var data = [
	{
		'model': 'Model1',
		'documents': [
			{
				'name': 'Doc1',
				'value': 200
			},
			{
				'name': 'Doc2',
				'value': 400
			}
		]
	}
];

Methods

seeder.connect(db, [callback])

Initializes connection to MongoDB via Mongoose singleton.


seeder.loadModels(filePaths)

Loads mongoose models into Mongoose singleton. Only Models that have been loaded can be cleared or populated.


seeder.clearModels(modelArray, [callback])

Clears DB collection specified by each model in modelArray. Callback is executed after DB is cleared (useful for populateModels method)


seeder.populateModels(dataArray, [callback])

Populates MongoDB with documents in dataArray. dataArray consists of objects with 'model' and 'documents' keys, where 'documents' is an array of valid collection documents. Note that Mongoose Schema validation is enforced.


seeder.disconnect()

Disconnects mongoose db-handle. Use it inside populateModels callback to cleanly exit the program (see example above).


seeder.setLogOutput(logOutput)

Disables or enables calls to console.log. If false is passed, only errors will be print to console.

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.