Skip to content

Commit b51a105

Browse files
authored
Fix MySQL output plugin query parameter, error handling (#243)
The query parameter misspelling meant that a properly spelled config file would not override the script default query, leading to a confusing situation. It looks like some debug logging was left in. This has been removed. If people want to log to stdout, they'll add an output to do so. The previous query callback statement had no effect, and we weren't logging out any errors in the callback. That made troubleshooting any database-related issues very tricky.
1 parent 3cbe4c3 commit b51a105

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

plugins/outputs/mysql/output_mysql.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,51 @@ var base_output = require('@pastash/pastash').base_output,
22
logger = require('@pastash/pastash').logger,
33
util = require('util');
44
var sqdb;
5-
var THIS ;
65
var mysql = require('mysql2');
76

87
function OutputMySql() {
98
base_output.BaseOutput.call(this);
109
this.mergeConfig({
1110
name: 'MySql',
12-
optional_params: ['db', 'query', 'host', 'user', 'password', 'port','interval_ms'],
11+
optional_params: ['db', 'query', 'host', 'user', 'password', 'port', 'interval_ms'],
1312
default_values: {
1413
'db' : 'database',
1514
'host': '127.0.0.1',
1615
'port': 3306,
1716
'user': 'root',
1817
'password': 'admin',
1918
'interval_ms': 10000,
20-
'qurey': "call SP_UpdateSyncResult('{|data|}');"
19+
'query': "call SP_UpdateSyncResult('{|data|}');"
2120
},
2221
start_hook: this.start,
2322
});
2423
}
2524
OutputMySql.prototype.start =function(callback) {
26-
THIS = this
2725
if (this.db) {
2826
try {
2927
var cfg = { database: this.db, rowsAsArray: true };
3028
if(this.host) cfg.host = this.host;
3129
if(this.user) cfg.user = this.user;
3230
if(this.password) cfg.password = this.password;
3331
sqdb = mysql.createConnection(cfg);
34-
logger.info('Initializing Outpu MySql:',this.db);
35-
} catch(e){ logger.error('Failed Initializing Filter MySql',e); }
32+
logger.info('Initializing Output MySql:',this.db);
33+
} catch(e) { logger.error('Failed Initializing Output MySql',e); }
3634
}
3735

38-
39-
logger.info('Initialized Filter MySql');
36+
logger.info('Initialized Output MySql');
4037
callback();
41-
42-
4338
}
4439
util.inherits(OutputMySql, base_output.BaseOutput);
4540

4641
OutputMySql.prototype.process = function(data) {
42+
var sqlstr = this.query
43+
sqlstr = sqlstr.replace('{|data|}',JSON.stringify(data, null, 2));
4744

48-
process.stdout.write('[STDOUT] ' + JSON.stringify(data, null, 2) + '\n');
49-
var sqlstr = THIS.qurey
50-
sqlstr= sqlstr.replace('{|data|}',JSON.stringify(data, null, 2) )
51-
52-
console.log(sqlstr );
5345
sqdb.query(sqlstr ,null, function(err, results, fields) {
54-
55-
sqlstr= sqlstr.replace('{|data|}',JSON.stringify(results, null, 2) )
56-
})
46+
if (err) {
47+
logger.error('MySQL query error:', err);
48+
}
49+
});
5750
};
5851

5952
OutputMySql.prototype.close = function(callback) {

0 commit comments

Comments
 (0)