Skip to content

Commit 27b4941

Browse files
committed
feat(seed): refactoring seeding functionality to be exported by the mongoose helper library and used as a gulp task, rather than the server bootstrap method
1 parent 3a36de1 commit 27b4941

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

server/config/lib/app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ function startMongoose() {
3232
return new Promise(function (resolve, reject) {
3333
mongoose.loadModels()
3434
.then(mongoose.connect)
35-
.then(mongoose.seed)
3635
.then(function(dbConnection) {
3736
resolve(dbConnection);
3837
})

server/config/lib/mongoose.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Module dependencies.
55
*/
6-
var config = require('../config'),
6+
const config = require('../config'),
77
chalk = require('chalk'),
88
path = require('path'),
99
mongoose = require('mongoose'),
@@ -21,24 +21,28 @@ module.exports.loadModels = function() {
2121

2222
resolve();
2323
});
24-
}
24+
};
2525

2626
/**
2727
* seed the database with default data
2828
* @param {object} dbConnection the mongoose connection
2929
*/
3030
module.exports.seed = function(dbConnection) {
3131
return new Promise(function (resolve, reject) {
32-
if (config.seedDB && config.seedDB.seed) {
33-
console.log(chalk.bold.red('Warning: Database seeding is turned on'));
34-
seed.start().then(function() {
35-
resolve(dbConnection);
36-
});
37-
} else {
38-
resolve(dbConnection);
32+
33+
if (!dbConnection) {
34+
reject(new Error('dbConnection parameter is falsey'));
3935
}
36+
37+
dbConnection.connection.db.dropDatabase(function (error, result) {
38+
if (error) {
39+
reject(error);
40+
}
41+
42+
resolve();
43+
});
4044
});
41-
}
45+
};
4246

4347
/**
4448
* Connect to the MongoDB server
@@ -66,7 +70,7 @@ module.exports.connect = function() {
6670
reject(err);
6771
})
6872
});
69-
}
73+
};
7074

7175
/**
7276
* Disconnect from the MongoDB server

server/gulpfile.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ gulp.task('ava:test:integration', function() {
210210
});
211211
});
212212

213+
gulp.task('seed:mongoose', function(done) {
214+
const mongoose = require('./config/lib/mongoose');
215+
216+
mongoose.connect()
217+
.then(mongoose.seed)
218+
.then(function() {
219+
done();
220+
});
221+
});
222+
213223
gulp.task('test:integration', function(done) {
214224
runSequence('env:test', 'server:bootstrap', 'ava:test:integration', done);
215225
});

0 commit comments

Comments
 (0)