-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
87 lines (72 loc) · 1.82 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
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
'use strict';
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
mongoose.Promise = global.Promise;
//MODEL FOR ACRONYMS
const acronymSchema = mongoose.Schema({
username: {type: String, required: true},
acronym: {type: String, required: true},
spellOut: {type: String, required: true},
definition: String,
categoryId: String
});
//creating response method to avoid _id vs id issues
acronymSchema.methods.apiResponse = function() {
return {
id: this.id,
username: this.username,
acronym: this.acronym,
spellOut: this.spellOut,
definition: this.definition,
categoryId: this.categoryId
}
}
const Acronym = mongoose.model('Acronym', acronymSchema);
//MODEL FOR CATEGORIES
const categorySchema = mongoose.Schema({
username: String,
title: String,
color: String
});
//creating response method to avoid _id vs id issues
categorySchema.methods.apiResponse = function() {
return {
id: this.id,
username: this.username,
title: this.title,
color: this.color
}
}
const Category = mongoose.model('Category', categorySchema);
//MODEL FOR COLORS
const colorSchema = mongoose.Schema({
username: String,
hexCode: String,
used: String
});
const Color = mongoose.model('Color', colorSchema);
//MODEL FOR USERS
const userSchema = mongoose.Schema({
username: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
}
});
userSchema.methods.apiResponse = function() {
return {
username: this.username
}
}
userSchema.methods.validatePassword = function(password) {
return bcrypt.compare(password, this.password);
}
userSchema.statics.hashPassword = function(password) {
return bcrypt.hash(password, 10);
}
const User = mongoose.model('User', userSchema);
module.exports = {Acronym, Category, Color, User};