lib/dataset/query.js
function(includeDatasets, fromModel) {
            var ds = this.mergeOptions({}),
                originalRowCb = ds.rowCb;

            if(!ds.__opts._eagerAssoc) {
                ds.__opts._eagerAssoc = includeDatasets;
                ds.rowCb = function (topLevelResults) {

                    function toObject(thing) {
                        if (!thing) {
                            return comb.when(thing);
                        }
                        if (Array.isArray(thing)) {
                            return comb.when(thing.map(function(item) {
                                return toObject(item);
                            }));
                        }
                        if ('toObject' in thing) {
                            return comb.when(thing.toObject());
                        }
                        return comb.when(thing);
                    }

                    var eagerResults = {},
                        whens = [];

                        if (!originalRowCb) {
                            // pass through for when topLevelResults is already resolved
                            originalRowCb = function(r){return r;};
                        }

                        return comb.when(originalRowCb(topLevelResults)).chain(function(maybeModel) {
                            whens = Object.keys(ds.__opts._eagerAssoc).map(function(key) {
                                return ds.__opts._eagerAssoc[key](maybeModel).chain(function(result) {
                                    return toObject(result).chain(function(res) {
                                        eagerResults[key] = res;
                                        return result;
                                    });
                                });
                            });

                            return comb.when(whens).chain(function () {
                                return toObject(maybeModel).chain(function(json) {
                                    // merge associations on to main data
                                    return Object.assign(json, eagerResults);
                                });
                            });

                        });
                };
            }

            return ds.mergeOptions({
                _eagerAssoc: Object.assign(ds.__opts._eagerAssoc, includeDatasets)
            });
        }
function(includeDatasets, fromModel) {
            var ds = this.mergeOptions({}),
                originalRowCb = ds.rowCb;

            if(!ds.__opts._eagerAssoc) {
                ds.__opts._eagerAssoc = includeDatasets;
                ds.rowCb = function (topLevelResults) {

                    function toObject(thing) {
                        if (!thing) {
                            return comb.when(thing);
                        }
                        if (Array.isArray(thing)) {
                            return comb.when(thing.map(function(item) {
                                return toObject(item);
                            }));
                        }
                        if ('toObject' in thing) {
                            return comb.when(thing.toObject());
                        }
                        return comb.when(thing);
                    }

                    var eagerResults = {},
                        whens = [];

                        if (!originalRowCb) {
                            // pass through for when topLevelResults is already resolved
                            originalRowCb = function(r){return r;};
                        }

                        return comb.when(originalRowCb(topLevelResults)).chain(function(maybeModel) {
                            whens = Object.keys(ds.__opts._eagerAssoc).map(function(key) {
                                return ds.__opts._eagerAssoc[key](maybeModel).chain(function(result) {
                                    return toObject(result).chain(function(res) {
                                        eagerResults[key] = res;
                                        return result;
                                    });
                                });
                            });

                            return comb.when(whens).chain(function () {
                                return toObject(maybeModel).chain(function(json) {
                                    // merge associations on to main data
                                    return Object.assign(json, eagerResults);
                                });
                            });

                        });
                };
            }

            return ds.mergeOptions({
                _eagerAssoc: Object.assign(ds.__opts._eagerAssoc, includeDatasets)
            });
        }
function ( includeDatasets , fromModel ) { var ds = this . mergeOptions ( { } ) , originalRowCb = ds . rowCb ; if ( ! ds . __opts . _eagerAssoc ) { ds . __opts . _eagerAssoc = includeDatasets ; ds . rowCb = function ( topLevelResults ) { function toObject ( thing ) { if ( ! thing ) { return comb . when ( thing ) ; } if ( Array . isArray ( thing ) ) { return comb . when ( thing . map ( function ( item ) { return toObject ( item ) ; } ) ) ; } if ( 'toObject' in thing ) { return comb . when ( thing . toObject ( ) ) ; } return comb . when ( thing ) ; } var eagerResults = { } , whens = [ ] ; if ( ! originalRowCb ) { // pass through for when topLevelResults is already resolved originalRowCb = function ( r ) { return r ; } ; } return comb . when ( originalRowCb ( topLevelResults ) ) . chain ( function ( maybeModel ) { whens = Object . keys ( ds . __opts . _eagerAssoc ) . map ( function ( key ) { return ds . __opts . _eagerAssoc [ key ] ( maybeModel ) . chain ( function ( result ) { return toObject ( result ) . chain ( function ( res ) { eagerResults [ key ] = res ; return result ; } ) ; } ) ; } ) ; return comb . when ( whens ) . chain ( function ( ) { return toObject ( maybeModel ) . chain ( function ( json ) { // merge associations on to main data return Object . assign ( json , eagerResults ) ; } ) ; } ) ; } ) ; } ; } return ds . mergeOptions ( { _eagerAssoc : Object . assign ( ds . __opts . _eagerAssoc , includeDatasets ) } ) ; }
Allows the loading of another query and combining them in one patio command.
Queries can be related or completely unrelated.
All data comes back as JSON NOT Patio models.

@example

DB.from('company').filter({name: 'Amazon'})
.eager({
// company from parent query is passed in and usable in the eager query.
leader: (company) => DB.from('leader').filter({id: company.leaderId}).one()
})
// Load completely unrelated data.
.eager({org: () => DB.from('organization').one() })
.one()})

{ id: 1,
name: 'Amazon.com',
leader: {
id: 1,
name: 'Jeff'
},
org: {
id: 1,
name: 'Google Inc.'
}
}

Can do one to many loading for every item in the parent dataset.
Be careful doing this because it can lead to lots of extra queries.

DB.from('company').filter({state: 'IA'})
.eager({invoices: (company) => DB.from('invoices').filter({companyId: company.id}).one() })
.all()})

[
{ id: 1,
name: 'Principal',
invoices: [
{ id: 1, amount: 200},
{ id: 2, amount: 300},
]
},
{ id: 2,
name: 'John Deere',
invoices: [
{ id: 3, amount: 200},
{ id: 4, amount: 300},
]
}
]
Allows the loading of another query and combining them in one patio command . Queries can be related or completely unrelated . All data comes back as JSON NOT Patio models .