Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
seanemmer committed Jun 2, 2015
0 parents commit 397a283
Show file tree
Hide file tree
Showing 2,207 changed files with 310,707 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"node": true, // Enable globals available when code is running inside of the NodeJS runtime environment.
"mocha": true, // Enable globals available when code is running inside of the Mocha tests.
"jasmine": true, // Enable globals available when code is running inside of the Jasmine tests.
"browser": true, // Standard browser globals e.g. `window`, `document`.
"esnext": true, // Allow ES.next specific features such as `const` and `let`.
"bitwise": false, // Prohibit bitwise operators (&, |, ^, etc.).
"camelcase": false, // Permit only camelcase for `var` and `object indexes`.
"curly": false, // Require {} for every new block or scope.
"eqeqeq": true, // Require triple equals i.e. `===`.
"immed": true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
"latedef": "nofunc", // Prohibit variable use before definition except for function declaration.
"newcap": true, // Require capitalization of all constructor functions e.g. `new F()`.
"noarg": true, // Prohibit use of `arguments.caller` and `arguments.callee`.
"quotmark": "single", // Define quotes to string values.
"regexp": true, // Prohibit `.` and `[^...]` in regular expressions.
"undef": true, // Require all non-global variables be declared before they are used.
"unused": false, // Warn unused variables.
"strict": true, // Require `use strict` pragma in every file.
"trailing": true, // Prohibit trailing whitespaces.
"smarttabs": false, // Suppresses warnings about mixed tabs and spaces
"globals": { // Globals variables.
"angular": true,
"io": true,
"ApplicationConfiguration": true,
"Chart": true,
"moment": true,
"$": true,
"_": true,
"google": true
},
"predef": [ // Extra globals.
"inject",
"by",
"browser",
"element"
],
"indent": 4, // Specify indentation spacing
"devel": true, // Allow development statements e.g. `console.log();`.
"noempty": true // Prohibit use of empty blocks.
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Sean Emmer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Empty file added README.md
Empty file.
128 changes: 128 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
'use strict';

/**
* Module dependencies.
**/
var _ = require('lodash'),
async = require('async'),
mongoose = require('mongoose'),
chalk = require('chalk'),
path = require('path');

function Seeder() {
this.connected = false;
}

Seeder.prototype.connect = function(db, cb) {
var _this = this;
mongoose.connect(db, function(err) {
// Log Error
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(err);
} else {
_this.connected = true;
console.log('Successfully initialized mongoose-seed');
cb();
}
});
};

Seeder.prototype.loadModels = function(modelPaths) {
modelPaths.forEach(function(modelPath) {
require(path.resolve(modelPath));
});
};

Seeder.prototype.invalidModelCheck = function(models, cb) {
var invalidModels = [];

models.forEach(function(model) {
if(_.indexOf(mongoose.modelNames(), model) === -1) {
invalidModels.push(model);
}
});

if (invalidModels.length) {
cb(new Error('Models not registered in Mongoose: ' + invalidModels));
} else {
cb();
}
};

Seeder.prototype.clearModels = function(models, cb) {
if(!this.connected) {
return new Error('Not connected to db, exiting function');
}

var modelNames = [];

// Convert to array if not already
if (Array.isArray(models)) {
modelNames = models;
} else if (typeof(models) === 'string') {
modelNames.push(models);
} else {
console.error(chalk.red('Error: Invalid model type'));
return;
}

// Confirm that all Models have been registered in Mongoose
var invalidModels = this.invalidModelCheck(modelNames, function(err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}

// Clear each model
async.each(modelNames, function(modelName, done) {
var Model = mongoose.model(modelName);
Model.remove({}, function(err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}
console.log(modelName + 's collection cleared');
done();
});
}, function(err) {
// Final async callback
if(err) { return; }
cb();
});
});
};

Seeder.prototype.populateModels = function(seedData) {
if(!this.connected) {
return new Error('Not connected to db, exiting function');
}

var modelNames = _.unique(_.pluck(seedData,'model'));

// Confirm that all Models have been registered in Mongoose
var invalidModels = this.invalidModelCheck(modelNames, function(err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}

// Populate each model
seedData.forEach(function(entry) {
var Model = mongoose.model(entry.model);
entry.documents.forEach(function(document, j) {
Model.create(document, function(err) {
if (err) {
console.error(chalk.red('Error creating document [' + j + '] of ' + entry.model + ' model'));
console.error(chalk.red('Error: ' + err.message));
return;
}
console.log('Successfully created document [' + j + '] of ' + entry.model + ' model');
});
});
});

});
};

module.exports = new Seeder();
25 changes: 25 additions & 0 deletions node_modules/async/.jshintrc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions node_modules/async/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions node_modules/async/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions node_modules/async/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 397a283

Please sign in to comment.