forked from aravindnc/mongoose-paginate-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (118 loc) · 4.52 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var Promise = require('bluebird');
/**
* @param {Object} [query={}]
* @param {Object} [options={}]
* @param {Object|String} [options.select]
* @param {Object|String} [options.sort]
* @param {Object|String} [options.customLabels]
* @param {Object|} [options.collation]
* @param {Array|Object|String} [options.populate]
* @param {Boolean} [options.lean=false]
* @param {Boolean} [options.leanWithId=true]
* @param {Number} [options.offset=0] - Use offset or page to set skip position
* @param {Number} [options.page=1]
* @param {Number} [options.limit=10]
* @param {Function} [callback]
*
* @returns {Promise}
*/
function paginate(query, options, callback) {
query = query || {};
options = Object.assign({}, paginate.options, options);
options.customLabels = options.customLabels ? options.customLabels : {};
var select = options.select;
var sort = options.sort;
var collation = options.collation;
var populate = options.populate;
var lean = options.lean || false;
var leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true;
var limit = options.hasOwnProperty('limit') ? options.limit : 10;
var skip, offset, page;
// Custom Labels
var labelTotal = options.customLabels.totalDocs ? options.customLabels.totalDocs : 'totalDocs';
var labelLimit = options.customLabels.limit ? options.customLabels.limit : 'limit';
var labelPage = options.customLabels.page ? options.customLabels.page : 'page';
var labelTotalPages = options.customLabels.totalPages ? options.customLabels.totalPages : 'totalPages';
var labelDocs = options.customLabels.docs ? options.customLabels.docs : 'docs';
var labelNextPage = options.customLabels.nextPage ? options.customLabels.nextPage : 'nextPage';
var labelPrevPage = options.customLabels.prevPage ? options.customLabels.prevPage : 'prevPage';
if (options.hasOwnProperty('offset')) {
offset = options.offset;
skip = offset;
} else if (options.hasOwnProperty('page')) {
page = options.page;
skip = (page - 1) * limit;
} else {
offset = 0;
page = 1;
skip = offset;
}
var promises = {
docs: Promise.resolve([]),
count: this.countDocuments(query).exec()
};
if (limit) {
var query = this.find(query)
.select(select)
.sort(sort)
.collation(collation)
.skip(skip)
.limit(limit)
.lean(lean);
if (populate) {
[].concat(populate).forEach(function(item) {
query.populate(item);
});
}
promises.docs = query.exec();
if (lean && leanWithId) {
promises.docs = promises.docs.then(function(docs) {
docs.forEach(function(doc) {
doc.id = String(doc._id);
});
return docs;
});
}
}
return Promise.props(promises)
.then(function(data) {
var result = {
[labelDocs]: data.docs,
[labelTotal]: data.count,
limit: limit
};
if (offset !== undefined) {
result.offset = offset;
}
if (page !== undefined) {
const pages = Math.ceil(data.count / limit) || 1;
result.hasPrevPage = false;
result.hasNextPage = false;
result[labelPage] = page;
result[labelTotalPages] = pages;
// Set prev page
if(page > 1) {
result.hasPrevPage = true;
result[labelPrevPage] = (page - 1);
} else {
result[labelPrevPage] = null;
}
// Set next page
if(page < pages) {
result.hasNextPage = true;
result[labelNextPage] = (page + 1);
} else {
result[labelNextPage] = null;
}
}
return result;
})
.asCallback(callback);
}
/**
* @param {Schema} schema
*/
module.exports = function(schema) {
schema.statics.paginate = paginate;
};
module.exports.paginate = paginate;