-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
59 lines (48 loc) · 1.41 KB
/
models.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
'use strict';
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const userSchema = mongoose.Schema({
"username": {type: String, required: true},
"password": {type: String, required: true},
"firstName": {type: String, default: ''},
"lastName": {type: String, default: ''}
}
);
userSchema.methods.serialize=function(){
return {
username: this.username,
firstName: this.firstName,
lastName: this.lastName
};
};
userSchema.statics.hashPassword = function(password){
return bcrypt.hash(password, 10);
};
userSchema.methods.validatePassword = function(password){
return bcrypt.compare(password, this.password);
};
const blogPostSchema = mongoose.Schema({
author: {
firstName: String,
lastName: String
},
title: { type: String, required: true },
content: { type: String },
created: { type: Date, default: Date.now }
});
blogPostSchema.virtual('authorName').get(function () {
return `${this.author.firstName} ${this.author.lastName}`.trim();
});
blogPostSchema.methods.serialize = function () {
return {
id: this._id,
author: this.authorName,
content: this.content,
title: this.title,
created: this.created
};
};
// const BlogPost = mongoose.model('BlogPosts', blogPostSchema);
const BlogPost = mongoose.model('Posts', blogPostSchema);
const UserModel = mongoose.model('User', userSchema);
module.exports = { BlogPost, UserModel };