Skip to content

Commit 6e20eaa

Browse files
committed
isDeleted argument added to trigger condition functions
* Test case updated to check condition function arguments
1 parent ad4382d commit 6e20eaa

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,13 @@ Name | Type | Description
100100

101101
#### Condition Function
102102

103-
A condition function accepts one or two arguments:
103+
A condition function accepts up to three arguments:
104104

105105
Argument Name | Description
106106
--------------|-----------------------------
107107
`row` | Table row data
108-
`newRow` | New row data (only available on `UPDATE` queries)
108+
`newRow` | New row data (only available on `UPDATE` queries, `null` for others)
109+
`rowDeleted` | Extra argument for aid in external caching: `true` on `DELETE` queries, `false` on `INSERT` queries, `null` on `UPDATE` queries.
109110

110111
Return `true` when the row data meets the condition to update the result set.
111112

lib/LiveMysqlSelect.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ LiveMysqlSelect.prototype.matchRowEvent = function(event){
4545
var self = this;
4646
var tableMap = event.tableMap[event.tableId];
4747
var eventName = event.getEventName();
48-
var trigger, row;
48+
var trigger, row, rowDeleted;
4949
for(var i = 0; i < self.triggers.length; i++){
5050
trigger = self.triggers[i];
5151
triggerDatabase = trigger.database ||
@@ -59,10 +59,11 @@ LiveMysqlSelect.prototype.matchRowEvent = function(event){
5959
for(var r = 0; r < event.rows.length; r++){
6060
row = event.rows[r];
6161
if(eventName === 'updaterows'){
62-
return trigger.condition.call(self, row.before, row.after);
62+
return trigger.condition.call(self, row.before, row.after, null);
6363
}else{
6464
// writerows or deleterows
65-
return trigger.condition.call(self, row);
65+
rowDeleted = eventName === 'deleterows';
66+
return trigger.condition.call(self, row, null, rowDeleted);
6667
}
6768
}
6869
}

test/index.js

+49-9
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,43 @@ module.exports = {
2323
querySequence(conn.db, [
2424
'DROP TABLE IF EXISTS ' + escId(table),
2525
'CREATE TABLE ' + escId(table) + ' (col INT UNSIGNED)',
26-
'INSERT INTO ' + escId(table) + ' (col) VALUES (10)',
2726
], function(results){
2827
queries.splice(0, queries.length);
2928

3029
var query = 'SELECT * FROM ' + escId(table);
30+
var conditionCheckIndex = 0;
3131
var triggers = [ {
32+
database: server.database,
33+
table: table,
34+
condition: function(row, newRow, isDeleted) {
35+
// Ensure that each call of this condition function receives
36+
// the correct arguments
37+
conditionCheckIndex++;
38+
switch(conditionCheckIndex) {
39+
case 1:
40+
// Row has been inserted
41+
test.equal(row.col, 10);
42+
test.equal(newRow, null);
43+
test.equal(isDeleted, false);
44+
break;
45+
case 2:
46+
// Row has been updated
47+
test.equal(row.col, 10);
48+
test.equal(newRow.col, 15);
49+
test.equal(isDeleted, null);
50+
break;
51+
case 3:
52+
// Row has been deleted
53+
test.equal(row.col, 15);
54+
test.equal(newRow, null);
55+
test.equal(isDeleted, true);
56+
break;
57+
}
58+
return true;
59+
}
60+
} ];
61+
// Second, resultsBuffer check query doesn't need the condition
62+
var triggersSimple = [ {
3263
database: server.database,
3364
table: table
3465
} ];
@@ -37,12 +68,13 @@ module.exports = {
3768
// After initial update
3869
if(data.length > 0 && data[0].col === 10){
3970
// Second select instance to check resultsBuffer
40-
conn.select(query, triggers).on('update', function(data){
71+
conn.select(query, triggersSimple).on('update', function(data){
4172
if(data.length > 0 && data[0].col === 15){
4273
// [1] Test in LiveMysqlSelect created later,
4374
// Ensure only First select, update, second select occurred
75+
// Along with the INSERT and UPDATE queries, 5 total
4476
// i.e. No duplicate selects, resultsBuffer working
45-
test.equal(queries.length, 3);
77+
test.equal(queries.length, 5);
4678
conn.db.query('DELETE FROM ' + escId(table));
4779
}
4880
}).on('added', function(row, index){
@@ -74,12 +106,6 @@ module.exports = {
74106
}
75107
});
76108

77-
querySequence(conn.db, [
78-
'UPDATE ' + escId(table) +
79-
' SET `col` = 15'
80-
], function(results){
81-
// ...
82-
});
83109
}
84110
}).on('added', function(row, index){
85111
test.equal(index, 0);
@@ -94,6 +120,20 @@ module.exports = {
94120
test.done();
95121
});
96122

123+
// Perform database operation sequence
124+
querySequence(conn.db, [
125+
'INSERT INTO ' + escId(table) + ' (col) VALUES (10)'
126+
], function(results){
127+
// Wait before updating the row
128+
setTimeout(function() {
129+
querySequence(conn.db, [
130+
'UPDATE ' + escId(table) + ' SET `col` = 15'
131+
], function(results){
132+
// ...
133+
});
134+
}, 100);
135+
});
136+
97137
});
98138
});
99139
},

0 commit comments

Comments
 (0)