-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
68 lines (56 loc) · 1.5 KB
/
model.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
'use strict';
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
mongoose.Promise = global.Promise;
const userSchema = mongoose.Schema({
username: {type: String, required: true},
email: {type: String, required: true},
password: {type: String, required: true}
});
const pastSearchSchema = mongoose.Schema({
user: { type: mongoose.Schema.Types.ObjectId, ref: 'Users' },
music_title: {type: String},
IMSLP_links: [String],
creation: {type: String}
});
pastSearchSchema.pre('find', function(next) {
this.populate('user');
next();
});
pastSearchSchema.pre('findOne', function(next) {
this.populate('user');
next();
});
pastSearchSchema.pre('findById', function(next) {
this.populate('user');
next();
});
pastSearchSchema.pre('findByIdAndUpdate', function(next) {
this.populate('user');
next();
});
pastSearchSchema.methods.serialize = function() {
return {
id: this._id,
username: this.user.username,
music_title: this.music_title,
IMSLP_links: this.IMSLP_links,
creation: this.creation
};
};
userSchema.methods.serialize = function() {
return {
id: this._id,
username: this.username,
email: this.email
};
};
userSchema.methods.validatePassword = function(password) {
return bcrypt.compare(password, this.password);
}
userSchema.statics.hashPassword = function(password) {
return bcrypt.hash(password, 10);
}
const Users = mongoose.model('Users',userSchema);
const PastSearches = mongoose.model('PastSearches', pastSearchSchema);
module.exports = { Users, PastSearches };