Skip to content

Add possibility to parse/convert property values #8

@VividVisions

Description

@VividVisions

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions