dbconn.ts 1.01 KB
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 ?? 'testing'
Rosanny Sihombing's avatar
updates    
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
8
9
10
11
12
13
const userConnection = mysql.createConnection({
  host: config.database.host,
  user: config.database.user,
  password: config.database.password,
  port: config.database.port,
  database: config.database.dbUser,
  multipleStatements: true
Rosanny Sihombing's avatar
Rosanny Sihombing committed
14
15
})

Rosanny Sihombing's avatar
Rosanny Sihombing committed
16
17
userConnection.connect(function (err) {
  if (err != null) throw err
Rosanny Sihombing's avatar
Rosanny Sihombing committed
18
})
Rosanny Sihombing's avatar
Rosanny Sihombing committed
19
userConnection.query('USE ' + String(config.database.dbUser))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
20
21

// ==== PROJECT DB CONNECTION ====
Rosanny Sihombing's avatar
Rosanny Sihombing committed
22
23
24
25
26
27
const projectConnection = mysql.createConnection({
  host: config.database.host_project,
  user: config.database.user,
  password: config.database.password,
  port: config.database.port,
  database: config.database.dbProject
Rosanny Sihombing's avatar
Rosanny Sihombing committed
28
29
})

Rosanny Sihombing's avatar
Rosanny Sihombing committed
30
31
projectConnection.connect(function (err) {
  if (err != null) throw err
Rosanny Sihombing's avatar
Rosanny Sihombing committed
32
})
Rosanny Sihombing's avatar
Rosanny Sihombing committed
33
projectConnection.query('USE ' + String(config.database.dbProject))
Rosanny Sihombing's avatar
Rosanny Sihombing committed
34

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

Rosanny Sihombing's avatar
Rosanny Sihombing committed
40
export = connection