-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblog.model.js
More file actions
executable file
·48 lines (44 loc) · 1.28 KB
/
blog.model.js
File metadata and controls
executable file
·48 lines (44 loc) · 1.28 KB
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
const mongoose = require('mongoose');
// define the schema for our blog
const blogSchema = mongoose.Schema({
title: {
type: String,
required: true,
unique: true
},
content: {
type: String
}
}, {
timestamps: true
});
blogSchema.statics = {
search(params) {
//count documents https://stackoverflow.com/questions/43925662/mongoose-pagination-from-server-side
return this.count({})
.then((count) => {
if (count === 0) {
return Promise.reject({msg: 'No Document in Database..'});
}
return Promise.resolve(count);
})
.then((count) => {
//get paginated documents
return this.find().skip(params.pagination.start).limit(params.pagination.number).exec().then(function (docs) {
if (!docs) {
return Promise.reject({msg: 'No Document in Database..'});
} else {
const result = {
totalRecords: count,
numberOfPages: Math.ceil(count / params.pagination.number),
data: docs
};
return Promise.resolve(result);
}
});
})
.catch((err) => Promise.reject(err));
}
};
// create the model for badgeCategory and expose it to our app
module.exports = mongoose.model('Blog', blogSchema);