forked from mrvautin/expressCart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginate.js
118 lines (107 loc) · 2.95 KB
/
paginate.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
const {
getConfig
} = require('./config');
/**
* @param {boolean} frontend // whether or not this is an front or admin call
* @param {req} req // express `req` object
* @param {integer} page // The page number
* @param {string} collection // The collection to search
* @param {object} query // The mongo query
* @param {object} sort // The mongo sort
*/
const paginateData = (frontend, req, page, collection, query, sort) => {
const db = req.app.db;
const config = getConfig();
let numberItems = 10;
if(frontend){
numberItems = config.productsPerPage ? config.productsPerPage : 6;
}
let skip = 0;
if(page > 1){
skip = (page - 1) * numberItems;
}
if(!query){
query = {};
}
if(!sort){
sort = {};
}
// Run our queries
return Promise.all([
db[collection].find(query).skip(skip).limit(parseInt(numberItems)).sort(sort).toArray(),
db[collection].countDocuments(query)
])
.then((result) => {
const returnData = { data: result[0], totalItems: result[1] };
return returnData;
})
.catch((err) => {
throw new Error('Error retrieving paginated data');
});
};
/**
* @param {boolean} frontend // whether or not this is an front or admin call
* @param {req} req // express `req` object
* @param {integer} page // The page number
* @param {string} collection // The collection to search
* @param {object} query // The mongo query
* @param {object} sort // The mongo sort
*/
const paginateProducts = (frontend, db, page, query, sort) => {
const config = getConfig();
let numberItems = 10;
if(frontend){
numberItems = config.productsPerPage ? config.productsPerPage : 6;
}
let skip = 0;
if(page > 1){
skip = (page - 1) * numberItems;
}
if(!query){
query = {};
}
if(!sort){
sort = {};
}
// Run our queries
return Promise.all([
db.products.aggregate([
{ $match: query },
{
$lookup: {
from: 'variants',
localField: '_id',
foreignField: 'product',
as: 'variants'
}
}
]).sort(sort).skip(skip).limit(parseInt(numberItems)).toArray(),
db.products.countDocuments(query)
])
.then((result) => {
const returnData = { data: result[0], totalItems: result[1] };
return returnData;
})
.catch((err) => {
throw new Error('Error retrieving paginated data');
});
};
const getSort = () => {
const config = getConfig();
let sortOrder = -1;
if(config.productOrder === 'ascending'){
sortOrder = 1;
}
let sortField = 'productAddedDate';
if(config.productOrderBy === 'title'){
sortField = 'productTitle';
}
return {
[sortField]: sortOrder
};
};
module.exports = {
paginateData,
paginateProducts,
getSort
};