-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
34 lines (29 loc) · 927 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
const { Sequelize } = require('sequelize')
const { DB } = require('./config')
if (!DB) return
function connect(dbName) {
if (dbName == 'mysql' || dbName == 'mssql') {
return connectWithParams(DB.host, DB.username, DB.password, DB.database, DB._type)
} else if (dbName === 'postgres') {
return connectWithString(DB.connectionString, DB._type)
}
}
function connectWithParams(host, username, password, database, dialect) {
return new Sequelize(database, username, password, {
host: host,
dialect: dialect
})
}
function connectWithString(connectionString, dialect) {
return new Sequelize(connectionString, { dialect: dialect })
}
const sequelize = connect(DB._type)
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.')
})
.catch((error) => {
console.error('Unable to connect to the database: ', error)
})
module.exports = sequelize