diff --git a/lib/model.js b/lib/model.js index 7d5f2c8..70f7d6e 100644 --- a/lib/model.js +++ b/lib/model.js @@ -125,8 +125,20 @@ function deepCopy (obj) { } +/** + * Set field to value in a model + * Create it if it doesn't exist + * @param {Object} obj The model to set a field for + * @param {Object} mod mod is of the form { k: v }. k is the field, v its value + * k can contain dots, in that case that means we will set a subfield + */ +modifierFunctions.$set = function (obj, mod) { + var field, value; + + +}; + modifierFunctions.$inc = function () {}; -modifierFunctions.$set = function () {}; /** @@ -154,7 +166,19 @@ function modify (obj, updateQuery) { // Apply modifiers modifiers = _.uniq(keys); modifiers.forEach(function (m) { + var keys; + if (!modifierFunctions[m]) { throw "Unknown modifier " + m; } + + try { + keys = Object.keys(updateQuery[m]); + } catch (e) { + throw "Modifier " + m + "'s argument must be an object"; + } + + keys.forEach(function (k) { + modifierFunctions[m](obj, k, updateQuery[m][k]); + }); }); } diff --git a/test/model.test.js b/test/model.test.js index 1984bec..e2fc1dd 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -240,6 +240,15 @@ describe('Model', function () { }).should.throw(); }); + it('Throw an error if a modifier is used with a non-object argument', function () { + var obj = { some: 'thing' } + , updateQuery = { $set: 'this exists' }; + + (function () { + model.modify(obj, updateQuery); + }).should.throw(); + }); + }); // ==== End of 'Modifying documents' ==== // });