12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- const mysql = require('mysql');
- const dbConfig = require('../conf/database');
- const adminDatabasePool = mysql.createPool(dbConfig.mysql.adminDatabase);
- const userInfoDatabasePool = mysql.createPool(dbConfig.mysql.userInfoDatabase);
- const userDatabasePool = mysql.createPool(dbConfig.mysql.userDatabase);
- /**
- * 封装使用占位符的query语句
- * @param {*} pool
- * @param {string} sql
- * @param {*} args
- * @param {function} callback
- */
- function queryArgs(pool, sql, args, callback) {
- pool.getConnection((err, connection) => {
- if (err) {
- console.error('error connecting: ' + err.stack);
- callback({
- err: err,
- errInfo: '数据库连接错误,请重试'
- });
- return;
- }
- console.log('connected as id ' + connection.threadId);
- connection.query(sql, args, (err, results, fields) => {
- if (err) {
- callback({
- err: err,
- errInfo: '数据库连接错误,请重试'
- });
- } else {
- callback(err, results, fields);
- }
- //释放链接
- connection.release();
- });
- });
- }
- module.exports = {
- pool: {
- adminDatabasePool: adminDatabasePool,
- userInfoDatabasePool: userInfoDatabasePool,
- userDatabasePool: userDatabasePool
- },
- queryArgs: queryArgs
- }
|