forked from Dieter1978/Full_Stack_App_T3A2-B
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
91 lines (73 loc) · 2.31 KB
/
db.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
88
89
90
91
import mongoose from 'mongoose'
import dotenv from 'dotenv'
dotenv.config()
async function dbClose() {
await mongoose.connection.close()
console.log('dbClosed')
}
mongoose.connect(process.env.ATLAS_DB_URL)
.then(m => console.log(m.connection.readyState === 1 ? 'Mongoose connected!' : 'Mongoose did not connect'))
.catch(err => console.log(err))
// USER SCHEMA
const userSchema = new mongoose.Schema({
name : {type : String, required : [true,'Please add name']},
email : {
type: String,
required: [true, 'Please add an email'],
unique: true,
match: [
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
'Please add a valid email'
]
},
role: {
type: String,
enum: ['user', 'admin'],
default: 'user'
},
password: {
type: String,
required: [true, 'Please add a password'],
minlength: 6,
select: false
},
student : {type: mongoose.ObjectId, ref: 'student'},
})
const UserModel = mongoose.model('User', userSchema)
// CLASS SCHEMA
const classSchema = new mongoose.Schema({
name : {type : String, required: [true,'Please add name']},
year: {type: mongoose.ObjectId, ref: 'Year'}
})
classSchema.index({name: 1, year: 1}, {unique: true})
const ClassModel = mongoose.model('Class', classSchema)
// YEAR SCHEMA
const yearSchema = new mongoose.Schema({
name : {type : String, required: [true,'Please add name'], unique:true}
})
const YearModel = mongoose.model('Year', yearSchema)
// STUDENT SCHEMA
const studentSchema = new mongoose.Schema({
firstName : {type: String, required:true},
lastName: {type: String, required:true},
// year: {type: mongoose.ObjectId, ref: 'Year'},
class: {type: mongoose.ObjectId, ref: 'Class'},
email : {
type: String,
required: [true, 'Please add an email'],
unique: true,
match: [
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
'Please add a valid email'
]
},
photo : String,
contactDetails: String,
questionOne : String,
questionTwo : String,
questionThree : String,
questionFour : String,
quote : String
})
const StudentModel = mongoose.model('Student', studentSchema)
export { StudentModel, UserModel, YearModel, ClassModel, dbClose }