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

    // Check if the object is already in mappedCollection
    let resultMap = maps.find(map => map.mapId === mapId);
    let idProperty = getIdProperty(resultMap);
    let predicate = idProperty.reduce((accumulator, field) => {
        accumulator[field.name] = result[columnPrefix + field.column];
        return accumulator;
    }, {});

    let mappedObject = mappedCollection.find(item => {
        for (let k in predicate) {
            if (item[k] !== predicate[k]) {
                return false;
            }
        }
        return true;
    });

    // Inject only if the value of idProperty is not null (ignore joins to null records)
    let isIdPropertyNotNull = idProperty.every(field => result[columnPrefix + field.column] !== null);

    if (isIdPropertyNotNull) {
        // Create mappedObject if it does not exist in mappedCollection
        if (!mappedObject) {
            mappedObject = createMappedObject(resultMap);
            mappedCollection.push(mappedObject);
        }

        // Inject result in object
        injectResultInObject(result, mappedObject, maps, mapId, columnPrefix);
    }
}
function injectResultInCollection(result, mappedCollection, maps, mapId, columnPrefix = '') {

    // Check if the object is already in mappedCollection
    let resultMap = maps.find(map => map.mapId === mapId);
    let idProperty = getIdProperty(resultMap);
    let predicate = idProperty.reduce((accumulator, field) => {
        accumulator[field.name] = result[columnPrefix + field.column];
        return accumulator;
    }, {});

    let mappedObject = mappedCollection.find(item => {
        for (let k in predicate) {
            if (item[k] !== predicate[k]) {
                return false;
            }
        }
        return true;
    });

    // Inject only if the value of idProperty is not null (ignore joins to null records)
    let isIdPropertyNotNull = idProperty.every(field => result[columnPrefix + field.column] !== null);

    if (isIdPropertyNotNull) {
        // Create mappedObject if it does not exist in mappedCollection
        if (!mappedObject) {
            mappedObject = createMappedObject(resultMap);
            mappedCollection.push(mappedObject);
        }

        // Inject result in object
        injectResultInObject(result, mappedObject, maps, mapId, columnPrefix);
    }
}
function injectResultInCollection ( result , mappedCollection , maps , mapId , columnPrefix = '' ) { // Check if the object is already in mappedCollection let resultMap = maps . find ( map => map . mapId === mapId ) ; let idProperty = getIdProperty ( resultMap ) ; let predicate = idProperty . reduce ( ( accumulator , field ) => { accumulator [ field . name ] = result [ columnPrefix + field . column ] ; return accumulator ; } , { } ) ; let mappedObject = mappedCollection . find ( item => { for ( let k in predicate ) { if ( item [ k ] !== predicate [ k ] ) { return false ; } } return true ; } ) ; // Inject only if the value of idProperty is not null (ignore joins to null records) let isIdPropertyNotNull = idProperty . every ( field => result [ columnPrefix + field . column ] !== null ) ; if ( isIdPropertyNotNull ) { // Create mappedObject if it does not exist in mappedCollection if ( ! mappedObject ) { mappedObject = createMappedObject ( resultMap ) ; mappedCollection . push ( mappedObject ) ; } // Inject result in object injectResultInObject ( result , mappedObject , maps , mapId , columnPrefix ) ; } }
Maps a single database result to a single object using mapId and injects it into mappedCollection.

@param {Object} result - a single database result (one row)
@param {Array} mappedCollection - the collection in which the mapped object should 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
Maps a single database result to a single object using mapId and injects it into mappedCollection .