Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[proposal] Update transformer-builder #1587

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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;
};