Skip to content

Commit

Permalink
Enable Model.settings.mongodb.allowExtendedOperators
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien committed Jul 23, 2015
1 parent 686d2b6 commit fdb3448
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/mongodb.js
Expand Up @@ -385,8 +385,18 @@ MongoDB.prototype.find = function find(model, id, options, callback) {
*/
MongoDB.prototype.parseUpdateData = function(model, data) {
var parsedData = {};

if (this.settings.allowExtendedOperators === true) {

var modelClass = this._models[model];

var allowExtendedOperators = this.settings.allowExtendedOperators;
if (allowExtendedOperators !== false && modelClass.settings.mongodb
&& modelClass.settings.mongodb.hasOwnProperty('allowExtendedOperators')) {
allowExtendedOperators = modelClass.settings.mongodb.allowExtendedOperators === true;
} else if (allowExtendedOperators === true) {
allowExtendedOperators = true;
}

if (allowExtendedOperators) {
// Check for other operators and sanitize the data obj
var acceptedOperators = [
// Field operators
Expand Down
52 changes: 52 additions & 0 deletions test/mongodb.test.js
Expand Up @@ -82,6 +82,7 @@ describe('mongodb connector', function () {
});

beforeEach(function (done) {
User.settings.mongodb = {};
User.destroyAll(function () {
Post.destroyAll(function () {
PostWithObjectId.destroyAll(function () {
Expand Down Expand Up @@ -558,6 +559,57 @@ describe('mongodb connector', function () {
});
});
});

it('should be possible to enable per model settings', function(done) {
User.dataSource.settings.allowExtendedOperators = null;
User.settings.mongodb = { allowExtendedOperators: true };
User.create({name: 'Al', age: 31, email: 'al@strongloop'}, function(err1, createdusers1) {
should.not.exist(err1);

User.updateAll({name: 'Al'}, {'$rename': {name: 'firstname'}}, function(err, updatedusers) {
should.not.exist(err);
updatedusers.should.have.property('count', 1);

User.find({where: {firstname: 'Al'}}, function(err, foundusers) {
should.not.exist(err);
foundusers.length.should.be.equal(1);

done();
});

});
});
});

it('should not be possible to enable per model settings when globally disabled', function(done) {
User.dataSource.settings.allowExtendedOperators = false;
User.settings.mongodb = { allowExtendedOperators: true };
User.create({name: 'Al', age: 31, email: 'al@strongloop'}, function(err1, createdusers1) {
should.not.exist(err1);

User.updateAll({name: 'Al'}, {'$rename': {name: 'firstname'}}, function(err, updatedusers) {
should.exist(err);
err.name.should.equal('MongoError');
err.errmsg.should.equal('The dollar ($) prefixed field \'$rename\' in \'$rename\' is not valid for storage.');
done();
});
});
});

it('should not be possible to use when disabled per model settings', function(done) {
User.dataSource.settings.allowExtendedOperators = true;
User.settings.mongodb = { allowExtendedOperators: false };
User.create({name: 'Al', age: 31, email: 'al@strongloop'}, function(err1, createdusers1) {
should.not.exist(err1);

User.updateAll({name: 'Al'}, {'$rename': {name: 'firstname'}}, function(err, updatedusers) {
should.exist(err);
err.name.should.equal('MongoError');
err.errmsg.should.equal('The dollar ($) prefixed field \'$rename\' in \'$rename\' is not valid for storage.');
done();
});
});
});

it('should be possible to use the $inc operator', function(done) {
User.dataSource.settings.allowExtendedOperators = true;
Expand Down

0 comments on commit fdb3448

Please sign in to comment.