-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
44 lines (38 loc) · 1.17 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
// Import statements.
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const movieSchema = mongoose.Schema({
Title: { type: String, required: true },
Description: { type: String, required: true },
Genre: {
Name: String,
Description: String,
},
Director: {
Name: String,
Bio: String,
Birth: String,
Death: String,
},
ImagePath: String,
Featured: Boolean,
});
const userSchema = mongoose.Schema({
userName: { type: String, required: true },
password: { type: String, required: true },
email: { type: String, required: true },
birthday: Date,
favouriteMovies: [{ type: mongoose.Schema.Types.ObjectId, ref: "movies" }],
});
// Hashing of submitted passwords using bcrypt
userSchema.statics.hashPassword = (password) => {
return bcrypt.hashSync(password, 10);
};
// Validating submitted passwords
userSchema.methods.validatePassword = function (password) {
return bcrypt.compareSync(password, this.password);
};
const moviesCollection = mongoose.model("movies", movieSchema);
const usersCollection = mongoose.model("users", userSchema);
module.exports.Movies = moviesCollection;
module.exports.Users = usersCollection;