-
Notifications
You must be signed in to change notification settings - Fork 21
Open
Description
I needed a way to convert some properties, after fetching them from the DB. And since I wanted to prevent traversing the whole result set (before JoinJS) or object tree (after JoinJS) again, I thought it'd be great, if JoinJS could handle this in one go. So, I played around with something like this in injectResultInObject
:
// Copy id property
//[…]
let properties = {};
// Gather other properties
_.each(resultMap.properties, function(property) {
// If property is a string, convert it to an object
if (typeof property === 'string') {
property = {name: property, column: property};
}
// Copy only if property does not exist already
if (!mappedObject[property.name]) {
// The default for column name is property name
let column = (property.column) ? property.column : property.name;
properties[property.name] = result[columnPrefix + column];
//mappedObject[property.name] = result[columnPrefix + column];
}
});
// Check for conversion
if (resultMap.parse) {
properties = resultMap.parse(properties);
}
// Copy other properties
_.merge(mappedObject, properties);
// Copy associations
// […]
The reason I don't pass the whole mappedObject
to the parse
function is, that this way, the ID property and any other properties or functions the mappedObject
could have when createNew
is used are omitted and the chances of completely messing things up during parsing/converting are lower. ;)
What do you think? Would this be something you see within the scope of JoinJS?
Mikou
Metadata
Metadata
Assignees
Labels
No labels