| 47 | } |
| 48 | |
| 49 | var eagerLoad = function (err, items) { |
| 50 | var idMap = {}; |
| 51 | |
| 52 | var keys = _.map(items, function (item, index) { |
| 53 | var key = item[opts.keys[0]]; |
| 54 | // Create the association arrays |
| 55 | for (var i = 0, association; association = opts.__eager[i]; i++) { |
| 56 | item[association.name] = []; |
| 57 | } |
| 58 | idMap[key] = index; |
| 59 | |
| 60 | return key; |
| 61 | }); |
| 62 | |
| 63 | async.eachSeries(opts.__eager, |
| 64 | function (association, cb) { |
| 65 | opts.driver.eagerQuery(association, opts, keys, function (err, instances) { |
| 66 | if (err) return cb(err) |
| 67 | |
| 68 | for (var i = 0, instance; instance = instances[i]; i++) { |
| 69 | // Perform a parent lookup with $p, and initialize it as an instance. |
| 70 | items[idMap[instance.$p]][association.name].push(association.model(instance)); |
| 71 | } |
| 72 | cb(); |
| 73 | }); |
| 74 | }, |
| 75 | function (err) { |
| 76 | if (err) done(err); |
| 77 | else done(null, items); |
| 78 | } |
| 79 | ); |
| 80 | }; |
| 81 | |
| 82 | async.map(dataItems, opts.newInstance, function (err, items) { |
| 83 | if (err) return done(err); |