Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions lib/waterline/utils/system/transformer-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
var 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;
};