-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.js
135 lines (113 loc) · 2.98 KB
/
database.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
128
129
130
131
132
133
134
135
const { Sequelize } = require("sequelize");
require("./constants");
const sequelize = new Sequelize({
dialect: 'mysql',
host: process.env.HOST,
username: process.env.LOGIN,
password: process.env.PASSWORD,
database: process.env.DATABASE,
logging: false
});
// Set session time zone
sequelize.query("SET time_zone = 'America/New_York'").then(() => {
console.log("Session time zone set successfully");
}).catch(error => {
console.error("Error setting session time zone:", error);
});
console.log("Initializing Models...");
const initPatient = require('./models/Patient');
const initCaretaker = require('./models/Caretaker');
const initCabinet = require('./models/Cabinet');
const initCabinetBox = require('./models/CabinetBox');
const initMedication = require('./models/Medication');
const initNDC = require('./models/NDC');
const initSession = require("./models/Session");
const initSessionIntake = require("./models/SessionIntake");
const initSchedule = require("./models/Schedule");
const Caretaker = initCaretaker(sequelize);
const Patient = initPatient(sequelize);
const Cabinet = initCabinet(sequelize);
const CabinetBox = initCabinetBox(sequelize);
const Medication = initMedication(sequelize);
const NDC = initNDC(sequelize);
const Session = initSession(sequelize);
const SessionIntake = initSessionIntake(sequelize);
const Schedule = initSchedule(sequelize);
Caretaker.hasMany(Patient, {
foreignKey: "caretaker_id"
});
Patient.belongsTo(Caretaker, {
foreignKey: "caretaker_id"
})
Cabinet.hasMany(Patient, {
foreignKey: "cabinet_id"
});
Patient.belongsTo(Cabinet, {
foreignKey: "cabinet_id"
});
Medication.hasMany(NDC, {
foreignKey: "medication_id"
});
Medication.hasMany(Schedule, {
foreignKey: 'medication_id',
});
Schedule.belongsTo(Medication, {
foreignKey: 'medication_id',
});
Schedule.hasMany(SessionIntake, {
foreignKey: 'medication_id',
});
SessionIntake.belongsTo(Schedule, {
foreignKey: 'medication_id',
});
NDC.belongsTo(Medication, {
foreignKey: "medication_id"
});
Patient.hasMany(Session, {
foreignKey: "patient_id"
});
Session.belongsTo(Patient, {
foreignKey: "patient_id"
});
Session.hasMany(SessionIntake, {
foreignKey: "session_id"
});
SessionIntake.belongsTo(Session, {
foreignKey: "session_id"
});
Medication.hasMany(SessionIntake, {
foreignKey: "medication_id"
});
Patient.hasMany(Schedule, {
foreignKey: "patient_id"
});
Schedule.belongsTo(Patient, {
foreignKey: "patient_id"
});
Medication.hasMany(Schedule, {
foreignKey: "medication_id"
});
Medication.hasMany(CabinetBox, {
foreignKey: "medication_id"
})
CabinetBox.belongsTo(Cabinet, {
foreignKey: "cabinet_id"
})
Cabinet.hasMany(CabinetBox, {
foreignKey: "cabinet_id",
})
sequelize.sync({ force: false }).then(() => {
console.log("Models initialized successfully")
});
module.exports = {
sequelize,
Patient,
Caretaker,
Cabinet,
CabinetBox,
Medication,
NDC,
Session,
SessionIntake,
Schedule
};