How to use the mysql2.createPool function in mysql2

To help you get started, we’ve selected a few mysql2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google / wikiloop-battlefield / server / index.js View on Github external
function setupSTikiApiLisenter(app) {
  let stikiRouter = express();

  const apicache = require('apicache');
  let cache = apicache.middleware;
  const onlyGet = (req, res) => res.method === `GET`;
  stikiRouter.use(cache('1 week', onlyGet));

  const mysql = require('mysql2');

  // create the pool
  const pool = mysql.createPool(process.env.STIKI_MYSQL);
  // now get a Promise wrapped instance of that pool
  const promisePool = pool.promise();

  stikiRouter.get('/stiki', asyncHandler(async (req, res) => {
    logger.debug(`req.query`, req.query);
    let revIds = JSON.parse(req.query.revIds);
    const [rows, _fields] = await promisePool.query(`SELECT R_ID, SCORE FROM scores_stiki WHERE R_ID in (${revIds.join(',')})`);
    res.send(rows);
    req.visitor
        .event({ec: "api", ea: "/scores"})
        .send();
  }));

  stikiRouter.get('/stiki/:wikiRevId', asyncHandler(async (req, res) => {
    let revIds = req.query.revIds;
    logger.debug(`req.query`, req.query);
github wangweianger / zane-web-blog-node.js-ejs-mysql / src / tool / mysql.js View on Github external
import {
    DB
} from '../config.js'

const mysql2 = require('mysql2');
const pool = mysql2.createPool({
    host: DB.HOST,
    user: DB.USER,
    password: DB.PASSWORD,
    database: DB.DATABASE,
    port: DB.PROT,
    waitForConnections: DB.WAITFORCONNECTIONS, // 是否等待链接  
    connectionLimit: DB.POOLLIMIT, // 连接池数
    queueLimit: DB.QUEUELIMIT, // 排队限制 
});
const promisePool = pool.promise();


// let mysql = require('mysql2/promise');
// let connection = await mysql.createConnection({
//     host: DB.HOST,
//     user: DB.USER,
github keymetrics / pm2-io-apm / src / census / plugins / __tests__ / mysql2.spec.ts View on Github external
function createPool (url: string): any {
  return new mysql.createPool(url)
}
github wangweianger / mysqls / src / main.js View on Github external
function init(config = {}) {
    const mysql2 = require('mysql2');
    ispool = typeof(config.ispool) === 'boolean' ? config.ispool : true;
    if (ispool) {
        connection = mysql2.createPool({
            host: config.host || '127.0.0.1',
            user: config.user || 'root',
            password: config.password || 'root',
            database: config.database || 'test',
            port: config.port || 3306,
            waitForConnections: config.waitConnection || true,
            connectionLimit: config.connectionLimit || 10,
            queueLimit: config.queueLimit || 0,
        });
    } else {
        connection = mysql.createConnection({
            host: config.host || '127.0.0.1',
            user: config.user || 'root',
            password: config.password || 'root',
            database: config.database || 'test',
            port: config.port || 3306,
github zesty-io / accounts-ui / api / src / db / index.js View on Github external
module.exports = (options) => {
  let opts = Object.assign({}, {
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: process.env.DB_NAME
  }, options)

  try {
    return mysql.createPool(opts)
  } catch (err) {
    console.log('Failed Connecting to MySQL', err)
  }
}
github danprocoder / kasky / src / database / connection.js View on Github external
const mysql = require('mysql2')
const config = require('../core/config')
const env = require('../helpers/env')

const database = config.get('database')[env.getCurrentEnvironment()]

const pool = mysql.createPool({
  host: database.host,
  user: database.user,
  database: database.name,
  password: database.pass,
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
})

exports.getDatabase = function () {
  return pool.promise().getConnection()
}

exports.escape = function (str) {
  return pool.escape(str)
}
github trendmicro / SecureCodingDojo / trainingportal / db.js View on Github external
query(sql,params,callback){

    if(arguments.length===2){
      if(typeof params === 'function'){
        callback = params;
        params = [];
      }
    }

    if(MYSQL_CONFIG!==null){
      if(this.conPool._closed){
        this.conPool = mysql.createPool(MYSQL_CONFIG);
      }
      this.conPool.query(sql,params,callback);
    }
    else{
      liteDB.all(sql,params,callback);      
    }
  }