-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (43 loc) · 1.43 KB
/
index.js
File metadata and controls
58 lines (43 loc) · 1.43 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
const express = require('express');
const { sequelize } = require('./config/db');
const app = express();
const port = process.env.PORT || 3000;
const quizzRoute = require('./routes/quizz.route');
const Quiz = require('./models/quizz.model');
const QuizMl = require('./models/quizzMl.model');
app.use(express.json());
app.use('/api/quizz', quizzRoute);
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
sequelize.authenticate()
.then(async () => {
// 1. Sync physical tables first
// await sequelize.sync();
await Quiz.sync();
await QuizMl.sync();
// 2. Define the Raw SQL for the View
// This joins the main Quiz settings with the Multi-language (Ml) details
const createViewSql = `
CREATE OR REPLACE VIEW quizzes_view AS
SELECT
m.id AS id,
q.id AS quiz_id,
m.language_id,
m.name,
m.description,
q.image,
q.isFeatured,
q.isPopular
FROM quizzes q
JOIN quizzes_ml m ON q.id = m.quiz_id;
`;
await sequelize.query(createViewSql);
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});