node-mysql2
node-mysql2 copied to clipboard
How to log all SQL query ?
var mysql = require("mysql2");
const chalk = require('chalk');
...
pool.on("connection", function (connection) {
connection.on("enqueue", function (sequence) {
if ("Query" === sequence.constructor.name) {
console.log(chalk.green(`[+] mysql: ${sequence.sql}`));
}
});
});
...
I expect to see this on console:
[+] mysql: start transaction; insert into `entities` (`type`) values (3); insert into `companies` set `id` = last_insert_id(), `name` = 'google', `created_by` = 4, `url` = '/google2'; commit;
but nothing happens?
how to make it work on mysql2 ?
is it something that worked with mysqljs/mysql and does not work with mysql2?
Before I start helping with "how to make this example work" can you please describe what you see and how it's not matching what you want to see
thank you for replying me, this question has been asked 3 years ago: https://github.com/sidorares/node-mysql2/issues/610#issuecomment-314933491 did you add this feature?
I'll have to check, can't remember. ( or you can try to check it yourself in https://github.com/sidorares/node-mysql2/blob/master/lib/pool.js )
If not added I might have time to add this weekend
It seems doesn't work. Please add a feature to log all queries.
I need this feature badly.
+1 this feature would be very helpful!
@joynal @alete89 did you solve this?
Was it resolved? Is there any simple listener I can use to console.log any query executed?
@damianobarbati no, nothing added as far as I know. I'm open to adding something like
connection.on('commandStart', command => {
if (command.name === 'Query') {
console.log(command.sql);
}
});
It might be that all we need is this.emit('commandStart', this._command) on https://github.com/sidorares/node-mysql2/blob/b5b336b0783462b9d1d9b8801ec026e536006ea3/lib/connection.js#L482
do you want to try and create PR @damianobarbati ?