-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
127 lines (115 loc) · 3.42 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const mongoose = require("mongoose");
const { Schema } = mongoose;
const Staff = new Schema(
{
role: { type: Number },
email: { type: String, unique: true },
phone_number: { type: String, unique: true },
first_name: { type: String },
last_name: { type: String },
department: { type: Number },
position: { type: Number },
description: { type: String },
password: { type: String },
gender: { type: Number },
},
{ collection: "Staff" }
);
const Patient = new Schema(
{
email: { type: String, unique: true },
phone_number: { type: String, unique: true },
first_name: { type: String },
last_name: { type: String },
password: { type: String },
address: { type: String },
description: { type: String },
dob: { type: Date },
gender: { type: Number },
created_time: { type: Date, default: Date.now() },
updated_time: { type: Date, default: Date.now() },
code: { type: Number },
},
{ collection: "Patient" }
);
const Medication = new Schema(
{
name: { type: String },
brand: { type: String },
price: { type: Number },
description: { type: String },
quantity: { type: Date },
created_time: { type: Date, default: Date.now() },
updated_time: { type: Date, default: Date.now() },
created_by: { type: String },
updated_by: { type: String },
},
{ collection: "Medication" }
);
Medication.index({ name: 1, brand: 1 }, { unique: true });
const Room = new Schema(
{
type: { type: Number },
location: { type: Object, unique: true },
avaiable_slot: { type: Number },
history: { type: Object },
},
{ collection: "Room" }
);
const Image = new Schema(
{
obj_id: { type: String },
obj_type: { type: Number },
url: { type: String },
},
{ collection: "Image" }
);
Image.index({ obj_id: 1, obj_type: 1 }, { unique: true });
const Appointment = new Schema(
{
doctor: { type: String },
patient: { type: String },
start_time: { type: Date },
end_time: { type: Date },
created_time: { type: Date, default: Date.now() },
updated_time: { type: Date, default: Date.now() },
status: { type: Number },
illness_description: { type: String },
diagnose: { type: Object },
location: {type: Object},
created_by: { type: Object },
updated_by: { type: Object },
bill: { type: Object },
},
{ collection: "Appointment" }
);
//mongo model
mongoose.model("Staff", Staff);
mongoose.model("Patient", Patient);
mongoose.model("Medication", Medication);
mongoose.model("Room", Room);
mongoose.model("Image", Image);
mongoose.model("Appointment", Appointment);
//export database
module.exports.Staff = mongoose.model("Staff");
module.exports.Patient = mongoose.model("Patient");
module.exports.Medication = mongoose.model("Medication");
module.exports.Room = mongoose.model("Room");
module.exports.Image = mongoose.model("Image");
module.exports.Appointment = mongoose.model("Appointment");
mongoose.Promise = global.Promise;
const mongoUri = process.env.MONGO_URI || "";
mongoose
.connect(mongoUri, { useUnifiedTopology: true, useNewUrlParser: true })
.then((x) => {
console.log(
`Connected to Mongo! Database name: "${x.connections[0].name}"`
);
})
.catch((err) => {
console.error("Error connecting to mongo", err);
});
const connection = mongoose.connection;
connection.once("open", function () {
console.log("MongoDB database connection established successfully");
});