mysql-live-select
Advanced tools
Comparing version 0.0.22 to 0.0.23
@@ -48,3 +48,3 @@ /* mysql-live-select, MIT License ben@latenightsketches.com | ||
var eventName = event.getEventName(); | ||
var trigger, row; | ||
var trigger, row, rowDeleted; | ||
for(var i = 0; i < self.triggers.length; i++){ | ||
@@ -63,6 +63,7 @@ trigger = self.triggers[i]; | ||
if(eventName === 'updaterows'){ | ||
return trigger.condition.call(self, row.before, row.after); | ||
return trigger.condition.call(self, row.before, row.after, null); | ||
}else{ | ||
// writerows or deleterows | ||
return trigger.condition.call(self, row); | ||
rowDeleted = eventName === 'deleterows'; | ||
return trigger.condition.call(self, row, null, rowDeleted); | ||
} | ||
@@ -170,2 +171,12 @@ } | ||
self.base._select.splice(index, 1); | ||
// If no other instance of the same query string, remove the resultsBuffer | ||
var sameCount = self.base._select.filter(function(select) { | ||
return select.query === self.query; | ||
}).length; | ||
if(sameCount === 0) { | ||
delete self.base._resultsBuffer[self.query]; | ||
} | ||
return true; | ||
@@ -172,0 +183,0 @@ }else{ |
{ | ||
"name": "mysql-live-select", | ||
"version": "0.0.22", | ||
"version": "0.0.23", | ||
"description": "Live updating MySQL SELECT statements", | ||
@@ -5,0 +5,0 @@ "main": "lib/LiveMysql.js", |
@@ -103,3 +103,3 @@ # mysql-live-select [![Build Status](https://travis-ci.org/numtel/mysql-live-select.svg?branch=master)](https://travis-ci.org/numtel/mysql-live-select) | ||
A condition function accepts one or two arguments: | ||
A condition function accepts up to three arguments: | ||
@@ -109,3 +109,4 @@ Argument Name | Description | ||
`row` | Table row data | ||
`newRow` | New row data (only available on `UPDATE` queries) | ||
`newRow` | New row data (only available on `UPDATE` queries, `null` for others) | ||
`rowDeleted` | Extra argument for aid in external caching: `true` on `DELETE` queries, `false` on `INSERT` queries, `null` on `UPDATE` queries. | ||
@@ -152,5 +153,5 @@ Return `true` when the row data meets the condition to update the result set. | ||
Tests must be run with a properly configured MySQL server. Configure test settings in `test/settings.mysql.js`. | ||
Tests must be run with a properly configured MySQL server. Configure test settings in `test/settings/mysql.js`. | ||
Execute `nodeunit` using the `npm test` command. | ||
Execute [Nodeunit](https://github.com/caolan/nodeunit) using the `npm test` command. | ||
@@ -157,0 +158,0 @@ ## License |
@@ -26,3 +26,2 @@ /* mysql-live-select, MIT License ben@latenightsketches.com | ||
'CREATE TABLE ' + escId(table) + ' (col INT UNSIGNED)', | ||
'INSERT INTO ' + escId(table) + ' (col) VALUES (10)', | ||
], function(results){ | ||
@@ -32,4 +31,36 @@ queries.splice(0, queries.length); | ||
var query = 'SELECT * FROM ' + escId(table); | ||
var conditionCheckIndex = 0; | ||
var triggers = [ { | ||
database: server.database, | ||
table: table, | ||
condition: function(row, newRow, isDeleted) { | ||
// Ensure that each call of this condition function receives | ||
// the correct arguments | ||
conditionCheckIndex++; | ||
switch(conditionCheckIndex) { | ||
case 1: | ||
// Row has been inserted | ||
test.equal(row.col, 10); | ||
test.equal(newRow, null); | ||
test.equal(isDeleted, false); | ||
break; | ||
case 2: | ||
// Row has been updated | ||
test.equal(row.col, 10); | ||
test.equal(newRow.col, 15); | ||
test.equal(isDeleted, null); | ||
break; | ||
case 3: | ||
// Row has been deleted | ||
test.equal(row.col, 15); | ||
test.equal(newRow, null); | ||
test.equal(isDeleted, true); | ||
break; | ||
} | ||
return true; | ||
} | ||
} ]; | ||
// Second, resultsBuffer check query doesn't need the condition | ||
var triggersSimple = [ { | ||
database: server.database, | ||
table: table | ||
@@ -42,8 +73,9 @@ } ]; | ||
// Second select instance to check resultsBuffer | ||
conn.select(query, triggers).on('update', function(data){ | ||
conn.select(query, triggersSimple).on('update', function(data){ | ||
if(data.length > 0 && data[0].col === 15){ | ||
// [1] Test in LiveMysqlSelect created later, | ||
// Ensure only First select, update, second select occurred | ||
// Along with the INSERT and UPDATE queries, 5 total | ||
// i.e. No duplicate selects, resultsBuffer working | ||
test.equal(queries.length, 3); | ||
test.equal(queries.length, 5); | ||
conn.db.query('DELETE FROM ' + escId(table)); | ||
@@ -80,8 +112,2 @@ } | ||
querySequence(conn.db, [ | ||
'UPDATE ' + escId(table) + | ||
' SET `col` = 15' | ||
], function(results){ | ||
// ... | ||
}); | ||
} | ||
@@ -101,2 +127,16 @@ }).on('added', function(row, index){ | ||
// Perform database operation sequence | ||
querySequence(conn.db, [ | ||
'INSERT INTO ' + escId(table) + ' (col) VALUES (10)' | ||
], function(results){ | ||
// Wait before updating the row | ||
setTimeout(function() { | ||
querySequence(conn.db, [ | ||
'UPDATE ' + escId(table) + ' SET `col` = 15' | ||
], function(results){ | ||
// ... | ||
}); | ||
}, 100); | ||
}); | ||
}); | ||
@@ -222,3 +262,4 @@ }); | ||
], function(results){ | ||
conn.select('SELECT * FROM ' + escId(table), [ { | ||
var query = 'SELECT * FROM ' + escId(table); | ||
conn.select(query, [ { | ||
table: table, | ||
@@ -232,2 +273,5 @@ database: server.database | ||
this.stop(); | ||
// When all instances of query removed, resultsBuffer removed too | ||
test.equal(typeof conn._resultsBuffer[query], 'undefined'); | ||
test.ok(!this.active()); | ||
@@ -253,5 +297,8 @@ conn.db.query('DELETE FROM ' + escId(table)); | ||
immediate_disconnection: function(test){ | ||
// Update serverId setting to prevent collision | ||
settings.serverId++; | ||
var myTest = new LiveMysql(settings, function(error){ | ||
myTest.end(); | ||
test.ok(typeof error === 'undefined'); | ||
settings.serverId--; | ||
test.done(); | ||
@@ -258,0 +305,0 @@ }); |
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
39094
734
158