Mongoose 4.x (#9928)

* update mongoose to ^4.x

* another fix
This commit is contained in:
Matteo Pagliazzi
2018-02-02 16:37:36 +01:00
committed by GitHub
parent 389d6f18b4
commit 1fbdb7dbd0
8 changed files with 531 additions and 340 deletions

View File

@@ -1,5 +1,4 @@
import mongoose from 'mongoose';
import autoinc from 'mongoose-id-autoinc';
import logger from '../website/server/libs/logger';
import nconf from 'nconf';
import repl from 'repl';
@@ -28,7 +27,6 @@ let improveRepl = (context) => {
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
};
autoinc.init(
mongoose.connect(
nconf.get('NODE_DB_URI'),
mongooseOptions,
@@ -36,7 +34,6 @@ let improveRepl = (context) => {
if (err) throw err;
logger.info('Connected with Mongoose');
}
)
);
};

806
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -67,8 +67,7 @@
"method-override": "^2.3.5",
"moment": "^2.13.0",
"moment-recur": "git://github.com/habitrpg/moment-recur.git#f147ef27bbc26ca67638385f3db4a44084c76626",
"mongoose": "^4.8.6",
"mongoose-id-autoinc": "~2013.7.14-4",
"mongoose": "^4.13.10",
"morgan": "^1.7.0",
"nconf": "~0.8.2",
"node-gcm": "^0.14.4",

View File

@@ -131,7 +131,7 @@ describe('errorHandler', () => {
});
it('handle Mongoose Validation errors', () => {
let error = new Error('User validation failed.');
let error = new Error('User validation failed');
error.name = 'ValidationError';
error.errors = {
@@ -151,7 +151,7 @@ describe('errorHandler', () => {
expect(res.json).to.be.calledWith({
success: false,
error: 'BadRequest',
message: 'User validation failed.',
message: 'User validation failed',
errors: [
{ path: 'auth.local.email', message: 'Invalid email.', value: 'not an email' },
],

View File

@@ -1,6 +1,5 @@
import nconf from 'nconf';
import logger from './logger';
import autoinc from 'mongoose-id-autoinc';
import mongoose from 'mongoose';
import Bluebird from 'bluebird';
@@ -19,10 +18,8 @@ if (MAINTENANCE_MODE !== 'true') {
const NODE_DB_URI = nconf.get('IS_TEST') ? nconf.get('TEST_DB_URI') : nconf.get('NODE_DB_URI');
let db = mongoose.connect(NODE_DB_URI, mongooseOptions, (err) => {
mongoose.connect(NODE_DB_URI, mongooseOptions, (err) => {
if (err) throw err;
logger.info('Connected with Mongoose.');
});
autoinc.init(db);
}

View File

@@ -39,7 +39,8 @@ module.exports = function errorHandler (err, req, res, next) { // eslint-disable
// Handle mongoose validation errors
if (err.name === 'ValidationError') {
responseErr = new BadRequest(err.message); // TODO standard message? translate?
const model = err.message.split(' ')[0];
responseErr = new BadRequest(`${model} validation failed`);
responseErr.errors = map(err.errors, (mongooseErr) => {
return {
message: mongooseErr.message,

View File

@@ -736,7 +736,7 @@ async function _updateUserWithRetries (userId, updates, numTry = 1, query = {})
return raw;
}).catch((err) => {
if (numTry < MAX_UPDATE_RETRIES) {
return _updateUserWithRetries(userId, updates, ++numTry);
return _updateUserWithRetries(userId, updates, ++numTry, query);
} else {
throw err;
}

View File

@@ -54,6 +54,23 @@ export let TaskSchema = new Schema({
return !validator.isUUID(val);
},
msg: 'Task short names cannot be uuids.',
}, {
validator (alias) {
return new Promise((resolve, reject) => {
Task.findOne({ // eslint-disable-line no-use-before-define
_id: { $ne: this._id },
userId: this.userId,
alias,
}).exec().then((task) => {
let aliasAvailable = !task;
return aliasAvailable ? resolve() : reject();
}).catch(() => {
reject();
});
});
},
msg: 'Task alias already used on another task.',
}],
},
tags: [{
@@ -193,20 +210,6 @@ TaskSchema.methods.scoreChallengeTask = async function scoreChallengeTask (delta
export let Task = mongoose.model('Task', TaskSchema);
Task.schema.path('alias').validate(function valiateAliasNotTaken (alias, respond) {
Task.findOne({
_id: { $ne: this._id },
userId: this.userId,
alias,
}).exec().then((task) => {
let aliasAvailable = !task;
respond(aliasAvailable);
}).catch(() => {
respond(false);
});
}, 'Task alias already used on another task.');
// habits and dailies shared fields
let habitDailySchema = () => {
return {history: Array}; // [{date:Date, value:Number}], // this causes major performance problems