Change mysql library to mysql2 as it is more maintained and support latest mysql authention (#862)

* Use mysql2 lib

* fix pool on acquire event cause MaxListenersExceededWarning (#854)

before: every query need to register pool on acquire event to specify queryFormat based on payload type
will cause MaxListenersExceededWarning

after:
from https://www.npmjs.com/package/mysql#pooling-connections

pool.query is a shortcut for pool.getConnection() -> connection.query() -> connection.release()

so use pool.getConnection and then
set queryFormat before query method be called

Co-authored-by: Dave Conway-Jones <dceejay@users.noreply.github.com>

* fix mysql require

* Add decimalNumbers flag true to mysql beta

* mysql remove old Timeout option, clarify timezone options

* add mysqlConnectionLimit settings option.

Co-authored-by: saknarak <saknarak@gmail.com>
This commit is contained in:
Dave Conway-Jones 2022-01-05 10:56:14 +00:00 committed by GitHub
parent e6930274e2
commit fea47843d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 63 deletions

View File

@ -22,7 +22,7 @@
</div>
<div class="form-row">
<label for="node-config-input-tz"><i class="fa fa-clock-o"></i> <span data-i18n="mysql.label.timezone"></span></label>
<input type="text" id="node-config-input-tz">
<input type="text" id="node-config-input-tz" placeholder="&#177;hh:mm">
</div>
<div class="form-row">
<label for="node-config-input-charset"><i class="fa fa-language"></i> <span data-i18n="mysql.label.charset"></span></label>
@ -32,6 +32,7 @@
<label for="node-config-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
<input type="text" id="node-config-input-name" data-i18n="[placeholder]node-red:common.label.name">
</div>
<div class="form-tips"><span data-i18n="[html]mysql.tip"></span></div>
</script>
<script type="text/javascript">

View File

@ -2,7 +2,7 @@
module.exports = function(RED) {
"use strict";
var reconnect = RED.settings.mysqlReconnectTime || 20000;
var mysqldb = require('mysql');
var mysqldb = require('mysql2');
function MySQLNode(n) {
RED.nodes.createNode(this,n);
@ -41,10 +41,10 @@ module.exports = function(RED) {
timezone : node.tz,
insecureAuth: true,
multipleStatements: true,
connectionLimit: 50,
timeout: 30000,
connectionLimit: RED.settings.mysqlConnectionLimit || 50,
connectTimeout: 30000,
charset: node.charset
charset: node.charset,
decimalNumbers: true
});
}
@ -112,65 +112,41 @@ module.exports = function(RED) {
if (node.mydbConfig.connected) {
if (typeof msg.topic === 'string') {
//console.log("query:",msg.topic);
var bind = [];
if (Array.isArray(msg.payload)) {
bind = msg.payload;
node.mydbConfig.pool.on('acquire', function(connection) {
connection.config.queryFormat = null;
});
}
else if (typeof msg.payload === 'object' && msg.payload !== null) {
bind = msg.payload;
node.mydbConfig.pool.on('acquire', function(connection) {
connection.config.queryFormat = function(query, values) {
if (!values) {
return query;
}
return query.replace(/\:(\w+)/g, function(txt, key) {
if (values.hasOwnProperty(key)) {
return this.escape(values[key]);
}
return txt;
}.bind(this));
};
});
}
node.mydbConfig.pool.query(msg.topic, bind, function(err, rows) {
node.mydbConfig.pool.getConnection(function (err, conn) {
if (err) {
status = {fill:"red",shape:"ring",text:RED._("mysql.status.error")+": "+err.code};
conn.release()
status = { fill: "red", shape: "ring", text: RED._("mysql.status.error") + ": " + err.code };
node.status(status);
node.error(err,msg);
node.error(err, msg);
if (done) { done(); }
return
}
else {
// if (rows.constructor.name === "OkPacket") {
// msg.payload = JSON.parse(JSON.stringify(rows));
// }
// else if (rows.constructor.name === "Array") {
// if (rows[0] && rows[0].constructor.name === "RowDataPacket") {
// msg.payload = rows.map(v => Object.assign({}, v));
// }
// else if (rows[0] && rows[0].constructor.name === "Array") {
// if (rows[0][0] && rows[0][0].constructor.name === "RowDataPacket") {
// msg.payload = rows.map(function(v) {
// if (!Array.isArray(v)) { return v; }
// v.map(w => Object.assign({}, w))
// });
// }
// else { msg.payload = rows; }
// }
// else { msg.payload = rows; }
// }
// else { msg.payload = rows; }
msg.payload = rows;
send(msg);
status = {fill:"green",shape:"dot",text:RED._("mysql.status.ok")};
node.status(status);
var bind = [];
if (Array.isArray(msg.payload)) {
bind = msg.payload;
}
if (done) { done(); }
// if (node.mydbConfig.pool._freeConnections.indexOf(node.mydbConfig.connection) === -1) {
// node.mydbConfig.connection.release();
// }
});
else if (typeof msg.payload === 'object' && msg.payload !== null) {
bind = msg.payload;
}
conn.config.queryFormat = Array.isArray(msg.payload) ? null : customQueryFormat
conn.query(msg.topic, bind, function (err, rows) {
conn.release()
if (err) {
status = { fill: "red", shape: "ring", text: RED._("mysql.status.error") + ": " + err.code };
node.status(status);
node.error(err, msg);
}
else {
msg.payload = rows;
send(msg);
status = { fill: "green", shape: "dot", text: RED._("mysql.status.ok") };
node.status(status);
}
if (done) { done(); }
});
})
}
else {
if (typeof msg.topic !== 'string') { node.error("msg.topic : "+RED._("mysql.errors.notstring")); done(); }
@ -200,3 +176,15 @@ module.exports = function(RED) {
}
RED.nodes.registerType("mysql",MysqlDBNodeIn);
}
function customQueryFormat(query, values) {
if (!values) {
return query;
}
return query.replace(/\:(\w+)/g, function(txt, key) {
if (values.hasOwnProperty(key)) {
return this.escape(values[key]);
}
return txt;
}.bind(this));
}

View File

@ -19,6 +19,7 @@
"notstring": "the query is not defined as a string",
"notconnected": "Database not connected",
"notconfigured": "MySQL database not configured"
}
},
"tip": "Tip: The timezone should be specified as &#177;hh:mm or leave blank for 'local'."
}
}

View File

@ -1,9 +1,9 @@
{
"name": "node-red-node-mysql",
"version": "0.3.0",
"version": "1.0.0-beta-5",
"description": "A Node-RED node to read and write to a MySQL database",
"dependencies": {
"mysql": "^2.18.1"
"mysql2": "^2.3.3"
},
"repository": {
"type": "git",