node-red-nodes/storage/mysql/68-mysql.js

142 lines
4.9 KiB
JavaScript
Raw Normal View History

module.exports = function(RED) {
"use strict";
var reconnect = RED.settings.mysqlReconnectTime || 20000;
var mysqldb = require('mysql');
2014-02-23 19:32:49 +01:00
function MySQLNode(n) {
RED.nodes.createNode(this,n);
this.host = n.host;
this.port = n.port;
this.tz = n.tz || "local";
this.connected = false;
this.connecting = false;
this.dbname = n.db;
2016-11-27 18:03:03 +01:00
this.setMaxListeners(0);
var node = this;
function doConnect() {
node.connecting = true;
2016-11-27 18:03:03 +01:00
node.emit("state","connecting");
node.connection = mysqldb.createConnection({
host : node.host,
port : node.port,
user : node.credentials.user,
password : node.credentials.password,
database : node.dbname,
timezone : node.tz,
insecureAuth: true,
multipleStatements: true
});
node.connection.connect(function(err) {
node.connecting = false;
if (err) {
2015-11-01 16:04:42 +01:00
node.error(err);
2016-11-27 18:03:03 +01:00
node.emit("state",err.code);
node.tick = setTimeout(doConnect, reconnect);
} else {
node.connected = true;
2016-11-27 18:03:03 +01:00
node.emit("state","connected");
}
});
node.connection.on('error', function(err) {
node.connected = false;
2016-11-27 18:03:03 +01:00
node.emit("state",err.code);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
doConnect(); // silently reconnect...
} else {
node.error(err);
doConnect();
}
});
}
this.connect = function() {
if (!this.connected && !this.connecting) { doConnect(); }
if (this.connected) { node.emit("state","connected"); }
else { node.emit("state","connecting"); }
}
this.on('close', function (done) {
if (this.tick) { clearTimeout(this.tick); }
node.connected = false;
2016-11-27 18:03:03 +01:00
node.emit("state"," ");
if (this.connection) {
node.connection.end(function(err) {
if (err) { node.error(err); }
done();
});
} else {
done();
}
});
}
RED.nodes.registerType("MySQLdatabase",MySQLNode, {
credentials: {
user: {type: "text"},
password: {type: "password"}
}
});
function MysqlDBNodeIn(n) {
RED.nodes.createNode(this,n);
this.mydb = n.mydb;
this.mydbConfig = RED.nodes.getNode(this.mydb);
if (this.mydbConfig) {
this.mydbConfig.connect();
var node = this;
2016-11-27 18:03:03 +01:00
node.mydbConfig.on("state", function(info) {
if (info === "connecting") { node.status({fill:"grey",shape:"ring",text:info}); }
else if (info === "connected") { node.status({fill:"green",shape:"dot",text:info}); }
else {
if (info === "ECONNREFUSED") { info = "connection refused"; }
if (info === "PROTOCOL_CONNECTION_LOST") { info = "connection lost"; }
node.status({fill:"red",shape:"ring",text:info});
}
});
node.on("input", function(msg) {
if (node.mydbConfig.connected) {
if (typeof msg.topic === 'string') {
//console.log("query:",msg.topic);
var bind = Array.isArray(msg.payload) ? msg.payload : [];
node.mydbConfig.connection.query(msg.topic, bind, function(err, rows) {
if (err) {
node.error(err,msg);
node.status({fill:"red",shape:"ring",text:"Error"});
}
else {
msg.payload = rows;
node.send(msg);
node.status({fill:"green",shape:"dot",text:"OK"});
}
});
}
else {
if (typeof msg.topic !== 'string') { node.error("msg.topic : the query is not defined as a string"); }
}
}
else {
node.error("Database not connected",msg);
2016-11-27 18:03:03 +01:00
node.status({fill:"red",shape:"ring",text:"not yet connected"});
}
});
node.on('close', function () {
node.mydbConfig.removeAllListeners();
node.status({});
});
}
else {
this.error("MySQL database not configured");
}
}
RED.nodes.registerType("mysql",MysqlDBNodeIn);
}