Cannot mix behaviour and cannot use non registered modifiers

pull/2/head
Louis Chatriot 12 years ago
parent 8d4182067b
commit 6586c7f81a
  1. 35
      lib/model.js
  2. 20
      test/model.test.js

@ -7,6 +7,8 @@
var dateToJSON = function () { return { $$date: this.getTime() }; } var dateToJSON = function () { return { $$date: this.getTime() }; }
, originalDateToJSON = Date.prototype.toJSON , originalDateToJSON = Date.prototype.toJSON
, util = require('util') , util = require('util')
, _ = require('underscore')
, modifierFunctions = {}
; ;
@ -123,19 +125,42 @@ function deepCopy (obj) {
} }
modifierFunctions.$inc = function () {};
modifierFunctions.$set = function () {};
/** /**
* Modify a DB object according to an update query * Modify a DB object according to an update query
* For now the updateQuery only replaces the object * For now the updateQuery only replaces the object
*/ */
function modify (obj, updateQuery) { function modify (obj, updateQuery) {
if (updateQuery._id) { throw "You cannot change a document's _id"; } var keys = Object.keys(updateQuery)
, firstChars = _.map(keys, function (item) { return item[0]; })
, dollarFirstChars = _.filter(firstChars, function (c) { return c === '$'; })
, newDoc, modifiers
;
updateQuery = deepCopy(updateQuery); if (keys.indexOf('_id') !== -1) { throw "You cannot change a document's _id"; }
updateQuery._id = obj._id;
checkObject(updateQuery); if (dollarFirstChars.length !== 0 && dollarFirstChars.length !== firstChars.length) {
throw "You cannot mix modifiers and normal fields";
}
if (dollarFirstChars.length === 0) {
// Simply replace the object with the update query contents
newDoc = deepCopy(updateQuery);
newDoc._id = obj._id;
} else {
// Apply modifiers
modifiers = _.uniq(keys);
modifiers.forEach(function (m) {
if (!modifierFunctions[m]) { throw "Unknown modifier " + m; }
});
}
return updateQuery; // Check result is valid and return it
checkObject(newDoc);
return newDoc;
}; };

@ -212,7 +212,7 @@ describe('Model', function () {
t._id.should.equal('keepit'); t._id.should.equal('keepit');
}); });
it('Raise an error if trying to replace the _id field in a copy-type modification', function () { it('Throw an error if trying to replace the _id field in a copy-type modification', function () {
var obj = { some: 'thing', _id: 'keepit' } var obj = { some: 'thing', _id: 'keepit' }
, updateQuery = { replace: 'done', bloup: [ 1, 8], _id: 'donttryit' } , updateQuery = { replace: 'done', bloup: [ 1, 8], _id: 'donttryit' }
; ;
@ -222,6 +222,24 @@ describe('Model', function () {
}).should.throw(); }).should.throw();
}); });
it('Throw an error if trying to use modify in a mixed copy+modify way', function () {
var obj = { some: 'thing' }
, updateQuery = { replace: 'me', $modify: 'metoo' };
(function () {
model.modify(obj, updateQuery);
}).should.throw();
});
it('Throw an error if trying to use an inexistent modifier', function () {
var obj = { some: 'thing' }
, updateQuery = { $set: 'this exists', $modify: 'not this one' };
(function () {
model.modify(obj, updateQuery);
}).should.throw();
});
}); // ==== End of 'Modifying documents' ==== // }); // ==== End of 'Modifying documents' ==== //
}); });

Loading…
Cancel
Save