Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
feat(config): Mongoose 4.11 upgrade (#1818)
Browse files Browse the repository at this point in the history
Upgrades Mongoose to 4.11.1

Updates Mongoose connection implementation to accommodate deprecated
features & connection options.

Also, updates the Gulp Mocha tasks to reflect changes to the Mongoose
implementation.

Fixes tests to get the database from the existing Mongoose singleton.

Derives from changes in #1816

Closes #1814
  • Loading branch information
mleanos authored Jul 27, 2017
1 parent 1177746 commit dc880eb
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 66 deletions.
5 changes: 1 addition & 4 deletions config/env/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ var defaultEnvConfig = require('./default');
module.exports = {
db: {
uri: process.env.MONGOHQ_URL || process.env.MONGODB_URI || 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost') + '/mean-dev',
options: {
user: '',
pass: ''
},
options: {},
// Enable mongoose debug mode
debug: process.env.MONGODB_DEBUG || false
},
Expand Down
5 changes: 1 addition & 4 deletions config/env/local.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
module.exports = {
db: {
uri: 'mongodb://localhost/local-dev',
options: {
user: '',
pass: ''
}
options: {}
},
sessionSecret: process.env.SESSION_SECRET || 'youshouldchangethistosomethingsecret',
facebook: {
Expand Down
2 changes: 0 additions & 2 deletions config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ module.exports = {
db: {
uri: process.env.MONGOHQ_URL || process.env.MONGODB_URI || 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost') + '/mean',
options: {
user: '',
pass: ''
/**
* Uncomment to enable ssl certificate based authentication to mongodb
* servers. Adjust the settings below for your specific certificate
Expand Down
5 changes: 1 addition & 4 deletions config/env/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ var defaultEnvConfig = require('./default');
module.exports = {
db: {
uri: process.env.MONGOHQ_URL || process.env.MONGODB_URI || 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost') + '/mean-test',
options: {
user: '',
pass: ''
},
options: {},
// Enable mongoose debug mode
debug: process.env.MONGODB_DEBUG || false
},
Expand Down
10 changes: 5 additions & 5 deletions config/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/
var config = require('../config'),
mongoose = require('./mongoose'),
mongooseService = require('./mongoose'),
express = require('./express'),
chalk = require('chalk'),
seed = require('./seed');
Expand All @@ -16,11 +16,11 @@ function seedDB() {
}
}

// Initialize Models
mongoose.loadModels(seedDB);

module.exports.init = function init(callback) {
mongoose.connect(function (db) {
mongooseService.connect(function (db) {
// Initialize Models
mongooseService.loadModels(seedDB);

// Initialize express
var app = express.init(db);
if (callback) callback(app, db, config);
Expand Down
6 changes: 3 additions & 3 deletions config/lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ module.exports.initSession = function (app, db) {
},
name: config.sessionKey,
store: new MongoStore({
mongooseConnection: db.connection,
db: db,
collection: config.sessionCollection
})
}));
Expand All @@ -130,9 +130,9 @@ module.exports.initSession = function (app, db) {
/**
* Invoke modules server configuration
*/
module.exports.initModulesConfiguration = function (app, db) {
module.exports.initModulesConfiguration = function (app) {
config.files.server.configs.forEach(function (configPath) {
require(path.resolve(configPath))(app, db);
require(path.resolve(configPath))(app);
});
};

Expand Down
36 changes: 19 additions & 17 deletions config/lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/**
* Module dependencies.
*/
var config = require('../config'),
var _ = require('lodash'),
config = require('../config'),
chalk = require('chalk'),
path = require('path'),
mongoose = require('mongoose');
Expand All @@ -19,30 +20,31 @@ module.exports.loadModels = function (callback) {
};

// Initialize Mongoose
module.exports.connect = function (cb) {
var _this = this;

module.exports.connect = function (callback) {
mongoose.Promise = config.db.promise;

var db = mongoose.connect(config.db.uri, config.db.options, function (err) {
// Log Error
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(err);
} else {
var options = _.merge(config.db.options || {}, { useMongoClient: true });

mongoose
.connect(config.db.uri, options)
.then(function (connection) {
// Enabling mongoose debug mode if required
mongoose.set('debug', config.db.debug);

// Call callback FN
if (cb) cb(db);
}
});
if (callback) callback(connection.db);
})
.catch(function (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(err);
});

};

module.exports.disconnect = function (cb) {
mongoose.disconnect(function (err) {
console.info(chalk.yellow('Disconnected from MongoDB.'));
cb(err);
});
mongoose.connection.db
.close(function (err) {
console.info(chalk.yellow('Disconnected from MongoDB.'));
return cb(err);
});
};
2 changes: 1 addition & 1 deletion config/lib/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = function (app, db) {

// Create a MongoDB storage object
var mongoStore = new MongoStore({
mongooseConnection: db.connection,
db: db,
collection: config.sessionCollection
});

Expand Down
31 changes: 18 additions & 13 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ gulp.task('templatecache', function () {

// Mocha tests task
gulp.task('mocha', function (done) {
// Open mongoose connections
var mongoose = require('./config/lib/mongoose.js');
var mongooseService = require('./config/lib/mongoose');
var testSuites = changedTestFiles.length ? changedTestFiles : testAssets.tests.server;
var error;

// Connect mongoose
mongoose.connect(function () {
mongoose.loadModels();
// Run the tests
mongooseService.connect(function (db) {
// Load mongoose models
mongooseService.loadModels();

gulp.src(testSuites)
.pipe(plugins.mocha({
reporter: 'spec',
Expand All @@ -296,9 +296,13 @@ gulp.task('mocha', function (done) {
error = err;
})
.on('end', function () {
// When the tests are done, disconnect mongoose and pass the error state back to gulp
mongoose.disconnect(function () {
done(error);
mongooseService.disconnect(function (err) {
if (err) {
console.log('Error disconnecting from database');
console.log(err);
}

return done(error);
});
});
});
Expand Down Expand Up @@ -362,16 +366,17 @@ gulp.task('karma:coverage', function(done) {
// Drops the MongoDB database, used in e2e testing
gulp.task('dropdb', function (done) {
// Use mongoose configuration
var mongoose = require('./config/lib/mongoose.js');
var mongooseService = require('./config/lib/mongoose');

mongoose.connect(function (db) {
db.connection.db.dropDatabase(function (err) {
mongooseService.connect(function (db) {
db.dropDatabase(function (err) {
if (err) {
console.error(err);
} else {
console.log('Successfully dropped db: ', db.connection.db.databaseName);
console.log('Successfully dropped db: ', db.databaseName);
}
db.connection.db.close(done);

mongooseService.disconnect(done);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion modules/articles/server/config/articles.server.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ var path = require('path'),
/**
* Module init function.
*/
module.exports = function (app, db) {
module.exports = function (app) {

};
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var app,
describe('Article Admin CRUD tests', function () {
before(function (done) {
// Get application
app = express.init(mongoose);
app = express.init(mongoose.connection.db);
agent = request.agent(app);

done();
Expand Down
14 changes: 7 additions & 7 deletions modules/articles/tests/server/article.server.routes.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Article CRUD tests', function () {

before(function (done) {
// Get application
app = express.init(mongoose);
app = express.init(mongoose.connection.db);
agent = request.agent(app);

done();
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('Article CRUD tests', function () {
// Save the article
articleObj.save(function () {
// Request articles
request(app).get('/api/articles')
agent.get('/api/articles')
.end(function (req, res) {
// Set assertion
res.body.should.be.instanceof(Array).and.have.lengthOf(1);
Expand All @@ -137,7 +137,7 @@ describe('Article CRUD tests', function () {

// Save the article
articleObj.save(function () {
request(app).get('/api/articles/' + articleObj._id)
agent.get('/api/articles/' + articleObj._id)
.end(function (req, res) {
// Set assertion
res.body.should.be.instanceof(Object).and.have.property('title', article.title);
Expand All @@ -150,7 +150,7 @@ describe('Article CRUD tests', function () {

it('should return proper error for single article with an invalid Id, if not signed in', function (done) {
// test is not a valid mongoose Id
request(app).get('/api/articles/test')
agent.get('/api/articles/test')
.end(function (req, res) {
// Set assertion
res.body.should.be.instanceof(Object).and.have.property('message', 'Article is invalid');
Expand All @@ -162,7 +162,7 @@ describe('Article CRUD tests', function () {

it('should return proper error for single article which doesnt exist, if not signed in', function (done) {
// This is a valid mongoose Id but a non-existent article
request(app).get('/api/articles/559e9cd815f80b4c256a8f41')
agent.get('/api/articles/559e9cd815f80b4c256a8f41')
.end(function (req, res) {
// Set assertion
res.body.should.be.instanceof(Object).and.have.property('message', 'No article with that identifier has been found');
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('Article CRUD tests', function () {
// Save the article
articleObj.save(function () {
// Try deleting article
request(app).delete('/api/articles/' + articleObj._id)
agent.delete('/api/articles/' + articleObj._id)
.expect(403)
.end(function (articleDeleteErr, articleDeleteRes) {
// Set message assertion
Expand Down Expand Up @@ -312,7 +312,7 @@ describe('Article CRUD tests', function () {
if (err) {
return done(err);
}
request(app).get('/api/articles/' + articleObj._id)
agent.get('/api/articles/' + articleObj._id)
.end(function (req, res) {
// Set assertion
res.body.should.be.instanceof(Object).and.have.property('title', article.title);
Expand Down
2 changes: 1 addition & 1 deletion modules/core/tests/server/core.server.config.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ describe('Configuration Tests:', function () {
process.env.NODE_ENV = env;

// Gget application
app = express.init(mongoose);
app = express.init(mongoose.connection.db);
agent = request.agent(app);

// Get rendered layout
Expand Down
2 changes: 1 addition & 1 deletion modules/users/server/config/users.server.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var passport = require('passport'),
/**
* Module init function
*/
module.exports = function (app, db) {
module.exports = function (app) {
// Serialize sessions
passport.serializeUser(function (user, done) {
done(null, user.id);
Expand Down
2 changes: 1 addition & 1 deletion modules/users/tests/server/user.server.routes.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('User CRUD tests', function () {

before(function (done) {
// Get application
app = express.init(mongoose);
app = express.init(mongoose.connection.db);
agent = request.agent(app);

done();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"lusca": "~1.4.1",
"method-override": "~2.3.8",
"mocha": "~3.4.2",
"mongoose": "~4.10.2",
"mongoose": "~4.11.3",
"morgan": "~1.8.1",
"multer": "~1.3.0",
"nodemailer": "~4.0.1",
Expand Down

0 comments on commit dc880eb

Please sign in to comment.