Skip to content

Instantly share code, notes, and snippets.

@batazo
Forked from Rowadz/monitoring_binary_logs.js
Created May 17, 2022 16:39
Show Gist options
  • Save batazo/2d990c6344a88c6d02f5aec20f88596a to your computer and use it in GitHub Desktop.
Save batazo/2d990c6344a88c6d02f5aec20f88596a to your computer and use it in GitHub Desktop.
Monitoring binary logs via Nodejs
const mysql = require('mysql');
const MySQLEvents = require('@rodrigogs/mysql-events');
const ora = require('ora'); // cool spinner
const spinner = ora({
text: '🛸 Waiting for database events... 🛸',
color: 'blue',
spinner: 'dots2'
});
const program = async () => {
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root'
});
const instance = new MySQLEvents(connection, {
startAtEnd: true // to record only the new binary logs, if set to false or you didn'y provide it all the events will be console.logged after you start the app
});
await instance.start();
instance.addTrigger({
name: 'monitoring all statments',
expression: 'TEST.*', // listen to TEST database !!!
statement: MySQLEvents.STATEMENTS.ALL, // you can choose only insert for example MySQLEvents.STATEMENTS.INSERT, but here we are choosing everything
onEvent: e => {
console.log(e);
spinner.succeed('👽 _EVENT_ 👽');
spinner.start();
}
});
instance.on(MySQLEvents.EVENTS.CONNECTION_ERROR, console.error);
instance.on(MySQLEvents.EVENTS.ZONGJI_ERROR, console.error);
};
program()
.then(spinner.start.bind(spinner))
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment