Skip to content

Commit 26b1837

Browse files
committed
Added aggregate diff event
* Provide a single event for all changes from a binlog event
1 parent 7dc6b14 commit 26b1837

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ NPM Package to provide events when a MySQL select statement result set changes.
55
Built using the [`zongji` Binlog Tailer](https://github.com/nevill/zongji) and [`node-mysql`](https://github.com/felixge/node-mysql) projects.
66

77
* [Example Application using Express, SockJS and React](https://github.com/numtel/reactive-mysql-example)
8+
* [Meteor package for reactive MySQL](https://github.com/numtel/meteor-mysql)
89

910
## Installation
1011

@@ -121,6 +122,7 @@ Event Name | Arguments | Description
121122
`added` | `row`, `index` | Row added to result set at index
122123
`changed` | `row`, `newRow`, `index` | Row contents mutated at index
123124
`removed` | `row`, `index` | Row removed at index
125+
`diff` | `diff` | Aggregation of `added`, `changed`, `removed` events for current event into a single array for easier handling of multiple changes
124126
`error` | `error` | Unhandled errors will be thrown
125127

126128
## Running Tests

lib/LiveMysqlSelect.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,30 @@ LiveMysqlSelect.prototype._setRows = function(rows){
7575
self.emit('update', rows);
7676

7777
if(!self.base.settings.skipDiff){
78+
var diff = [];
79+
var diffEvent = function(){
80+
self.emit.apply(self, arguments);
81+
diff.push(Array.prototype.slice.call(arguments));
82+
}
7883
rows.forEach(function(row, index){
7984
if(self.data.length - 1 < index){
80-
self.emit('added', row, index);
85+
diffEvent('added', row, index);
8186
self.data[index] = row;
8287
}else if(JSON.stringify(self.data[index]) !== JSON.stringify(row)){
83-
self.emit('changed', self.data[index], row, index);
88+
diffEvent('changed', self.data[index], row, index);
8489
self.data[index] = row;
8590
}
8691
});
8792
if(self.data.length > rows.length){
8893
for(var i = self.data.length - 1; i >= rows.length; i--){
89-
self.emit('removed', self.data[i], i);
94+
diffEvent('removed', self.data[i], i);
9095
}
9196
self.data.splice(rows.length, self.data.length - rows.length);
9297
}
98+
if(diff.length !== 0){
99+
// Output all difference events in a single event
100+
self.emit('diff', diff);
101+
}
93102
}
94103

95104
self.lastUpdate = Date.now();

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mysql-live-select",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "Live updating MySQL SELECT statements",
55
"main": "lib/LiveMysql.js",
66
"repository": "https://github.com/numtel/mysql-live-select",

test/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ module.exports = {
4747
}).on('removed', function(row, index){
4848
test.equal(index, 0);
4949
test.equal(row.col, 15);
50+
}).on('diff', function(diff){
51+
// Only one row will change at once
52+
test.equal(diff.length, 1);
53+
// Index will always be 0, the first item
54+
test.equal(diff[0][diff[0].length - 1], 0);
55+
switch(diff[0][0]){
56+
case 'added':
57+
test.equal(diff[0][1].col, 10);
58+
break;
59+
case 'changed':
60+
test.equal(diff[0][1].col, 10);
61+
test.equal(diff[0][2].col, 15);
62+
break;
63+
case 'added':
64+
test.equal(diff[0][1].col, 15);
65+
break;
66+
}
5067
});
5168

5269
querySequence(conn.db, [

0 commit comments

Comments
 (0)