-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapiFeatures.js
48 lines (40 loc) · 1.51 KB
/
apiFeatures.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
class ApiFeatures {
constructor(query, queryStr) {
this.query = query;
this.queryStr = queryStr;
}
// Search Feature
search() {
const keyword = this.queryStr.keyword ? {
name: {
$regex: this.queryStr.keyword,
$options: "i", //Case insensitive M and m are same
}
} : {};
this.query = this.query.find({ ...keyword });
return this;
}
// filter feature
filter() {
// const queryCopy = this.queryStr
// this will take the exact reference , changing one will change the other too, so we will use spread operator
const queryCopy = { ...this.queryStr };
// Removing some fields for category
const removeFields = ["keyword", "page", "limit"];
removeFields.forEach(key => delete queryCopy[key]);
// Filter for price and rating
let queryStr = JSON.stringify(queryCopy);
queryStr = queryStr.replace(/\b(gt|gte|lt|lte)\b/g, key => `$${key}`);
this.query = this.query.find(JSON.parse(queryStr));
return this;
}
// categories can be case sensitive cauz user will not search for the category he/she will choose from the given ones
// Pagination
pagination(resultPerPage) {
const currentPage = Number(this.queryStr.page) || 1;
const skip = resultPerPage * (currentPage - 1);
this.query = this.query.limit(resultPerPage).skip(skip);
return this;
}
}
module.exports = ApiFeatures;