-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquery.js
37 lines (32 loc) · 1.25 KB
/
query.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module.exports = function query(collection) {
'use strict';
return function query(kontx, next) {
var _ = require('lodash'),
db = require('../db'),
config = require('../config'),
options = kontx.args.options || {};
function getListPageSize(options){
return ( !_.isNull(options.limit) && !_.isUndefined(options.limit) && !_.isNaN(parseInt(options.limit, 10)) && _.isNumber(parseInt(options.limit, 10)) ) ?
parseInt(options.limit, 10) : config.db.defaultPageSize;
}
function getListSkipSize(options){
return ( !_.isNull(options.skip) && !_.isUndefined(options.skip) && !_.isNaN(parseInt(options.skip, 10)) && _.isNumber(parseInt(options.skip, 10)) ) ?
parseInt(options.skip) : 0;
}
if(!_.isUndefined(kontx.args)){
options.limit = getListPageSize(options);
options.skip = getListSkipSize(options);
}
else {
options.limit = config.db.defaultPageSize;
options.skip = 0;
}
db[collection].query(kontx.args.filters, options).then(function(value){
kontx.payload = value;
next();
},
function(err){
next(err);
}).done();
};
};