forked from seanemmer/mongoose-seed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 397a283
Showing
2,207 changed files
with
310,707 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.