(Model, opts)
| 8 | module.exports = ChainFind; |
| 9 | |
| 10 | function ChainFind(Model, opts) { |
| 11 | var promiseFunctionPostfix = Model.settings.get('promiseFunctionPostfix'); |
| 12 | |
| 13 | var prepareConditions = function () { |
| 14 | return Utilities.transformPropertyNames( |
| 15 | opts.conditions, opts.properties |
| 16 | ); |
| 17 | }; |
| 18 | |
| 19 | var prepareOrder = function () { |
| 20 | return Utilities.transformOrderPropertyNames( |
| 21 | opts.order, opts.properties |
| 22 | ); |
| 23 | }; |
| 24 | |
| 25 | var chainRun = function (done) { |
| 26 | var order, conditions; |
| 27 | |
| 28 | conditions = Utilities.transformPropertyNames( |
| 29 | opts.conditions, opts.properties |
| 30 | ); |
| 31 | order = Utilities.transformOrderPropertyNames( |
| 32 | opts.order, opts.properties |
| 33 | ); |
| 34 | |
| 35 | opts.driver.find(opts.only, opts.table, conditions, { |
| 36 | limit : opts.limit, |
| 37 | order : order, |
| 38 | merge : opts.merge, |
| 39 | offset : opts.offset, |
| 40 | exists : opts.exists |
| 41 | }, function (err, dataItems) { |
| 42 | if (err) { |
| 43 | return done(err); |
| 44 | } |
| 45 | if (dataItems.length === 0) { |
| 46 | return done(null, []); |
| 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 |
nothing calls this directly
no test coverage detected