mysql-promise.js
799 Bytes
'use strict';
const mysql = require('promise-mysql');
const config = require('../config/config');
class MysqlPromise {
constructor() {
this.connect().then(conn => {
this.connect = conn;
});
}
connect() {
if (this.connect) {
return Promise.resolve(this.connect);
}
return mysql.createConnection({
host: config.mysql.host,
user: config.mysql.user,
password: config.mysql.password,
port: config.mysql.port,
database: config.mysql.database
}).then(conn => {
return conn;
}).catch(err => {
console.error(`mysql connect fail, err: ${err.message}`);
return null;
});
}
}
module.exports = MysqlPromise;