aboutsummaryrefslogtreecommitdiff
path: root/src/db.js
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-02-28 16:11:57 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-02-28 16:11:57 +0300
commitfeaa5065f900a9c031ca7d66d80957040e2ee99f (patch)
treec6dbaf2861364725df1c54a8ba3423ca273107e0 /src/db.js
parent142869948c40900569f339a2177e95a3be3bbdfb (diff)
refactor: moved some files to lib/
Diffstat (limited to 'src/db.js')
-rw-r--r--src/db.js49
1 files changed, 0 insertions, 49 deletions
diff --git a/src/db.js b/src/db.js
deleted file mode 100644
index 7874a92..0000000
--- a/src/db.js
+++ /dev/null
@@ -1,49 +0,0 @@
-const config = 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: config.get('mysql_host'),
- user: config.get('mysql_user'),
- password: config.get('mysql_password'),
- database: config.get('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)
-} \ No newline at end of file