Skip to content

Commit

Permalink
feat(seed): refactoring seeding functionality to be exported by the m…
Browse files Browse the repository at this point in the history
…ongoose helper library and used as a gulp task, rather than the server bootstrap method
  • Loading branch information
lirantal committed Jan 3, 2017
1 parent 3a36de1 commit 27b4941
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
1 change: 0 additions & 1 deletion server/config/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function startMongoose() {
return new Promise(function (resolve, reject) {
mongoose.loadModels()
.then(mongoose.connect)
.then(mongoose.seed)
.then(function(dbConnection) {
resolve(dbConnection);
})
Expand Down
26 changes: 15 additions & 11 deletions server/config/lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Module dependencies.
*/
var config = require('../config'),
const config = require('../config'),
chalk = require('chalk'),
path = require('path'),
mongoose = require('mongoose'),
Expand All @@ -21,24 +21,28 @@ module.exports.loadModels = function() {

resolve();
});
}
};

/**
* seed the database with default data
* @param {object} dbConnection the mongoose connection
*/
module.exports.seed = function(dbConnection) {
return new Promise(function (resolve, reject) {
if (config.seedDB && config.seedDB.seed) {
console.log(chalk.bold.red('Warning: Database seeding is turned on'));
seed.start().then(function() {
resolve(dbConnection);
});
} else {
resolve(dbConnection);

if (!dbConnection) {
reject(new Error('dbConnection parameter is falsey'));
}

dbConnection.connection.db.dropDatabase(function (error, result) {
if (error) {
reject(error);
}

resolve();
});
});
}
};

/**
* Connect to the MongoDB server
Expand Down Expand Up @@ -66,7 +70,7 @@ module.exports.connect = function() {
reject(err);
})
});
}
};

/**
* Disconnect from the MongoDB server
Expand Down
10 changes: 10 additions & 0 deletions server/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ gulp.task('ava:test:integration', function() {
});
});

gulp.task('seed:mongoose', function(done) {
const mongoose = require('./config/lib/mongoose');

mongoose.connect()
.then(mongoose.seed)
.then(function() {
done();
});
});

gulp.task('test:integration', function(done) {
runSequence('env:test', 'server:bootstrap', 'ava:test:integration', done);
});
Expand Down

0 comments on commit 27b4941

Please sign in to comment.