Skip to content

Latest commit

 

History

History
118 lines (94 loc) · 2.8 KB

README.md

File metadata and controls

118 lines (94 loc) · 2.8 KB

mysql-event-emitter

A mysql / mariadb event emitter.

For now it will only tell you *that* something changed - not what changed.

It is tested against mariadb 10.1.31.

Install

npm install mysql-event-emitter

Setup

Mysql Server

Enable binary log replication in /etc/mysql/my.cnf

[mysqld]
server-id        = 1
log_bin          = /var/log/mysql/mysql-bin
log_bin_index    = /var/log/mysql/mysql-bin.index
binlog-format    = row

Mysql User

Give your user the rights to read binary logs

GRANT REPLICATION SLAVE, REPLICATION CLIENT, SELECT ON *.* TO '[USER]'@'[HOST]'

Usage

const MyEmitter = require('mysql-event-emitter');

var mye =  MyEmitter({
  "mysql": {
    "user": "[USER]",
    "password": "[PWD]"
  }
});

mye.on('change', function(db, table, event){
  console.log('An ' + event + ' occured in ' + db + '.' + table);
}):

mye.on('insert', function(db, table){
  console.log('An insert occured in ' + db + '.' + table);
}):

// Vanilla Events
mye.on('MyDB.MyTable.insert', function(){
  console.log('An insert occured in MyDB.MyTable');
}):

mye.on('MyDB.MyTable', function(event){
  console.log('An ' + event + ' occured in MyDB.MyTable');
}):

mye.on('MyDB', function(table, event){
  console.log('An ' + event + ' occured in MyDB.' + table);
}):

mye.start(function(err){
  if (err) throw err;
  console.log('started');
});

Options

  • For a single instance no binlog option is needed
  • Any mysql connection and pool option is supported.

Defaults

{  
  // mysql pool options
  "mysql": {
    "[socket]": "[SOCKET]",
    "[host]":   "127.0.0.1",
    "user":     "[USER]",
    "password": "[PWD]",
    "[database]": null        // (is not required)
  }
  // binlog options
  "binlog": {
    "slaveId": 1,             // Needs to be counted up, if more than one instance is running  
    "recoverTimeout": 240,    // Time in ms between reconnection attempts. (Eg. on a mysql server restart)
  }
}

Events

Event Data .
change DB, Table, Event Any of the following events
insert DB, Table
update DB, Table
delete DB, Table
truncate DB, Table
connected
disconnected
reconnecting
recovering
error Error

Vanilla Events

For your individual db's and tables, vanilla events can be defined: mye.on('MyDB.MyTable.insert').

Event Data
[TABLE] Event
[TABLE].[EVENT]
[DATABASE] Table, Event
[DATABASE].[TABLE] Event
[DATABASE].[TABLE].[EVENT]