Refactor `updateAttributes`

This commit is contained in:
Amir Jafarian 2016-01-20 18:24:05 -05:00 committed by Miroslav Bajtoš
parent d49dfa0293
commit 550cfa2a86
1 changed files with 126 additions and 102 deletions

View File

@ -52,6 +52,48 @@ function getIdValue(m, data) {
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) {
if (data) {
data[idName(m)] = value;
@ -2374,6 +2416,7 @@ DataAccessObject.prototype.unsetAttribute = function unsetAttribute(name, nullif
* @param {Function} cb Callback function called with (err, instance)
*/
DataAccessObject.prototype.updateAttributes = function updateAttributes(data, options, cb) {
var self = this;
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
if (connectionPromise) {
return connectionPromise;
@ -2452,23 +2495,14 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
data = ctx.data;
if (strict && !allowExtendedOperators) {
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);
}
}
data = removeUndefined(result);
applyStrictCheck(self.constructor, strict, data, inst, validateAndSave);
} else {
validateAndSave(null, data);
}
function validateAndSave(err, data) {
if (err) return cb(err);
data = removeUndefined(data);
var doValidate = true;
if (options.validate === undefined) {
if (Model.settings.automaticValidation !== undefined) {
@ -2497,19 +2531,8 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
function triggerSave(){
inst.trigger('save', function (saveDone) {
inst.trigger('update', function (done) {
var typedData = {};
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();
}
}
copyData(data, inst);
var typedData = convertDataPropertiesByType(data);
context.data = typedData;
function updateAttributesCallback(err) {
@ -2573,6 +2596,7 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
}, data, cb);
}, data, cb);
}
}
});
return cb.promise;
};