-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
56 lines (50 loc) · 1 KB
/
index.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
const PaginationService = {
build: build,
paginate: paginate,
}
function build(query) {
const params = {
skip: 0,
limit: 100,
sort: 'name ASC'
}
if (query) {
let newParams = {
skip: query.skip
? query.skip
: params.skip,
limit: query.limit
? query.limit
: params.limit,
sort: query.sort
? query.sort
: params.sort
}
if (query.where) {
newParams.where = query.where
}
return newParams
} else {
return params
}
}
function paginate(count, params, rows){
const currentPage = params.skip > 0
? (params.skip / params.limit) + 1
: 1
const totalPages = isNaN(Math.ceil(count/params.limit))
? 1
: Math.ceil(count/params.limit)
return {
count: count,
totalPages: totalPages,
currentPage: currentPage,
qs: {
skip: parseInt(params.skip),
limit: parseInt(params.limit),
sort: params.sort,
},
rows: rows,
}
}
module.exports = PaginationService