Skip to content

Commit 7d78a75

Browse files
committed
feat(query): add PoC for runSetters option
Re: #4569
1 parent ea67c82 commit 7d78a75

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

lib/schema.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@ Schema.interpretAsType = function(path, obj, options) {
689689
'You can only nest using refs or arrays.');
690690
}
691691

692+
obj = utils.clone(obj);
693+
if (!('runSetters' in obj)) {
694+
obj.runSetters = options.runSetters;
695+
}
692696
return new MongooseTypes[name](path, obj);
693697
};
694698

lib/schema/string.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ SchemaString.prototype.castForQuery = function($conditional, val) {
506506
if (Object.prototype.toString.call(val) === '[object RegExp]') {
507507
return val;
508508
}
509+
if (this.options && this.options.runSetters) {
510+
return this.applySetters(val, null);
511+
}
509512
return this.cast(val);
510513
};
511514

test/model.query.casting.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,26 @@ describe('model query casting', function() {
10211021
catch(done);
10221022
});
10231023

1024+
it('lowercase in query (gh-4569)', function(done) {
1025+
var db = start();
1026+
1027+
var testSchema = new Schema({
1028+
name: { type: String, lowercase: true }
1029+
}, { runSetters: true });
1030+
1031+
var Test = db.model('gh-4569', testSchema);
1032+
Test.create({ name: 'val' }).
1033+
then(function() {
1034+
return Test.findOne({ name: 'VAL' });
1035+
}).
1036+
then(function(doc) {
1037+
assert.ok(doc);
1038+
assert.equal(doc.name, 'val');
1039+
done();
1040+
}).
1041+
catch(done);
1042+
});
1043+
10241044
it('_id = 0 (gh-4610)', function(done) {
10251045
var db = start();
10261046

0 commit comments

Comments
 (0)