src/index.js
function injectResultInObject(result, mappedObject, maps, mapId, columnPrefix = '') {

    // Get the resultMap for this object
    let resultMap = maps.find(map => map.mapId === mapId);

    // Copy id property
    let idProperty = getIdProperty(resultMap);

    idProperty.forEach(field => {
        if (!mappedObject[field.name]) {
            mappedObject[field.name] = result[columnPrefix + field.column];
        }
    });

    const {properties, associations, collections} = resultMap;

    // Copy other properties
    properties && properties.forEach(property => {
        // If property is a string, convert it to an object
        if (typeof property === 'string') {
            // eslint-disable-next-line
            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;

            mappedObject[property.name] = result[columnPrefix + column];
        }
    });

    // Copy associations
    associations && associations.forEach(association => {

        let associatedObject = mappedObject[association.name];

        if (!associatedObject) {
            let associatedResultMap = maps.find(map => map.mapId === association.mapId);
            let associatedObjectIdProperty = getIdProperty(associatedResultMap);

            mappedObject[association.name] = null;

            // Don't create associated object if it's key value is null
            let isAssociatedObjectIdPropertyNotNull = associatedObjectIdProperty.every(field =>
                result[association.columnPrefix + field.column] !== null
            );

            if (isAssociatedObjectIdPropertyNotNull) {
                associatedObject = createMappedObject(associatedResultMap);
                mappedObject[association.name] = associatedObject;
            }
        }

        if (associatedObject) {
            injectResultInObject(result, associatedObject, maps, association.mapId, association.columnPrefix);
        }
    });

    // Copy collections
    collections && collections.forEach(collection => {

        let mappedCollection = mappedObject[collection.name];

        if (!mappedCollection) {
            mappedCollection = [];
            mappedObject[collection.name] = mappedCollection;
        }

        injectResultInCollection(result, mappedCollection, maps, collection.mapId, collection.columnPrefix);
    });
}
function injectResultInObject(result, mappedObject, maps, mapId, columnPrefix = '') {

    // Get the resultMap for this object
    let resultMap = maps.find(map => map.mapId === mapId);

    // Copy id property
    let idProperty = getIdProperty(resultMap);

    idProperty.forEach(field => {
        if (!mappedObject[field.name]) {
            mappedObject[field.name] = result[columnPrefix + field.column];
        }
    });

    const {properties, associations, collections} = resultMap;

    // Copy other properties
    properties && properties.forEach(property => {
        // If property is a string, convert it to an object
        if (typeof property === 'string') {
            // eslint-disable-next-line
            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;

            mappedObject[property.name] = result[columnPrefix + column];
        }
    });

    // Copy associations
    associations && associations.forEach(association => {

        let associatedObject = mappedObject[association.name];

        if (!associatedObject) {
            let associatedResultMap = maps.find(map => map.mapId === association.mapId);
            let associatedObjectIdProperty = getIdProperty(associatedResultMap);

            mappedObject[association.name] = null;

            // Don't create associated object if it's key value is null
            let isAssociatedObjectIdPropertyNotNull = associatedObjectIdProperty.every(field =>
                result[association.columnPrefix + field.column] !== null
            );

            if (isAssociatedObjectIdPropertyNotNull) {
                associatedObject = createMappedObject(associatedResultMap);
                mappedObject[association.name] = associatedObject;
            }
        }

        if (associatedObject) {
            injectResultInObject(result, associatedObject, maps, association.mapId, association.columnPrefix);
        }
    });

    // Copy collections
    collections && collections.forEach(collection => {

        let mappedCollection = mappedObject[collection.name];

        if (!mappedCollection) {
            mappedCollection = [];
            mappedObject[collection.name] = mappedCollection;
        }

        injectResultInCollection(result, mappedCollection, maps, collection.mapId, collection.columnPrefix);
    });
}
function injectResultInObject ( result , mappedObject , maps , mapId , columnPrefix = '' ) { // Get the resultMap for this object let resultMap = maps . find ( map => map . mapId === mapId ) ; // Copy id property let idProperty = getIdProperty ( resultMap ) ; idProperty . forEach ( field => { if ( ! mappedObject [ field . name ] ) { mappedObject [ field . name ] = result [ columnPrefix + field . column ] ; } } ) ; const { properties , associations , collections } = resultMap ; // Copy other properties properties && properties . forEach ( property => { // If property is a string, convert it to an object if ( typeof property === 'string' ) { // eslint-disable-next-line 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 ; mappedObject [ property . name ] = result [ columnPrefix + column ] ; } } ) ; // Copy associations associations && associations . forEach ( association => { let associatedObject = mappedObject [ association . name ] ; if ( ! associatedObject ) { let associatedResultMap = maps . find ( map => map . mapId === association . mapId ) ; let associatedObjectIdProperty = getIdProperty ( associatedResultMap ) ; mappedObject [ association . name ] = null ; // Don't create associated object if it's key value is null let isAssociatedObjectIdPropertyNotNull = associatedObjectIdProperty . every ( field => result [ association . columnPrefix + field . column ] !== null ) ; if ( isAssociatedObjectIdPropertyNotNull ) { associatedObject = createMappedObject ( associatedResultMap ) ; mappedObject [ association . name ] = associatedObject ; } } if ( associatedObject ) { injectResultInObject ( result , associatedObject , maps , association . mapId , association . columnPrefix ) ; } } ) ; // Copy collections collections && collections . forEach ( collection => { let mappedCollection = mappedObject [ collection . name ] ; if ( ! mappedCollection ) { mappedCollection = [ ] ; mappedObject [ collection . name ] = mappedCollection ; } injectResultInCollection ( result , mappedCollection , maps , collection . mapId , collection . columnPrefix ) ; } ) ; }
Injects id, properties, associations and collections to the supplied mapped object.

@param {Object} result - a single database result (one row)
@param {Object} mappedObject - the object in which result needs to be injected
@param {Array} maps - an array of result maps
@param {String} mapId - mapId of the top-level objects in the resultSet
@param {String} [columnPrefix] - prefix that should be applied to the column names of the top-level objects
Injects id properties associations and collections to the supplied mapped object .