-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.js
40 lines (34 loc) · 857 Bytes
/
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
require('dotenv').config();
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASSWORD,
{
host: 'localhost',
dialect: 'mysql' /* 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,
}
);
(async () => {
try {
await sequelize.authenticate();
console.log('💾 Database connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
})();
// Create Models
const { TaskModel } = require('./models/Task');
const Task = TaskModel(sequelize);
const { UserModel } = require('./models/User');
const User = UserModel(sequelize);
if (process.env.MIGRATE_DB == 'TRUE') {
sequelize.sync().then(() => {
console.log(`All tables synced!`);
process.exit(0);
});
}
module.exports = {
Task,
User,
};