aboutsummaryrefslogtreecommitdiff
path: root/src/db.js
blob: 0ffba84002b10cf4e0f381a0182133554b8542f7 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const {workerConfig} = require('./config')
const {getLogger} = require('./logger')
const mysql = require('promise-mysql')

let link
const logger = getLogger('db')

async function init() {
    link = await mysql.createConnection({
        host: workerConfig.mysql_host,
        user: workerConfig.mysql_user,
        password: workerConfig.mysql_password,
        database: workerConfig.mysql_database
    })
}

function wrap(method, isAsync = true, log = true) {
    return isAsync ? async function(...args) {
        if (log)
            logger.trace(`${method}: `, args)

        try {
            return await link[method](...args)
        } catch (error) {
            logger.error(`db.${method}:`, error, link)

            if (       error.code === 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR'
                    || error.code === 'PROTOCOL_CONNECTION_LOST'
                    || error.fatal === true) {
                // try to reconnect and call it again, once
                await init()
                return await link[method](...args)
            }
        }
    } : function(...args) {
        if (log)
            logger.trace(`${method}: `, args)

        return link[method](...args)
    }
}

module.exports = {
    init,
    query: wrap('query'),
    beginTransaction: wrap('beginTransaction'),
    commit: wrap('commit'),
    escape: wrap('escape', false, false)
}