UNPKG

3.64 kBJavaScriptView Raw
1'use strict';
2
3var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4
5function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7var api = require('./api');
8
9var _require = require('../utils'),
10 isEmpty = _require.isEmpty,
11 isNumber = _require.isNumber;
12
13var Search = function () {
14 function Search() {
15 _classCallCheck(this, Search);
16
17 this.query_hash = {
18 sort_by: [],
19 aggregate: [],
20 with_field: []
21 };
22 }
23
24 _createClass(Search, [{
25 key: 'expression',
26 value: function expression(value) {
27 this.query_hash.expression = value;
28 return this;
29 }
30 }, {
31 key: 'max_results',
32 value: function max_results(value) {
33 this.query_hash.max_results = value;
34 return this;
35 }
36 }, {
37 key: 'next_cursor',
38 value: function next_cursor(value) {
39 this.query_hash.next_cursor = value;
40 return this;
41 }
42 }, {
43 key: 'aggregate',
44 value: function aggregate(value) {
45 this.query_hash.aggregate.push(value);
46 return this;
47 }
48 }, {
49 key: 'with_field',
50 value: function with_field(value) {
51 this.query_hash.with_field.push(value);
52 return this;
53 }
54 }, {
55 key: 'sort_by',
56 value: function sort_by(field_name) {
57 var dir = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "desc";
58
59 var sort_bucket;
60 sort_bucket = {};
61 sort_bucket[field_name] = dir;
62 this.query_hash.sort_by.push(sort_bucket);
63 return this;
64 }
65 }, {
66 key: 'to_query',
67 value: function to_query() {
68 var _this = this;
69
70 Object.keys(this.query_hash).forEach(function (k) {
71 var v = _this.query_hash[k];
72 if (!isNumber(v) && isEmpty(v)) {
73 delete _this.query_hash[k];
74 }
75 });
76 return this.query_hash;
77 }
78 }, {
79 key: 'execute',
80 value: function execute(options, callback) {
81 if (callback === null) {
82 callback = options;
83 }
84 options = options || {};
85 return api.search(this.to_query(), options, callback);
86 }
87 }], [{
88 key: 'instance',
89 value: function instance() {
90 return new Search();
91 }
92 }, {
93 key: 'expression',
94 value: function expression(value) {
95 return this.instance().expression(value);
96 }
97 }, {
98 key: 'max_results',
99 value: function max_results(value) {
100 return this.instance().max_results(value);
101 }
102 }, {
103 key: 'next_cursor',
104 value: function next_cursor(value) {
105 return this.instance().next_cursor(value);
106 }
107 }, {
108 key: 'aggregate',
109 value: function aggregate(value) {
110 return this.instance().aggregate(value);
111 }
112 }, {
113 key: 'with_field',
114 value: function with_field(value) {
115 return this.instance().with_field(value);
116 }
117 }, {
118 key: 'sort_by',
119 value: function sort_by(field_name) {
120 var dir = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'asc';
121
122 return this.instance().sort_by(field_name, dir);
123 }
124 }]);
125
126 return Search;
127}();
128
129module.exports = Search;
\No newline at end of file