mysql-events
A node.js package that watches a MySQL database and runs callbacks on matched events.
This package is based on the original ZongJi and the original mysql-events modules. Please make sure that you meet the requirements described at ZongJi, like MySQL binlog etc.
Check @kuroski's mysql-events-ui for a mysql-events
UI implementation.
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:
'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 = ;const MySQLEvents = ; const program = async { const connection = mysql; const instance = connection startAtEnd: true excludedSchemas: mysql: true ; await instancestart; instance; instance; instance;}; ;
Usage
### #constructor(connection, options)
-
Instantiate and create a database connection using a DSN
const dsn =host: 'localhost'user: 'username'password: 'password';const myInstance = dsn /* ZongJi options */ ; -
Instantiate and create a database connection using a preexisting connection
const connection = mysql;const myInstance = connection /* ZongJi options */ ; -
Options(the second argument) is for ZongJi options
const myInstance = /* connection */serverId: 3startAtEnd: true;
### #start()
- start function ensures that MySQL is connected and ZongJi is running before resolving its promisemyInstancestart;
### #stop()
- stop function terminates MySQL connection and stops ZongJi before resolving its promisemyInstance;
### #pause()
- pause function pauses MySQL connection until
#resume()
is called, this it useful when you're receiving more data than you can handle at the timemyInstance;
### #resume()
- resume function resumes a paused MySQL connection, so it starts to generate binlog events againmyInstance;
### #addTrigger({ name, expression, statement, onEvent })
- Adds a trigger for the given expression/statement and calls the
onEvent
function when the event happensinstance; - The
name
argument must be unique for each expression/statement, it will be user later if you want to remove a triggerinstance;instance; - The
expression
argument is very dynamic, you can replace any step by*
to make it wait for any schema, table or column eventsinstance;instance;instance;instance; - The
statement
argument indicates in which database operation an event should be triggeredAllowed statementsinstance; - The
onEvent
argument is a function where the trigger events should be threatedinstance;
### #removeTrigger({ name, expression, statement })
- Removes a trigger from the current instanceinstance;
### Instance events
- MySQLEvents class emits some events related to its MySQL connection and ZongJi instanceinstance;instance;
Tigger event object
It has the following structure:
type: 'INSERT | UPDATE | DELETE' schema: 'SCHEMA_NAME' table: 'TABLE_NAME' affectedRows: before: column1: 'A' column2: 'B' column3: 'C' ... after: column1: 'D' column2: 'E' column3: 'F' ... affectedColumns: 'column1' 'column2' 'column3' timestamp: 1530645380029 nextPosition: 1343 binlogName: 'bin.001'
Make sure the database user has the privilege to read the binlog on database that you want to watch on.
LICENSE
BSD-3-Clause © Rodrigo Gomes da Silva