-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.js
More file actions
77 lines (67 loc) · 2.07 KB
/
Copy pathmodels.js
File metadata and controls
77 lines (67 loc) · 2.07 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
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
// importing mongoose
const mongoose = require("mongoose");
// importing bcrypt Node.js'module
const bcrypt = require("bcrypt");
// defining schema for anime collection
let animeSchema = mongoose.Schema({
Title: { type: String, required: true },
Description: { type: String, required: true },
Genre: {
Name: [String],
Description: String,
},
MangaArtist: {
Name: String,
Bio: String,
Born: Date,
},
Image: String,
Featured: Boolean,
});
// Transform _id to id and remove _id
animeSchema.set("toJSON", {
virtuals: true,
versionKey: false,
transform: function (doc, ret) {
ret.id = ret._id.toString();
delete ret._id;
},
});
// defining schema for users collection
let userSchema = mongoose.Schema({
Username: { type: String, required: true },
Email: { type: String, required: true },
Password: { type: String, required: true },
Birthdate: Date,
FavoriteAnimes: [{ type: mongoose.Schema.Types.ObjectId, ref: "Anime" }],
});
// defined a function that hashes the password
userSchema.statics.hashPassword = (password) => {
return bcrypt.hashSync(password, 10);
};
// defined a function to compare the submitted hashed passwored with the hashed one stored in database
userSchema.methods.validatePassword = function (password) {
return bcrypt.compareSync(password, this.Password);
};
// Genre Schema
let genreSchema = mongoose.Schema({
Name: { type: String, required: true },
Description: { type: String, required: true },
});
// MangaArtist Schema
let mangaArtistsSchema = mongoose.Schema({
Name: { type: String, required: true },
Bio: { type: String, required: true },
Birth: { type: String, required: true },
Death: { type: String },
});
// creating models
let Anime = mongoose.model("Anime", animeSchema);
let User = mongoose.model("User", userSchema);
let Genre = mongoose.model("Genre", genreSchema);
let MangaArtists = mongoose.model("MangaArtists", mangaArtistsSchema);
// exporting the created models
module.exports.Anime = Anime;
module.exports.User = User;
module.exports.Genre = Genre;
module.exports.MangaArtists = MangaArtists;