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
+1 -4
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);
}
+2 -1
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,
+1 -1
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;
}
+17 -14
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