node+mysql+express接口开发数据库连接池

const DB_MYSQL = require('mysql');

const DB_CONFIG = require('./DBConfig');

/**

  • 数据库连接池
  • @type {Pool}

*/

const pool = DB_MYSQL.createPool({undefined

host: DB_CONFIG.database.HOST,

user: DB_CONFIG.database.USERNAME,

password: DB_CONFIG.database.PASSWORD,

database: DB_CONFIG.database.DATABASE,

port: DB_CONFIG.database.PORT

});

/**

  • 通用方法
  • @param sql
  • @param options

*/

const DB = (sql, options) =>{undefined

options= (options) ? options:{}

return new Promise((resolve, reject) => {undefined

    pool.getConnection((error, connection) => {undefined

        if (error) {undefined

            reject(error);

        } else {undefined

            connection.query(sql, options, (error, results, fields) => {undefined

                //事件驱动回调

                if (results){undefined

                    resolve(results);

                }

                if (error) {undefined

                    reject(error)

}

            });

        }

         //释放连接

        pool.releaseConnection(connection);

    });

})

};

module.exports = DB;

上一篇:springMVC---基础


下一篇:[WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu)