-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (68 loc) · 2.63 KB
/
index.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
const express = require('express');
const {Sequelize} = require('sequelize');
const contactRequestsRouter = require('./routes/contact_requests_route');
const usersRouter = require('./routes/users_route');
const authRouter = require('./routes/auth_route');
const reviewsRouter = require('./routes/reviews_route');
const tutoringClassRouter = require('./routes/tutoring_classes_route');
const enrolmentRouter = require('./routes/enrolment_router');
const bodyParser = require('body-parser');
const {initializeContactRequestsTable} = require('./database/contact_requests');
const {initializeUsersTable} = require("./database/users");
const {initializeReviewsTable} = require("./database/reviews");
const {sync} = require("./database/sync");
const {initializeTutoringClassTable} = require("./database/tutoring_class");
const {initializeEnrolmentTable} = require("./database/enrolment");
const DB_USER = 'root';
const DB_PASSWORD = 'root';
const sequelizeForDatabaseCreation = new Sequelize('', DB_USER, DB_PASSWORD, {
host: 'localhost',
dialect: 'mysql',
logging: false,
});
const databaseName = 'meditatii';
let sequelize;
(async () => {
try {
await sequelizeForDatabaseCreation
.query(`CREATE DATABASE IF NOT EXISTS ${databaseName};`);
await sequelizeForDatabaseCreation.authenticate();
await sequelizeForDatabaseCreation.close();
} catch (error) {
console.error('Unable to connect to the database: ', error);
}
sequelize = new Sequelize(databaseName, 'root', 'root', {
host: 'localhost',
dialect: 'mysql',
logging: false,
});
try {
await sequelize.authenticate();
console.log('Connection to the database has been established successfully!');
} catch (error) {
console.error('Unable to connect to the database: ', error);
}
try {
await initializeContactRequestsTable(sequelize);
await initializeUsersTable(sequelize);
await initializeReviewsTable(sequelize);
await initializeTutoringClassTable(sequelize);
await initializeEnrolmentTable(sequelize);
await sync();
} catch (error) {
console.error('Table initialization failed: ', error);
}
})();
const app = express();
const port = 8000;
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`)
});
app.use(bodyParser.json());
app.use('/contact-requests', contactRequestsRouter);
app.use('/users', usersRouter);
app.use('/auth', authRouter);
app.use('/reviews', reviewsRouter);
app.use('/tutoring-classes', tutoringClassRouter);
app.use('/tutoring-class', enrolmentRouter);
module.exports = {sequelize};