Merge pull request #826 from strongloop/updateAttributes-refactoring

Refactor `updateAttributes`
This commit is contained in:
Amir-61 2016-01-25 10:26:44 -05:00
commit 63482ef061
1 changed files with 126 additions and 102 deletions

View File

@ -52,6 +52,48 @@ function getIdValue(m, data) {
return data && data[idName(m)]; return data && data[idName(m)];
} }
function copyData(from, to) {
for (var key in from) {
to[key] = from[key];
}
}
function convertDataPropertiesByType(data) {
var typedData = {};
for (var key in data) {
// Convert the properties by type
typedData[key] = data[key];
if (typeof typedData[key] === 'object'
&& typedData[key] !== null
&& typeof typedData[key].toObject === 'function') {
typedData[key] = typedData[key].toObject();
}
}
return typedData;
}
/**
* Apply strict check for model's data.
* Notice: Please note this method modifies `inst` when `strict` is `validate`.
*/
function applyStrictCheck(model, strict, data, inst, cb) {
var props = model.definition.properties;
var keys = Object.keys(data);
var result = {}, key;
for (var i = 0; i < keys.length; i++) {
key = keys[i];
if (props[key]) {
result[key] = data[key];
} else if (strict === 'throw') {
cb(new Error('Unknown property: ' + key));
return;
} else if (strict === 'validate') {
inst.__unknownProperties.push(key);
}
}
cb(null, result);
}
function setIdValue(m, data, value) { function setIdValue(m, data, value) {
if (data) { if (data) {
data[idName(m)] = value; data[idName(m)] = value;
@ -2374,6 +2416,7 @@ DataAccessObject.prototype.unsetAttribute = function unsetAttribute(name, nullif
* @param {Function} cb Callback function called with (err, instance) * @param {Function} cb Callback function called with (err, instance)
*/ */
DataAccessObject.prototype.updateAttributes = function updateAttributes(data, options, cb) { DataAccessObject.prototype.updateAttributes = function updateAttributes(data, options, cb) {
var self = this;
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments); var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) { if (connectionPromise) {
return connectionPromise; return connectionPromise;
@ -2452,23 +2495,14 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
data = ctx.data; data = ctx.data;
if (strict && !allowExtendedOperators) { if (strict && !allowExtendedOperators) {
var props = Model.definition.properties; applyStrictCheck(self.constructor, strict, data, inst, validateAndSave);
var keys = Object.keys(data); } else {
var result = {}, key; validateAndSave(null, data);
for (var i = 0; i < keys.length; i++) {
key = keys[i];
if (props[key]) {
result[key] = data[key];
} else if (strict === 'throw') {
cb(new Error('Unknown property: ' + key));
return;
} else if (strict === 'validate') {
inst.__unknownProperties.push(key);
}
}
data = removeUndefined(result);
} }
function validateAndSave(err, data) {
if (err) return cb(err);
data = removeUndefined(data);
var doValidate = true; var doValidate = true;
if (options.validate === undefined) { if (options.validate === undefined) {
if (Model.settings.automaticValidation !== undefined) { if (Model.settings.automaticValidation !== undefined) {
@ -2497,19 +2531,8 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
function triggerSave(){ function triggerSave(){
inst.trigger('save', function (saveDone) { inst.trigger('save', function (saveDone) {
inst.trigger('update', function (done) { inst.trigger('update', function (done) {
var typedData = {}; copyData(data, inst);
var typedData = convertDataPropertiesByType(data);
for (var key in data) {
// Convert the properties by type
inst[key] = data[key];
typedData[key] = inst[key];
if (typeof typedData[key] === 'object'
&& typedData[key] !== null
&& typeof typedData[key].toObject === 'function') {
typedData[key] = typedData[key].toObject();
}
}
context.data = typedData; context.data = typedData;
function updateAttributesCallback(err) { function updateAttributesCallback(err) {
@ -2573,6 +2596,7 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
}, data, cb); }, data, cb);
}, data, cb); }, data, cb);
} }
}
}); });
return cb.promise; return cb.promise;
}; };