mysql-live-select
Advanced tools
Comparing version 0.0.18 to 0.0.19
@@ -18,2 +18,3 @@ /* mysql-live-select, MIT License ben@latenightsketches.com | ||
self._resultsBuffer = {}; | ||
self._schemaCache = {}; | ||
@@ -24,9 +25,9 @@ self.zongjiSettings = { | ||
includeEvents: [ 'tablemap', 'writerows', 'updaterows', 'deleterows' ], | ||
includeSchema: {} | ||
includeSchema: self._schemaCache | ||
}; | ||
db.connect(function(error){ | ||
self.db.connect(function(error){ | ||
if(error) return callback && callback(error); | ||
var zongji = self.zongji = new ZongJi(settings); | ||
var zongji = self.zongji = new ZongJi(self.settings); | ||
@@ -40,2 +41,3 @@ zongji.on('binlog', function(event) { | ||
// Update each select statement if matches event | ||
function _nextSelect(index){ | ||
@@ -80,3 +82,3 @@ var select; | ||
// Update schema included in ZongJi events | ||
var includeSchema = self.zongjiSettings.includeSchema; | ||
var includeSchema = self._schemaCache; | ||
for(var i = 0; i < triggers.length; i++){ | ||
@@ -99,2 +101,19 @@ var triggerDatabase = triggers[i].database || self.settings.database; | ||
LiveMysql.prototype.pause = function(){ | ||
var self = this; | ||
self.zongjiSettings.includeSchema = {}; | ||
self.zongji.set(self.zongjiSettings); | ||
}; | ||
LiveMysql.prototype.resume = function(){ | ||
var self = this; | ||
self.zongjiSettings.includeSchema = self._schemaCache; | ||
self.zongji.set(self.zongjiSettings); | ||
// Update all select statements | ||
self._select.forEach(function(select) { | ||
select.update(); | ||
}); | ||
}; | ||
LiveMysql.prototype.end = function(){ | ||
@@ -101,0 +120,0 @@ var self = this; |
@@ -75,26 +75,44 @@ /* mysql-live-select, MIT License ben@latenightsketches.com | ||
var self = this; | ||
self.emit('update', rows); | ||
var diff = []; | ||
if(!self.base.settings.skipDiff){ | ||
var diff = []; | ||
var diffEvent = function(){ | ||
self.emit.apply(self, arguments); | ||
diff.push(Array.prototype.slice.call(arguments)); | ||
// Determine what changes before updating cache in order to | ||
// be able to skip all event emissions if no change | ||
// TODO update this algorithm to use less data | ||
rows.forEach(function(row, index){ | ||
if(self.data.length - 1 < index){ | ||
diff.push([ 'added', row, index ]); | ||
}else if(JSON.stringify(self.data[index]) !== JSON.stringify(row)){ | ||
diff.push([ 'changed', self.data[index], row, index ]); | ||
} | ||
rows.forEach(function(row, index){ | ||
if(self.data.length - 1 < index){ | ||
diffEvent('added', row, index); | ||
self.data[index] = row; | ||
}else if(JSON.stringify(self.data[index]) !== JSON.stringify(row)){ | ||
diffEvent('changed', self.data[index], row, index); | ||
self.data[index] = row; | ||
}); | ||
if(self.data.length > rows.length){ | ||
for(var i = self.data.length - 1; i >= rows.length; i--){ | ||
diff.push([ 'removed', self.data[i], i ]); | ||
} | ||
} | ||
if(diff.length !== 0){ | ||
self.emit('update', rows); | ||
diff.forEach(function(evt){ | ||
switch(evt[0]){ | ||
case 'added': | ||
// New row added to end | ||
self.data[evt[2]] = evt[1]; | ||
break; | ||
case 'changed': | ||
// Update row data reference | ||
self.data[evt[3]] = evt[2]; | ||
break; | ||
case 'removed': | ||
// Remove extra rows off the end | ||
self.data.splice(evt[2], 1); | ||
break; | ||
} | ||
if(!self.base.settings.skipDiff){ | ||
self.emit.apply(self, evt); | ||
} | ||
}); | ||
if(self.data.length > rows.length){ | ||
for(var i = self.data.length - 1; i >= rows.length; i--){ | ||
diffEvent('removed', self.data[i], i); | ||
} | ||
self.data.splice(rows.length, self.data.length - rows.length); | ||
} | ||
if(diff.length !== 0){ | ||
if(!self.base.settings.skipDiff){ | ||
// Output all difference events in a single event | ||
@@ -101,0 +119,0 @@ self.emit('diff', diff); |
{ | ||
"name": "mysql-live-select", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "Live updating MySQL SELECT statements", | ||
@@ -5,0 +5,0 @@ "main": "lib/LiveMysql.js", |
@@ -1,2 +0,2 @@ | ||
# mysql-live-select [![Build Status](https://travis-ci.org/numtel/mysql-live-select.svg)](https://travis-ci.org/numtel/mysql-live-select) | ||
# mysql-live-select [![Build Status](https://travis-ci.org/numtel/mysql-live-select.svg?branch=master)](https://travis-ci.org/numtel/mysql-live-select) | ||
@@ -112,2 +112,14 @@ NPM Package to provide events when a MySQL select statement result set changes. | ||
### LiveMysql.prototype.pause() | ||
Temporarily skip processing of updates from the binary log. | ||
### LiveMysql.prototype.resume() | ||
Begin processing updates after `pause()`. All active live select instances will be refreshed upon resume. | ||
### LiveMysql.prototype.end() | ||
Close connections and stop checking for updates. | ||
## LiveMysqlSelect object | ||
@@ -114,0 +126,0 @@ |
@@ -143,3 +143,40 @@ /* mysql-live-select, MIT License ben@latenightsketches.com | ||
}, | ||
pauseAndResume: function(test){ | ||
var waitTime = 500; | ||
var table = 'pause_resume'; | ||
server.on('ready', function(conn, esc, escId, queries){ | ||
querySequence(conn.db, [ | ||
'DROP TABLE IF EXISTS ' + escId(table), | ||
'CREATE TABLE ' + escId(table) + ' (col INT UNSIGNED)', | ||
'INSERT INTO ' + escId(table) + ' (col) VALUES (10)', | ||
], function(results){ | ||
var pauseTime = Date.now(); | ||
conn.select('SELECT * FROM ' + escId(table), [ { | ||
table: table, | ||
database: server.database | ||
} ]).on('update', function(rows){ | ||
if(rows.length > 0 && rows[0].col === 10){ | ||
test.ok(true); | ||
conn.pause(); | ||
setTimeout(function(){ | ||
conn.resume(); | ||
}, waitTime); | ||
}else if(rows.length > 0 && rows[0].col === 15){ | ||
// Ensure that waiting occurred | ||
test.ok(Date.now() - pauseTime > waitTime); | ||
test.done(); | ||
} | ||
}); | ||
querySequence(conn.db, [ | ||
'UPDATE ' + escId(table) + | ||
' SET `col` = 15' | ||
], function(results){ | ||
// ... | ||
}); | ||
}); | ||
}); | ||
}, | ||
stopAndActive: function(test){ | ||
// Must be last test that uses binlog updates since it calls stop() | ||
var table = 'stop_active'; | ||
@@ -146,0 +183,0 @@ server.on('ready', function(conn, esc, escId, queries){ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34725
622
157