From dc880eb5a77168099b214f1733ed0d6d23ff7093 Mon Sep 17 00:00:00 2001 From: Michael Leanos Date: Thu, 27 Jul 2017 13:19:22 -0700 Subject: [PATCH] feat(config): Mongoose 4.11 upgrade (#1818) 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 https://github.com/meanjs/mean/pull/1816 Closes https://github.com/meanjs/mean/issues/1814 --- config/env/development.js | 5 +-- config/env/local.example.js | 5 +-- config/env/production.js | 2 -- config/env/test.js | 5 +-- config/lib/app.js | 10 +++--- config/lib/express.js | 6 ++-- config/lib/mongoose.js | 36 ++++++++++--------- config/lib/socket.io.js | 2 +- gulpfile.js | 31 +++++++++------- .../server/config/articles.server.config.js | 2 +- .../admin.article.server.routes.tests.js | 2 +- .../server/article.server.routes.tests.js | 14 ++++---- .../tests/server/core.server.config.tests.js | 2 +- .../server/config/users.server.config.js | 2 +- .../tests/server/user.server.routes.tests.js | 2 +- package.json | 2 +- 16 files changed, 62 insertions(+), 66 deletions(-) diff --git a/config/env/development.js b/config/env/development.js index da024eb886..42cbecf387 100644 --- a/config/env/development.js +++ b/config/env/development.js @@ -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 }, diff --git a/config/env/local.example.js b/config/env/local.example.js index 7a8641ff59..2f4aeabf2f 100644 --- a/config/env/local.example.js +++ b/config/env/local.example.js @@ -20,10 +20,7 @@ module.exports = { db: { uri: 'mongodb://localhost/local-dev', - options: { - user: '', - pass: '' - } + options: {} }, sessionSecret: process.env.SESSION_SECRET || 'youshouldchangethistosomethingsecret', facebook: { diff --git a/config/env/production.js b/config/env/production.js index c1124b0815..c43281fa27 100644 --- a/config/env/production.js +++ b/config/env/production.js @@ -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 diff --git a/config/env/test.js b/config/env/test.js index 845be38d34..0cbfb6bc84 100644 --- a/config/env/test.js +++ b/config/env/test.js @@ -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 }, diff --git a/config/lib/app.js b/config/lib/app.js index 5c4d13095f..3321e78e76 100644 --- a/config/lib/app.js +++ b/config/lib/app.js @@ -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'); @@ -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); diff --git a/config/lib/express.js b/config/lib/express.js index 9a7d999625..3df5eaf06f 100644 --- a/config/lib/express.js +++ b/config/lib/express.js @@ -118,7 +118,7 @@ module.exports.initSession = function (app, db) { }, name: config.sessionKey, store: new MongoStore({ - mongooseConnection: db.connection, + db: db, collection: config.sessionCollection }) })); @@ -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); }); }; diff --git a/config/lib/mongoose.js b/config/lib/mongoose.js index 17fae2c234..86554bda25 100644 --- a/config/lib/mongoose.js +++ b/config/lib/mongoose.js @@ -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'); @@ -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); + }); }; diff --git a/config/lib/socket.io.js b/config/lib/socket.io.js index 0050f4fb86..03da0dd89c 100644 --- a/config/lib/socket.io.js +++ b/config/lib/socket.io.js @@ -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 }); diff --git a/gulpfile.js b/gulpfile.js index a578b682fb..870d5015bd 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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', @@ -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); }); }); }); @@ -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); }); }); }); diff --git a/modules/articles/server/config/articles.server.config.js b/modules/articles/server/config/articles.server.config.js index 629c360646..1e5355ca77 100644 --- a/modules/articles/server/config/articles.server.config.js +++ b/modules/articles/server/config/articles.server.config.js @@ -9,6 +9,6 @@ var path = require('path'), /** * Module init function. */ -module.exports = function (app, db) { +module.exports = function (app) { }; diff --git a/modules/articles/tests/server/admin.article.server.routes.tests.js b/modules/articles/tests/server/admin.article.server.routes.tests.js index ea9b9e14f1..ba080eb2f9 100644 --- a/modules/articles/tests/server/admin.article.server.routes.tests.js +++ b/modules/articles/tests/server/admin.article.server.routes.tests.js @@ -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(); diff --git a/modules/articles/tests/server/article.server.routes.tests.js b/modules/articles/tests/server/article.server.routes.tests.js index 0c23a52150..6bc0523520 100644 --- a/modules/articles/tests/server/article.server.routes.tests.js +++ b/modules/articles/tests/server/article.server.routes.tests.js @@ -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(); @@ -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); @@ -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); @@ -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'); @@ -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'); @@ -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 @@ -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); diff --git a/modules/core/tests/server/core.server.config.tests.js b/modules/core/tests/server/core.server.config.tests.js index ab8e2368af..c4f3159ec5 100644 --- a/modules/core/tests/server/core.server.config.tests.js +++ b/modules/core/tests/server/core.server.config.tests.js @@ -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 diff --git a/modules/users/server/config/users.server.config.js b/modules/users/server/config/users.server.config.js index 2bfedceb01..f67857b318 100644 --- a/modules/users/server/config/users.server.config.js +++ b/modules/users/server/config/users.server.config.js @@ -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); diff --git a/modules/users/tests/server/user.server.routes.tests.js b/modules/users/tests/server/user.server.routes.tests.js index 6a51d63362..2dd8441d64 100644 --- a/modules/users/tests/server/user.server.routes.tests.js +++ b/modules/users/tests/server/user.server.routes.tests.js @@ -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(); diff --git a/package.json b/package.json index e4ec91b9a5..c2d14cf52a 100644 --- a/package.json +++ b/package.json @@ -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",