Can use multiple modifiers at once

pull/2/head
Louis Chatriot 12 years ago
parent 1a95ddb27a
commit f7c8687009
  1. 2
      lib/model.js
  2. 38
      test/db.test.js

@ -197,6 +197,7 @@ function modify (obj, updateQuery) {
} else { } else {
// Apply modifiers // Apply modifiers
modifiers = _.uniq(keys); modifiers = _.uniq(keys);
newDoc = deepCopy(obj);
modifiers.forEach(function (m) { modifiers.forEach(function (m) {
var keys; var keys;
@ -208,7 +209,6 @@ function modify (obj, updateQuery) {
throw "Modifier " + m + "'s argument must be an object"; throw "Modifier " + m + "'s argument must be an object";
} }
newDoc = deepCopy(obj);
keys.forEach(function (k) { keys.forEach(function (k) {
modifierFunctions[m](newDoc, k, updateQuery[m][k]); modifierFunctions[m](newDoc, k, updateQuery[m][k]);
}); });

@ -422,14 +422,50 @@ describe('Database', function () {
}); });
}); });
it('Cannot perform update if the new document contains a field beginning by $', function (done) { it('Cannot perform update if the update query is not either registered-modifiers-only or copy-only, or contain badly formatted fields', function (done) {
d.insert({ something: 'yup' }, function () { d.insert({ something: 'yup' }, function () {
d.update({}, { boom: { $badfield: 5 } }, { multi: false }, function (err) { d.update({}, { boom: { $badfield: 5 } }, { multi: false }, function (err) {
assert.isDefined(err); assert.isDefined(err);
d.update({}, { boom: { "bad.field": 5 } }, { multi: false }, function (err) {
assert.isDefined(err);
d.update({}, { $inc: { test: 5 }, mixed: 'rrr' }, { multi: false }, function (err) {
assert.isDefined(err);
d.update({}, { $inexistent: { test: 5 } }, { multi: false }, function (err) {
assert.isDefined(err);
done(); done();
}); });
}); });
}); });
});
});
});
it('Can update documents using multiple modifiers', function (done) {
var id;
d.insert({ something: 'yup', other: 40 }, function (err, newDoc) {
id = newDoc._id;
d.update({}, { $set: { something: 'changed' }, $inc: { other: 10 } }, { multi: false }, function (err, nr) {
assert.isNull(err);
nr.should.equal(1);
d.findOne({ _id: id }, function (err, doc) {
Object.keys(doc).length.should.equal(3);
doc._id.should.equal(id);
doc.something.should.equal('changed');
doc.other.should.equal(50);
done();
});
});
});
});
}); // ==== End of 'Update' ==== // }); // ==== End of 'Update' ==== //

Loading…
Cancel
Save