Skip to content
Browse files

validateAndUpdate now asks for the document first

  • Loading branch information...
1 parent c2365b0 commit d4c508e03234c9e6d046ce194924ad3a2748b42e @masylum committed Jul 13, 2011
Showing with 53 additions and 38 deletions.
  1. +22 −22 lib/model.js
  2. +1 −1 package.json
  3. +4 −3 test/integration/integration_test.js
  4. +26 −12 test/model_test.js
View
44 lib/model.js
@@ -182,20 +182,16 @@ module.exports = function (db, collection_name) {
/**
* Validates a mongo document and updates it
*
- * @param {Object} document
- * Document to uodate
- * @param {Object} update
- * Update object
- * @param {Object} options
- * Update options. Defaults to {upsert: true, multi: false}
- * @param {Function} callback
- * The callback returns a validator
+ * @param {Object} query - Document query
+ * @param {Object} update - Update object
+ * @param {Object} options - Update options. Defaults to {upsert: true, multi: false}
+ * @param {Function} callback - The callback returns a validator
*
* @returns itself
*/
- MODEL.validateAndUpdate = function (document, update, options, callback) {
+ MODEL.validateAndUpdate = function (query, update, options, callback) {
// options are optional
- if (typeof options === "function") {
+ if (typeof options === 'function') {
callback = options;
options = {};
} else {
@@ -207,14 +203,20 @@ module.exports = function (db, collection_name) {
options = _.defaults(options, {upsert: true, multi: false});
- MODEL.validate(document, update, function (error, validator) {
- if (validator.hasErrors()) {
- callback(error, validator);
- } else {
- MODEL.mongo('update' + namespace, {'_id': document._id}, update, options, function (error, doc) {
- callback(error, validator);
- });
- }
+ MODEL.mongo('findOne', query, function (error, document) {
+ if (error) return callback(error);
+
+ MODEL.validate(document, update, function (error, validator) {
+ if (error) return callback(error);
+
+ if (validator.hasErrors()) {
+ callback(null, validator);
+ } else {
+ MODEL.mongo('update' + namespace, {'_id': document._id}, update, options, function (error, doc) {
+ callback(null, validator);
+ });
+ }
+ });
});
return MODEL;
@@ -226,10 +228,8 @@ module.exports = function (db, collection_name) {
*
* @param {String} name
* @param {Object} document
- * @param {String} scope
- * Optional field to scope the resulting document
- * @param {Boolean} dot_notation
- * Return the nested object or using dot_notation ready for mongo
+ * @param {String} scope - Optional field to scope the resulting document
+ * @param {Boolean} dot_notation - Return the nested object or using dot_notation ready for mongo
*
* @returns document with applied skeleton
*/
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "mongolia",
"description": "Layer on top of the mongodb driver to implement data logic.",
- "version": "1.3.4",
+ "version": "1.3.5",
"author": "Pau Ramon <masylum@gmail.com>",
"contributors": [
{"name": "Pau Ramon", "email": "masylum@gmail.com"},
View
7 test/integration/integration_test.js
@@ -261,14 +261,15 @@ db.open(function (error) {
callback(null, validator);
};
- User.validateAndUpdate({name: 'John Smith'}, {name: 'foobar'}, done(function (error, validation) {
+ User.validateAndUpdate({name: 'John Smith'}, {'$set': {name: 'foobar'}}, function (error, validation) {
assert.deepEqual(validation.errors.name, ['We only love Zemba here']);
assert.deepEqual(validation.updated_document.name, 'John Smith');
- User.validateAndUpdate({name: 'John Smith'}, {name: 'zemba'}, function (error, validation) {
+ User.validateAndUpdate({name: 'John Smith'}, {'$set': {name: 'zemba'}}, function (error, validation) {
assert.deepEqual(validation.errors, {});
+ done();
});
- }));
+ });
})
.run();
View
38 test/model_test.js
@@ -184,11 +184,18 @@ testosterone
})
.add('`validateAndUpdate` when the model is invalid does not update it', function () {
- var document = {foo: 'bar'},
- update = {fleiba: 'zemba'},
- validator = _mock_validator(true),
- options = {},
- callback;
+ var query = {foo: 'bar'}
+ , document = {foo: 'bar', fleiba: 'foo'}
+ , update = {fleiba: 'zemba'}
+ , validator = _mock_validator(true)
+ , options = {}
+ , callback;
+
+ gently.expect(User, 'mongo', function (_method, _query, _callback) {
+ assert.equal(_method, 'findOne');
+ assert.deepEqual(_query, query);
+ _callback(null, document);
+ });
gently.expect(User, 'validate', function (_document, _data, _callback) {
_callback(null, validator);
@@ -199,15 +206,22 @@ testosterone
assert.deepEqual(_validator, validator);
});
- User.validateAndUpdate(document, update, options, callback);
+ User.validateAndUpdate(query, update, options, callback);
})
.add('`validateAndUpdate` when the model is valid updates it afterwards', function () {
- var document = {name: 'zemba', email: 'zemba@foobar.com'},
- update = {'$set': {name: 'John'}},
- validator = _mock_validator(false),
- options = {},
- callback;
+ var query = {foo: 'bar'}
+ , document = {foo: 'bar', fleiba: 'foo'}
+ , update = {'$set': {fleiba: 'John'}}
+ , validator = _mock_validator(false)
+ , options = {}
+ , callback;
+
+ gently.expect(User, 'mongo', function (_method, _query, _callback) {
+ assert.equal(_method, 'findOne');
+ assert.deepEqual(_query, query);
+ _callback(null, document);
+ });
User.beforeUpdate = function (_query, _update, _callback) {
_callback(null, document);
@@ -228,7 +242,7 @@ testosterone
assert.deepEqual(_validator, validator);
});
- User.validateAndUpdate(document, update, options, callback);
+ User.validateAndUpdate(query, update, options, callback);
})
.add('`getEmbeddedDocument` filters the document following the skeletons directive', function () {

0 comments on commit d4c508e

Please sign in to comment.
Something went wrong with that request. Please try again.