dbconn.js 1.44 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
const mysql = require('mysql')

Rosanny Sihombing's avatar
Rosanny Sihombing committed
3
var env = process.env.NODE_ENV || 'testing';
Rosanny Sihombing's avatar
Rosanny Sihombing committed
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
const config = require('../config/config')[env]

// ==== USER ACOOUNT DB CONNECTION ====
var 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
})

userConnection.connect(function(err) {
    if (err) throw err;
})
userConnection.query('USE '+config.database.dbUser)
    
// user db connection test
userConnection.query('SELECT 1 + 5 AS solution', function (err, rows, fields) {
    if (err) throw err
    console.log('Solution = ', rows[0].solution)
})
//userConnection.end()

// ==== PROJECT DB CONNECTION ====
var projectConnection = mysql.createConnection({
    host: config.database.host_project,
    user: config.database.user,
    password: config.database.password,
    port: config.database.port,
    database: config.database.dbProject
})

projectConnection.connect(function(err) {
    if (err) throw err;
})
projectConnection.query('USE '+config.database.dbProject)
    
// projectdb connection test
projectConnection.query('SELECT 10 + 5 AS project', function (err, rows, fields) {
    if (err) throw err
    console.log('Project = ', rows[0].project)
})
//projectConnection.end()

var connection = {
    user: userConnection,
    project: projectConnection
}

module.exports = connection