1 | var async= require('async'),
|
2 | _= require('underscore'),
|
3 | colors= require('colors');
|
4 |
|
5 | module.exports= function (dyn)
|
6 | {
|
7 | var finder= {};
|
8 |
|
9 | finder.canFind= function (query)
|
10 | {
|
11 | query.table.indexes.some(function (ind)
|
12 | {
|
13 | if (ind.usable(query))
|
14 | {
|
15 | query.index= ind;
|
16 | return true;
|
17 | }
|
18 | });
|
19 |
|
20 | return !!query.index;
|
21 | };
|
22 |
|
23 | finder.find= function (query)
|
24 | {
|
25 | if (query.opts.hints)
|
26 | console.log(('Query INDEX ('+query.index.name+')').yellow);
|
27 |
|
28 | var p= dyn.promise(['results','count','end'],null,'consumed'),
|
29 | cursor= query.index.find(query);
|
30 |
|
31 | if (query.count)
|
32 | cursor.chain(p);
|
33 | else
|
34 | cursor.results(function (items)
|
35 | {
|
36 | items.forEach(function (item, idx) { items[idx]= { _ref: item }; });
|
37 | p.trigger.results(items);
|
38 | })
|
39 | .error(p.trigger.error)
|
40 | .consumed(p.trigger.consumed)
|
41 | .end(p.trigger.end);
|
42 |
|
43 | return p;
|
44 |
|
45 | };
|
46 |
|
47 | return finder;
|
48 | };
|