Skip to content

Instantly share code, notes, and snippets.

@vordan
Last active December 13, 2021 20:00
Show Gist options
  • Save vordan/c07fefa08da4044c3cc4647939b11c7a to your computer and use it in GitHub Desktop.
Save vordan/c07fefa08da4044c3cc4647939b11c7a to your computer and use it in GitHub Desktop.
A node.js package that watches a MySQL database and runs callbacks on matched events. #node #JavaScript #mysql
mysql-events
https://www.npmjs.com/package/sql-events-listener
A node.js package that watches a MySQL database and runs callbacks on matched events.
Install
npm install @rodrigogs/mysql-events
MySQL configuration:
Enabling Binary Logging. Use SHOW BINARY LOGS query to determine whether Binlog has been enabled or not.
From MySQL 8.0, binary logging is enabled by default. Reference
Run the following query:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
Troubleshooting:
Authentication protocol
Message: MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client.
Solution: Perform the second step in MySQL configuration section.
Quick Start
const mysql = require('mysql');
const MySQLEvents = require('@rodrigogs/mysql-events');
const program = async () => {
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
});
const instance = new MySQLEvents(connection, {
startAtEnd: true,
excludedSchemas: {
mysql: true,
},
});
await instance.start();
instance.addTrigger({
name: 'TEST',
expression: '*',
statement: MySQLEvents.STATEMENTS.ALL,
onEvent: (event) => { // You will receive the events here
console.log(event);
},
});
instance.on(MySQLEvents.EVENTS.CONNECTION_ERROR, console.error);
instance.on(MySQLEvents.EVENTS.ZONGJI_ERROR, console.error);
};
program()
.then(() => console.log('Waiting for database events...'))
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment