-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlist.js
80 lines (70 loc) · 2.42 KB
/
list.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module.exports = function list(collection) {
'use strict';
var _ = require('lodash'),
config = require('../config');
function getListPageSize(options){
return ( ( !_.isUndefined(options.limit) && !_.isNull(options.limit) ) && _.isNumber(options.limit) ) ?
parseInt(options.limit, 10) : config.db.defaultPageSize;
}
function getListSkipSize(options){
return ( ( !_.isUndefined(options.skip) && !_.isNull(options.skip) ) && _.isNumber(options.skip) ) ?
parseInt(options.skip) : 0;
}
function getSortOption(options) {
return ( ( !_.isUndefined(options.sort) && !_.isNull(options.sort) ) ) ? options.sort : "";
}
return function(kontx, next){
var async = require('async'),
db = require('../db')(),
args = kontx.args,
options = {
query: {}
};
if(!_.isArray(kontx.args)){
args = [kontx.args];
}
if(!_.isUndefined(args[0])){
options.limit = getListPageSize(args[0]);
options.skip = getListSkipSize(args[0]);
options.sort = getSortOption(args[0]);
}
else {
options.limit = config.db.defaultPageSize;
options.skip = 0;
options.sort = "";
}
async.parallel(
[
function(cb){
db[collection].list(options).then(function(value){
cb(null, value);
},
function(err){
cb(err);
}).done();
},
function(cb){
db[collection].describe(options).then(function(value){
cb(null, value);
},
function(err){
cb(err);
}).done();
}
],function(err, results){
if(err){
next(err);
}
else {
kontx.payload = {
total: _.isUndefined(results[1][0]) ? 0 : results[1][0].count,
limit: options.limit,
skip: options.skip,
results: results[0]
};
next();
}
}
);
};
};