-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
68 lines (59 loc) · 1.83 KB
/
db.js
File metadata and controls
68 lines (59 loc) · 1.83 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
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const { mongoUrl } = require("./config.js");
const User = new mongoose.Schema({
// username, password
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
people: [{ type: mongoose.Schema.Types.ObjectId, ref: "Person" }],
timelines: [{ type: mongoose.Schema.Types.ObjectId, ref: "Timeline" }],
});
User.pre("save", async function (next) {
const user = this;
try {
const salt = await bcrypt.genSalt();
const hash = await bcrypt.hash(user.password, salt);
user.password = hash;
next();
} catch (e) {
console.log("Error occured");
next(e);
}
});
User.methods.isValidPassword = async function (password) {
const user = this;
const compare = await bcrypt.compare(password, user.password);
return compare;
};
const Block = new mongoose.Schema({
person: { type: mongoose.Schema.Types.ObjectId, ref: "Person" },
startTime: { type: Date, required: true },
endTime: { type: Date, required: true },
});
const Person = new mongoose.Schema({
user: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
name: { type: String, required: true },
maxTime: { type: Number },
availability: [Block],
});
const Timeline = new mongoose.Schema({
user: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
name: { type: String, reqired: true },
people: [{ type: mongoose.Schema.Types.ObjectId, ref: "Person" }],
maxTime: { type: Number },
blocks: [Block],
});
mongoose.model("User", User);
mongoose.model("Timeline", Timeline);
mongoose.model("Person", Person);
mongoose.connect(process.env.MONGODB_URI || mongoUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
});