Files
meanTorrent/config/lib/mongoose.js
Michael Leanos dc880eb5a7 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
2017-07-27 13:19:22 -07:00

51 lines
1.2 KiB
JavaScript

'use strict';
/**
* Module dependencies.
*/
var _ = require('lodash'),
config = require('../config'),
chalk = require('chalk'),
path = require('path'),
mongoose = require('mongoose');
// Load the mongoose models
module.exports.loadModels = function (callback) {
// Globbing model files
config.files.server.models.forEach(function (modelPath) {
require(path.resolve(modelPath));
});
if (callback) callback();
};
// Initialize Mongoose
module.exports.connect = function (callback) {
mongoose.Promise = config.db.promise;
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 (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.connection.db
.close(function (err) {
console.info(chalk.yellow('Disconnected from MongoDB.'));
return cb(err);
});
};