You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+66-59Lines changed: 66 additions & 59 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,27 +7,31 @@ This package is based on the [ZongJi](https://github.com/nevill/zongji) node mod
7
7
```javascript
8
8
var MySQLEvents =require('mysql-events');
9
9
var dsn = {
10
-
host: _dbhostname_,
11
-
user: _dbusername_,
12
-
password: _dbpassword_,
13
-
};
10
+
host: _dbhostname_,
11
+
user: _dbusername_,
12
+
password: _dbpassword_,
13
+
};
14
14
var mysqlEventWatcher =MySQLEvents(dsn);
15
-
var watcher =mysqlEventWatcher.add('myDB.table.field.value', function (oldRow, newRow) {
16
-
//row inserted
17
-
if (oldRow ===null) {
18
-
//insert code goes here
19
-
}
20
-
21
-
//row deleted
22
-
if (newRow ===null) {
23
-
//delete code goes here
24
-
}
25
-
26
-
//row updated
27
-
if (oldRow !==null&& newRow !==null) {
28
-
//update code goes here
29
-
}
30
-
}, 'Active');
15
+
var watcher =mysqlEventWatcher.add(
16
+
'myDB.table.field.value',
17
+
function (oldRow, newRow) {
18
+
//row inserted
19
+
if (oldRow ===null) {
20
+
//insert code goes here
21
+
}
22
+
23
+
//row deleted
24
+
if (newRow ===null) {
25
+
//delete code goes here
26
+
}
27
+
28
+
//row updated
29
+
if (oldRow !==null&& newRow !==null) {
30
+
//update code goes here
31
+
}
32
+
},
33
+
'match this string or regex'
34
+
);
31
35
```
32
36
33
37
#Installation
@@ -38,26 +42,30 @@ npm install mysql-events
38
42
#Usage
39
43
- Import the module into your application
40
44
```javascript
41
-
var MySQLEvents =require('mysql-events');
45
+
var MySQLEvents =require('mysql-events');
42
46
```
43
47
44
48
- Instantiate and create a database connection
45
49
```sh
46
-
var dsn = {
47
-
host:'localhost',
48
-
user:'username',
49
-
password: 'password'
50
-
};
51
-
var myCon = MySQLEvents(dsn);
50
+
var dsn = {
51
+
host:'localhost',
52
+
user:'username',
53
+
password: 'password'
54
+
};
55
+
var myCon = MySQLEvents(dsn);
52
56
```
53
57
54
58
Make sure the database user has the privilege to read the binlog on database that you want to watch on.
55
59
56
60
- Use the returned object to add new watchers
57
61
```sh
58
-
var event1 = myCon.add('dbName.tableName.fieldName.value', function(oldRow, newRow) {
59
-
//code goes here
60
-
}, 'Active');
62
+
var event1 = myCon.add(
63
+
'dbName.tableName.fieldName.value',
64
+
function(oldRow, newRow) {
65
+
//code goes here
66
+
},
67
+
'Active'
68
+
);
61
69
```
62
70
63
71
This will listen to any change in the _fieldName_ and if the changed value is equal to __Active__, then triggers the callback. Passing it 2 arguments. Argument value depends on the event.
@@ -71,46 +79,45 @@ It has the following structure:
71
79
72
80
```
73
81
{
74
-
database: dbName,
75
-
table: tableName,
76
-
affectedColumns: {
77
-
[{
78
-
name: fieldName1,
79
-
charset: String,
80
-
type: Number
81
-
metedata: String
82
-
},
83
-
{
84
-
name: fieldName2,
85
-
charset: String,
86
-
type: Number
87
-
metedata: String
88
-
}]
89
-
},
90
-
changedColumns: [fieldName1, fieldName2],
91
-
fields: {
92
-
fieldName1: recordValue1,
93
-
fieldName2: recordValue2,
94
-
....
95
-
....
96
-
....
97
-
fieldNameN: recordValueN
98
-
}
82
+
database: dbName,
83
+
table: tableName,
84
+
affectedColumns: {
85
+
[{
86
+
name: fieldName1,
87
+
charset: String,
88
+
type: Number
89
+
metedata: String
90
+
},{
91
+
name: fieldName2,
92
+
charset: String,
93
+
type: Number
94
+
metedata: String
95
+
}]
96
+
},{
97
+
changedColumns: [fieldName1, fieldName2],
98
+
fields: {
99
+
fieldName1: recordValue1,
100
+
fieldName2: recordValue2,
101
+
....
102
+
....
103
+
....
104
+
fieldNameN: recordValueN
105
+
}
99
106
}
100
107
```
101
108
102
109
##Remove an event
103
110
```
104
-
event1.remove();
111
+
event1.remove();
105
112
```
106
113
107
114
##Stop all events on the connection
108
115
```
109
-
myCon.stop();
116
+
myCon.stop();
110
117
```
111
118
112
119
#Watcher Setup
113
-
Its basically a dot '.' seperated string. It can have the following combinations
120
+
Its basically a dot '.' seperated string. It can have the following combinations
114
121
115
122
-_database_: watches the whole database for changes (insert/update/delete). Which table and row are affected can be inspected from the oldRow & newRow
116
123
-_database.table_: watches the whole table for changes. Which rows are affected can be inspected from the oldRow & newRow
@@ -119,4 +126,4 @@ It has the following structure:
119
126
-_database.table.column.regexp_: watches for changes in the column and only trigger the callback if the changed value passes a regular expression test to the 3rd argument passed to the add(). The 3rd argument must be a Javascript Regular Expression Object, like, if you want to match for a starting sting (eg: MySQL) in the value, use /MySQL/i. This will trigger the callback only if the new value starts with MySQL
0 commit comments