Skip to content

Commit

Permalink
Updated packages
Browse files Browse the repository at this point in the history
  • Loading branch information
athahersirnaik committed Oct 27, 2021
1 parent f5cec22 commit 91212bc
Show file tree
Hide file tree
Showing 4 changed files with 2,324 additions and 65 deletions.
67 changes: 28 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ Seeder.prototype.setLogOutput = function (logOutput) {
this.consoleLogEnabled = logOutput;
};

Seeder.prototype.connect = function(...params) {
Seeder.prototype.connect = function (...params) {
var _this = this;
/*
switch (mongoose.connection.readyState) {
case 0 : Disconnected;
case 1 : Connected;
case 2 : Connecting;
case 3 : Disconnecting;
}
source http://mongoosejs.com/docs/api.html#connection_Connection-readyState
*/
switch (mongoose.connection.readyState) {
case 0 : Disconnected;
case 1 : Connected;
case 2 : Connecting;
case 3 : Disconnecting;
}
source http://mongoosejs.com/docs/api.html#connection_Connection-readyState
*/

var db, cb, opts = null;

Expand All @@ -49,23 +49,12 @@ Seeder.prototype.connect = function(...params) {
console.error('Pass either 2 or 3 arguments to seeder.connect');
process.exit(1);
}

mongoose.set("useCreateIndex", true);
mongoose.set("useNewUrlParser", true);


if (mongoose.connection.readyState === 1) {
_this.connected = true;
consoleLog(_this, 'Successfully initialized mongoose-seed');
cb();
} else {
if (opts) {
opts.useNewUrlParser = true;
} else {
opts = {
useNewUrlParser: true
};
}

mongoose.connect(db, opts, function (err) {
afterConnect(_this, err, cb);
});
Expand All @@ -84,20 +73,20 @@ function afterConnect(_this, err, cb) {
}
}

Seeder.prototype.loadModels = function(modelPaths) {
Seeder.prototype.loadModels = function (modelPaths) {
consoleLog(this, modelPaths);
modelPaths.forEach(function(modelPath) {
modelPaths.forEach(function (modelPath) {
var model = require(path.resolve(modelPath));
if (model instanceof Function) {
model();
}
});
};

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

models.forEach(function(model) {
models.forEach(function (model) {
if (_.indexOf(mongoose.modelNames(), model) === -1) {
invalidModels.push(model);
}
Expand All @@ -110,7 +99,7 @@ Seeder.prototype.invalidModelCheck = function(models, cb) {
}
};

Seeder.prototype.clearModels = function(models, cb) {
Seeder.prototype.clearModels = function (models, cb) {
if (!this.connected) {
return new Error('Not connected to db, exiting function');
}
Expand All @@ -121,32 +110,32 @@ Seeder.prototype.clearModels = function(models, cb) {
// Convert to array if not already
if (Array.isArray(models)) {
modelNames = models;
} else if (typeof(models) === 'string') {
} 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
this.invalidModelCheck(modelNames, function(err) {
this.invalidModelCheck(modelNames, function (err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}

// Clear each model
async.each(modelNames, function(modelName, done) {
async.each(modelNames, function (modelName, done) {
var Model = mongoose.model(modelName);
Model.deleteMany({}, function(err) {
Model.deleteMany({}, function (err) {
if (err) {
console.error(chalk.red('Error: ' + err.message));
return;
}
consoleLog(_this, modelName + 's collection cleared');
done();
});
}, function(err) {
}, function (err) {
// Final async callback
if (err) {
return;
Expand All @@ -156,7 +145,7 @@ Seeder.prototype.clearModels = function(models, cb) {
});
};

Seeder.prototype.populateModels = function(seedData, cb) {
Seeder.prototype.populateModels = function (seedData, cb) {
if (!this.connected) {
return new Error('Not connected to db, exiting function');
}
Expand All @@ -165,17 +154,17 @@ Seeder.prototype.populateModels = function(seedData, cb) {
var _this = this;

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

// Populate each model
async.eachOf(seedData, function(entry, i, outerCallback) {
async.eachOf(seedData, function (entry, i, outerCallback) {
var Model = mongoose.model(entry.model);
async.eachOf(entry.documents, function(document, j, innerCallback) {
Model.create(document, function(err) {
async.eachOf(entry.documents, function (document, j, innerCallback) {
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));
Expand All @@ -184,17 +173,17 @@ Seeder.prototype.populateModels = function(seedData, cb) {
}
innerCallback();
});
}, function(err) {
}, function (err) {
outerCallback();
});
}, function(err) {
}, function (err) {
cb();
});
});
};

Seeder.prototype.disconnect = function () {
mongoose.disconnect();
mongoose.disconnect();
};

module.exports = new Seeder();
Loading

0 comments on commit 91212bc

Please sign in to comment.