Skip to content

Commit 6fe822f

Browse files
Merge branch 'master' of github.com:spencerlambert/mysql-events
Pulling 0.0.5 changes
2 parents fc6e486 + 08aafc0 commit 6fe822f

File tree

5 files changed

+106
-421
lines changed

5 files changed

+106
-421
lines changed

README.md

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@ This package is based on the [ZongJi](https://github.com/nevill/zongji) node mod
77
```javascript
88
var MySQLEvents = require('mysql-events');
99
var dsn = {
10-
host: _dbhostname_,
11-
user: _dbusername_,
12-
password: _dbpassword_,
13-
};
10+
host: _dbhostname_,
11+
user: _dbusername_,
12+
password: _dbpassword_,
13+
};
1414
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+
);
3135
```
3236

3337
#Installation
@@ -38,26 +42,30 @@ npm install mysql-events
3842
#Usage
3943
- Import the module into your application
4044
```javascript
41-
var MySQLEvents = require('mysql-events');
45+
var MySQLEvents = require('mysql-events');
4246
```
4347

4448
- Instantiate and create a database connection
4549
```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);
5256
```
5357

5458
Make sure the database user has the privilege to read the binlog on database that you want to watch on.
5559

5660
- Use the returned object to add new watchers
5761
```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+
);
6169
```
6270

6371
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:
7179

7280
```
7381
{
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+
}
99106
}
100107
```
101108

102109
##Remove an event
103110
```
104-
event1.remove();
111+
event1.remove();
105112
```
106113

107114
##Stop all events on the connection
108115
```
109-
myCon.stop();
116+
myCon.stop();
110117
```
111118

112119
#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
114121

115122
- _database_: watches the whole database for changes (insert/update/delete). Which table and row are affected can be inspected from the oldRow & newRow
116123
- _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:
119126
- _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
120127

121128
#LICENSE
122-
MIT
129+
MIT

mysql-events-tests.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)