sql-listener v0.1.10
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
API | description |
---|---|
tableId | ID from table in database |
tableMap | table schemas |
schema | database name |
table | table name |
type | event name |
rows | array of affected row with before and after affected / object |
affected | length of affected rows |
diff | optional 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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago