How to use mysql2

Comprehensive mysql2 code examples:

How to use mysql2.format:

103
104
105
106
107
108
109
110
111
112
  // [rows, fields] = values ? await conn.query(sql, values) : await conn.query(sql);
  [rows, fields] = await conn.query(sql, values);
  stop = Date.now();
} finally {
  if (rows && this.logging) {
    let sqlString = mysql.format(typeof sql === 'object' ? sql.sql : sql, values || sql.values).trim().replace(/\n/g, ' ').replace(/\s+/g, ' ').replace(/,\s/g, ',');
    if (sqlString.lastIndexOf(';') !== sqlString.length - 1) {
      sqlString = sqlString + ';';
    }
    const cmd = sqlString.split(' ')[0].toUpperCase();

How to use mysql2.raw:

140
141
142
143
144
145
146
147
148
149
query: query,
insert: insert,
update: update,
insertOrUpdate: insertOrUpdate,
now: function(){
    //return mysql.raw('CURRENT_TIMESTAMP()')
    return formatDate.ymdhis(new Date())
},
getRows: getRows,
chunk: chunk,

How to use mysql2.escape:

0
1
2
3
4
5
6
7
8
9
10
const { escapeId, escape } = require('mysql2');
const { schemaTypeToSql } = require('./utils');


class SqlQuery {
	static OPERATIONS = {
		$lt: (value, result) => result.push('<', escape(value)),
		$lte: (value, result) => result.push('<=', escape(value)),
		$gt: (value, result) => result.push('>', escape(value)),
		$gte: (value, result) => result.push('>=', escape(value)),
		$ne: (value, result) => result.push('<>', escape(value))

How to use mysql2.Pool:

How to use mysql2.escapeId:

16
17
18
19
20
21
22
23
24
25

static createTable (tableName, schema) {
	const lines = [];
	for (const field in schema) {
		const info = schema[field];
		let line = escapeId(field);
		line += ' ';
		line += schemaTypeToSql(info.type);

		if (info.type == 'enum') {

How to use mysql2.default:

82
83
84
85
86
87
88
89
90
91
module.exports = /** @class */ (function (_super) {
    __extends(Db, _super);
    function Db() {
        var _this_1 = _super.call(this) || this;
        _this_1.MysqlStore = _this_1.mysql_session;
        _this_1.mysql = mysql2_1.default;
        _this_1.__ = Singleton_1.Singleton.getLodash();
        _this_1.connection_configurations = {
            // Host name for database connection:
            host: config.configurations().host,

How to use mysql2.createPoolCluster:

505
506
507
508
509
510
511
512
513
514
    cluster = undefined
    queryable = undefined
  }
}

var cluster = mysql.createPoolCluster()
cluster.add(connectionOptions)
cluster.getConnection(function (err, conn) {
  if (err) throw err
  queryable = conn

How to use mysql2.createPool:

458
459
460
461
462
463
464
465
466
467
      queryablePromise.end()
      queryablePromise = undefined
    }
  }

  queryable = mysql.createPool(connectionOptions)
  queryablePromise = mysqlPromise.createPool(connectionOptions)

  cb()
})

How to use mysql2.createConnection:

435
436
437
438
439
440
441
442
443
444
    queryablePromise.end()
    queryablePromise = undefined
  }
}

queryable = mysql.createConnection(connectionOptions)
queryable.connect()

mysqlPromise.createConnection(connectionOptions).then(connection => {
  queryablePromise = connection