UNPKG

892 BJavaScriptView Raw
1
2var debug = require('./_debug');
3// debug.enabled = true;
4
5function getExtractor( cb ){
6
7 // es callback
8 var extractor = function( error, resp ){
9
10 // Response logger
11 debug.resp( resp );
12
13 // Handle errors from the es client
14 if( error ) return cb( error );
15
16 // Handle errors returned in the body
17 if( 'object' == typeof resp && resp.hasOwnProperty('error') ) return cb( resp.error );
18
19 // Check the response is valid ang contains at least one records
20 else if( 'object' == typeof resp && resp.hasOwnProperty('_source') ){
21 var source = resp._source;
22 source._index = resp._index;
23 source._type = resp._type;
24 source._id = resp._id;
25 return cb( undefined, source );
26 }
27
28 // The query returned 0 results
29 else return cb();
30 return cb( 'an unexpected error occured' );
31 }
32
33 return extractor;
34}
35
36module.exports = getExtractor;
\No newline at end of file