dbconn.ts 1.66 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
var env = process.env.NODE_ENV || 'testing'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
4
const config = require('./config')[env]
Rosanny Sihombing's avatar
Rosanny Sihombing committed
5
6
7
8
9
10
11

// ==== USER ACOOUNT DB CONNECTION ====
var userConnection = mysql.createConnection({
    host: config.database.host,
    user: config.database.user,
    password: config.database.password,
    port: config.database.port,
Rosanny Sihombing's avatar
Rosanny Sihombing committed
12
13
    database: config.database.dbUser,
    multipleStatements: true
Rosanny Sihombing's avatar
Rosanny Sihombing committed
14
15
16
})

userConnection.connect(function(err) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
17
    if (err) throw err
Rosanny Sihombing's avatar
Rosanny Sihombing committed
18
})
Rosanny Sihombing's avatar
Rosanny Sihombing committed
19
userConnection.query('USE '+config.database.dbUser)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
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

// ALTERNATIVE approach: close db connection manually after every query
/*
var dbconn = function dbconn(query, values, next) {
    var connection = mysql.createConnection({
        host: config.database.host,
        user: config.database.user,
        password: config.database.password,
        port: config.database.port,
        database: config.database.db
    })
    connection.connect(function(err) {
        if (err) throw err;
    })
    connection.query(query, values, function(err) {
        connection.end(); // close the connection
        if (err) {
            throw err;
        }
        // Execute the callback
        next.apply(this, arguments);
    });
}
*/

// ==== PROJECT DB CONNECTION ====
var projectConnection = mysql.createConnection({
47
    host: config.database.host_project,
Rosanny Sihombing's avatar
Rosanny Sihombing committed
48
49
50
51
52
53
54
    user: config.database.user,
    password: config.database.password,
    port: config.database.port,
    database: config.database.dbProject
})

projectConnection.connect(function(err) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
55
    if (err) throw err
Rosanny Sihombing's avatar
Rosanny Sihombing committed
56
57
58
59
60
61
62
63
})
projectConnection.query('USE '+config.database.dbProject)

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

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