dbconn.ts 981 Bytes
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
import mysql from 'mysql2'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
2

Rosanny Sihombing's avatar
Rosanny Sihombing committed
3
const env = process.env.NODE_ENV ?? 'development'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
4
const config = require('./config')[env]
Rosanny Sihombing's avatar
Rosanny Sihombing committed
5
6

// ==== USER ACOOUNT DB CONNECTION ====
Rosanny Sihombing's avatar
Rosanny Sihombing committed
7
const userConnection = mysql.createPool({
Rosanny Sihombing's avatar
Rosanny Sihombing committed
8
9
10
11
12
13
14
15
16
  host: config.database.host,
  user: config.database.user,
  password: config.database.password,
  port: config.database.port,
  database: config.database.dbUser,
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
})
Rosanny Sihombing's avatar
Rosanny Sihombing committed
17

Rosanny Sihombing's avatar
Rosanny Sihombing committed
18
userConnection.query('USE ' + config.database.dbUser)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
19
20

// ==== PROJECT DB CONNECTION ====
Rosanny Sihombing's avatar
Rosanny Sihombing committed
21
const projectConnection = mysql.createPool({
Rosanny Sihombing's avatar
Rosanny Sihombing committed
22
23
24
25
26
27
28
29
30
  host: config.database.host_project,
  user: config.database.user,
  password: config.database.password,
  port: config.database.port,
  database: config.database.dbProject,
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
})
Rosanny Sihombing's avatar
Rosanny Sihombing committed
31

Rosanny Sihombing's avatar
Rosanny Sihombing committed
32
projectConnection.query('USE ' + config.database.dbProject)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
33

Rosanny Sihombing's avatar
Rosanny Sihombing committed
34
const dbConnection = {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
35
36
  user: userConnection,
  project: projectConnection
Rosanny Sihombing's avatar
Rosanny Sihombing committed
37
38
}

Rosanny Sihombing's avatar
Rosanny Sihombing committed
39
export { dbConnection }