From 4db30401947f12e1e30ed2117e42641de2ddb8ed Mon Sep 17 00:00:00 2001 From: Loukan ElKadi Date: Fri, 7 Dec 2018 11:10:41 +0200 Subject: [PATCH 1/2] PROPOSAL: Update transformer-builder In the unserialize(), only transform pRecord's columns that match model's attributes. Or in other words, skip any fields available in the pRecord that don't match any attribute of the Model. --- .../utils/system/transformer-builder.js | 29 +++++-------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/lib/waterline/utils/system/transformer-builder.js b/lib/waterline/utils/system/transformer-builder.js index fae7d3e7e..c1e46c8c8 100644 --- a/lib/waterline/utils/system/transformer-builder.js +++ b/lib/waterline/utils/system/transformer-builder.js @@ -194,37 +194,22 @@ Transformation.prototype.serializeValues = function(values) { Transformation.prototype.unserialize = function(pRecord) { - // Get the database columns that we'll be transforming into attribute names. - var colsToTransform = _.values(this._transformations); - - // Shallow clone the physical record, so that we don't lose any values in cases - // where one attribute's name conflicts with another attribute's `columnName`. - // (see https://github.com/balderdashy/sails/issues/4079) - var copyOfPhysicalRecord = _.clone(pRecord); - - // Remove the values from the pRecord that are set for the columns we're - // going to transform. This ensures that the `columnName` and the - // attribute name don't both appear as properties in the final record - // (unless there's a conflict as described above). - _.each(_.keys(pRecord), function(key) { - if (_.contains(colsToTransform, key)) { - delete pRecord[key]; - } - }); + // Only unserialize fields of the pRecord that are in the _transformations + let unserializedRecord = {}; // Loop through the keys to transform of this record and reattach them. _.each(this._transformations, function(columnName, attrName) { // If there's no value set for this column name, continue. - if (!_.has(copyOfPhysicalRecord, columnName)) { + if (!_.has(pRecord, columnName)) { return; } - // Otherwise get the value from the cloned record. - pRecord[attrName] = copyOfPhysicalRecord[columnName]; + // Otherwise get the value from the pRecord. + unserializedRecord[attrName] = pRecord[columnName]; }); - // Return the original, mutated record. - return pRecord; + // Return the new unserialized record. + return unserializedRecord; }; From f44772f7d920baa97364c0476488bffcd8d698d4 Mon Sep 17 00:00:00 2001 From: Loukan ElKadi Date: Fri, 7 Dec 2018 11:42:06 +0200 Subject: [PATCH 2/2] change-let-to-var Changing let keyword to var, in transformer's unserialize(), for backward compatibility with old JS versions. --- lib/waterline/utils/system/transformer-builder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/waterline/utils/system/transformer-builder.js b/lib/waterline/utils/system/transformer-builder.js index c1e46c8c8..6f7247a57 100644 --- a/lib/waterline/utils/system/transformer-builder.js +++ b/lib/waterline/utils/system/transformer-builder.js @@ -195,7 +195,7 @@ Transformation.prototype.serializeValues = function(values) { Transformation.prototype.unserialize = function(pRecord) { // Only unserialize fields of the pRecord that are in the _transformations - let unserializedRecord = {}; + var unserializedRecord = {}; // Loop through the keys to transform of this record and reattach them. _.each(this._transformations, function(columnName, attrName) {