Skip to content

Commit

Permalink
Merge pull request #5252 from Automattic/4569
Browse files Browse the repository at this point in the history
Run setters on query option
  • Loading branch information
vkarpov15 authored May 18, 2017
2 parents 636e922 + 214323d commit f3805fa
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 38 deletions.
4 changes: 4 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@ Schema.interpretAsType = function(path, obj, options) {
'You can only nest using refs or arrays.');
}

obj = utils.clone(obj, { retainKeyOrder: true });
if (!('runSettersOnQuery' in obj)) {
obj.runSettersOnQuery = options.runSettersOnQuery;
}
return new MongooseTypes[name](path, obj);
};

Expand Down
4 changes: 2 additions & 2 deletions lib/schema/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ SchemaBoolean.prototype.castForQuery = function($conditional, val) {
return handler.call(this, val);
}

return this.cast(val);
return this._castForQuery(val);
}

return this.cast($conditional);
return this._castForQuery($conditional);
};

/*!
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ SchemaBuffer.prototype.castForQuery = function($conditional, val) {
return handler.call(this, val);
}
val = $conditional;
var casted = this.cast(val);
var casted = this._castForQuery(val);
return casted ? casted.toObject({ transform: false, virtuals: false }) : casted;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/schema/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ SchemaDate.prototype.castForQuery = function($conditional, val) {
var handler;

if (arguments.length !== 2) {
return this.cast($conditional);
return this._castForQuery($conditional);
}

handler = this.$conditionalHandlers[$conditional];
Expand Down
20 changes: 0 additions & 20 deletions lib/schema/decimal128.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,6 @@ Decimal128.prototype.$conditionalHandlers =
$lte: handleSingle
});

/**
* Casts contents for queries.
*
* @param {String} $conditional
* @param {any} [val]
* @api private
*/

Decimal128.prototype.castForQuery = function($conditional, val) {
var handler;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];
if (!handler) {
throw new Error('Can\'t use ' + $conditional + ' with ObjectId.');
}
return handler.call(this, val);
}
return this.cast($conditional);
};

/*!
* Module exports.
*/
Expand Down
4 changes: 4 additions & 0 deletions lib/schema/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ Embedded.prototype.castForQuery = function($conditional, val) {
return val;
}

if (this.options.runSetters) {
val = this._applySetters(val);
}

return new this.caster(val);
};

Expand Down
2 changes: 1 addition & 1 deletion lib/schema/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ SchemaNumber.prototype.castForQuery = function($conditional, val) {
}
return handler.call(this, val);
}
val = this.cast($conditional);
val = this._castForQuery($conditional);
return val;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/schema/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ ObjectId.prototype.castForQuery = function($conditional, val) {
}
return handler.call(this, val);
}
return this.cast($conditional);
return this._castForQuery($conditional);
};

/*!
Expand Down
3 changes: 2 additions & 1 deletion lib/schema/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ SchemaString.prototype.castForQuery = function($conditional, val) {
if (Object.prototype.toString.call(val) === '[object RegExp]') {
return val;
}
return this.cast(val);

return this._castForQuery(val);
};

/*!
Expand Down
47 changes: 36 additions & 11 deletions lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,20 +621,17 @@ SchemaType.prototype.getDefault = function(scope, init) {
return ret;
};

/**
* Applies setters
/*!
* Applies setters without casting
*
* @param {Object} value
* @param {Object} scope
* @param {Boolean} init
* @api private
*/

SchemaType.prototype.applySetters = function(value, scope, init, priorVal, options) {
var v = value,
setters = this.setters,
len = setters.length,
caster = this.caster;
SchemaType.prototype._applySetters = function(value, scope, init, priorVal) {
var v = value;
var setters = this.setters;
var len = setters.length;
var caster = this.caster;

while (len--) {
v = setters[len].call(scope, v, this);
Expand All @@ -648,7 +645,22 @@ SchemaType.prototype.applySetters = function(value, scope, init, priorVal, optio
v = newVal;
}

if (v === null || v === undefined) {
return v;
};

/**
* Applies setters
*
* @param {Object} value
* @param {Object} scope
* @param {Boolean} init
* @api private
*/

SchemaType.prototype.applySetters = function(value, scope, init, priorVal, options) {
var v = this._applySetters(value, scope, init, priorVal, options);

if (v == null) {
return v;
}

Expand Down Expand Up @@ -995,6 +1007,19 @@ SchemaType.prototype.castForQuery = function($conditional, val) {
return handler.call(this, val);
}
val = $conditional;
return this._castForQuery(val);
};

/*!
* Internal switch for runSetters
*
* @api private
*/

SchemaType.prototype._castForQuery = function(val) {
if (this.options && this.options.runSettersOnQuery) {
return this.applySetters(val, null);
}
return this.cast(val);
};

Expand Down
28 changes: 28 additions & 0 deletions test/model.query.casting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,34 @@ describe('model query casting', function() {
catch(done);
});

it('lowercase in query (gh-4569)', function(done) {
var db = start();

var testSchema = new Schema({
name: { type: String, lowercase: true },
num: { type: Number, set: function(v) { return Math.floor(v); } }
}, { runSettersOnQuery: true });

var Test = db.model('gh-4569', testSchema);
Test.create({ name: 'val', num: 3 }).
then(function() {
return Test.findOne({ name: 'VAL' });
}).
then(function(doc) {
assert.ok(doc);
assert.equal(doc.name, 'val');
}).
then(function() {
return Test.findOne({ num: 3.14 });
}).
then(function(doc) {
assert.ok(doc);
assert.equal(doc.name, 'val');
}).
then(function() { done(); }).
catch(done);
});

it('_id = 0 (gh-4610)', function(done) {
var db = start();

Expand Down

0 comments on commit f3805fa

Please sign in to comment.