0.1.10 • Published 4 years ago

sql-listener v0.1.10

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

SQL Listener

Node.js Package to listening replication log events for stream realtime database.

Installation

add package to your project:

npm install sql-listener

Events

  • Insert event.
  • Update event.
  • Delete event.

Note: with above event you will get 2 objects, the row before the object was modified and the row after it was modfied, allowing you to see what columns was effected.

Prerequisites

  • 1. Configure SQL for replication.
# binlog config
server-id        = 1
binlog_format    = ROW
log_bin          = /var/log/mysql/mysql-bin.log or (mariadb-bin)
expire_logs_days = 10            // optional
max_binlog_size  = 100M          // optional
symbolic-links	 = 0		 // optional for secure

then restart your sql server.

  • 2. Create a new MySQL user.
  • 3. Give the new account access to the databases you wish to log.
  • 4. Grant replication privileges to the new user like so:
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repluser'@'localhost'

Features

APIdescription
tableIdID from table in database
tableMaptable schemas
schemadatabase name
tabletable name
typeevent name
rowsarray of affected row with before and after affected / object
affectedlength of affected rows
diffoptional parameter(key want add to different, index from array rows default 0) get different values from rows object before and after

Example

/**
 * import sql-listener's package
 */ 
var SqlListener = require('sql-listener')

/**
 * Create a new isntance
 */
var listener = new SqlListener({
    "host": "localhost",
    "user": "repluser",
    "password": "password"
    
    /**
     * @see node-mysql for connection options
     */
});

/**
 * Listening to the `client_1` database
 */
listener.listen("client_1");

/**
 * Listening for new user on the `client_1.user` table
 *
 * Each time an insert happens on the user table with insert event
 * will is fired
 */
listener.on("client_1.user.insert", function(event){
	/**
	 * add this line if you wont listen tablemap every event fired
	 */
	 if (event.type() === 'tablemap') return

	/**
	 * Log the effected rows
	 */
	console.log(event.rows());
});

/**
 * Listening all events on the `client_1.user` table
 */
listener.on("client_1.user.*", function(event){
	/**
	 * Log the event type
	 */
	console.log(event.type());
});

/**
 * Catch all events on all tables on the `client_1` database
 */
listener.on("client_1.*.*", function(event){
	/**
	 * Log the event type
	 */
	console.log(event.type());
});

/**
 * Start the client
 */
listener.start();

Requirements

  • Node.js v0.10+

Contributions

We highly encourage contributions from the community, please feel free to create feature requests, pull requests and issues.

Please also note, the core of this project is ZongJi, so if your feature/pr/issue is for that area of the project, please send ot to ZongJi.

License

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago