Skip to content

Commit cf6f108

Browse files
committed
Code organized
1 parent 50accf3 commit cf6f108

5 files changed

Lines changed: 85 additions & 44 deletions

File tree

‎README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ NPM Package to provide events when a MySQL select statement result set changes.
44

55
Built using the [`zongji` Binlog Tailer](https://github.com/nevill/zongji) and [`node-mysql`](https://github.com/felixge/node-mysql) projects.
66

7+
## *Under Construction*
8+
9+
This package is yet completed. In order to test it, please use the following command sequence as a guide:
10+
11+
```bash
12+
$ git clone https://github.com/numtel/mysql-live-select.git
13+
$ cd mysql-live-select
14+
$ npm install
15+
$ cd node_modules
16+
# Updated ZongJi module not yet available on NPM, clone repo instead:
17+
$ git clone https://github.com/nevill/zongji.git
18+
$ cd ..
19+
# To use example as-is, copy example data into MySQL
20+
$ mysql < example.sql
21+
# Configure example to match your MySQL settings
22+
# (Be sure to also to enable the binary log, as described in next section)
23+
$ vim example.js
24+
# Run example
25+
$ node example.js
26+
```
27+
28+
Coming soon:
29+
30+
* Automated tests
31+
732
## Installation
833

934
* Enable MySQL binlog in `my.cnf`, restart MySQL server after making the changes.

‎example.sql‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
CREATE TABLE `players` (
3+
`id` int(11) NOT NULL AUTO_INCREMENT,
4+
`name` varchar(45) DEFAULT NULL,
5+
`score` int(11) NOT NULL DEFAULT '0',
6+
PRIMARY KEY (`id`)
7+
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
8+
9+
INSERT INTO `players` (`name`, `score`) VALUES
10+
('Kepler', 40),('Leibniz',50),('Maxwell',60),('Planck',70);

‎lib/LiveMysql.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var ZongJi = require('zongji');
2+
var mysql = require('mysql');
3+
var LiveMysqlSelect = require('./LiveMysqlSelect');
4+
5+
function LiveMysql(settings){
6+
var self = this;
7+
var zongji = new ZongJi(settings);
8+
var db = mysql.createConnection(settings);
9+
10+
self.settings = settings;
11+
self.zongji = zongji;
12+
self.db = db;
13+
self._select = [];
14+
15+
db.connect();
16+
17+
zongji.on('binlog', function(event) {
18+
if(event.getEventName() === 'tablemap') return;
19+
20+
self._select.forEach(function(select){
21+
if(select.matchRowEvent(event))
22+
select.update();
23+
});
24+
});
25+
26+
zongji.start({
27+
serverId: settings.serverId,
28+
startAtEnd: true,
29+
includeEvents: [ 'tablemap', 'writerows', 'updaterows', 'deleterows' ]
30+
});
31+
}
32+
33+
LiveMysql.prototype.select = function(query, triggers){
34+
var self = this;
35+
var newSelect = new LiveMysqlSelect(query, triggers, this);
36+
self._select.push(newSelect);
37+
return newSelect;
38+
};
39+
40+
41+
module.exports = LiveMysql;

index.js renamed to lib/LiveMysqlSelect.js

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,12 @@
1-
var ZongJi = require('zongji');
2-
var mysql = require('mysql');
31
var EventEmitter = require('events').EventEmitter;
42
var util = require('util');
53

6-
function LiveMysql(settings){
7-
var self = this;
8-
var zongji = new ZongJi(settings);
9-
var db = mysql.createConnection(settings);
10-
11-
self.settings = settings;
12-
self.zongji = zongji;
13-
self.db = db;
14-
self._select = [];
15-
16-
db.connect();
17-
18-
zongji.on('binlog', function(event) {
19-
if(event.getEventName() === 'tablemap') return;
20-
21-
self._select.forEach(function(select){
22-
if(select.matchRowEvent(event))
23-
select.update();
24-
});
25-
});
26-
27-
zongji.start({
28-
serverId: settings.serverId,
29-
startAtEnd: true,
30-
includeEvents: [ 'tablemap', 'writerows', 'updaterows', 'deleterows' ]
31-
});
32-
}
33-
34-
LiveMysql.prototype.select = function(query, triggers){
35-
var self = this;
36-
var newSelect = new LiveMysqlSelect(query, triggers, this);
37-
self._select.push(newSelect);
38-
return newSelect;
39-
};
40-
414
function LiveMysqlSelect(query, triggers, base){
425
if(!query)
436
throw new Error('query required');
447
if(!(triggers instanceof Array))
458
throw new Error('triggers array required');
46-
if(!(base instanceof LiveMysql))
9+
if(typeof base !== 'object')
4710
throw new Error('base LiveMysql instance required');
4811

4912
var self = this;
@@ -57,10 +20,8 @@ function LiveMysqlSelect(query, triggers, base){
5720
}
5821

5922
util.inherits(LiveMysqlSelect, EventEmitter);
60-
// util.inherits(LiveMysqlSelect, Array);
61-
6223

63-
LiveMysqlSelect.prototype._escapeQueryFun: function(query){
24+
LiveMysqlSelect.prototype._escapeQueryFun = function(query){
6425
var self = this;
6526
if(typeof query === 'function'){
6627
var escId = self.base.db.escapeId;
@@ -114,4 +75,4 @@ LiveMysqlSelect.prototype.update = function(){
11475
}
11576
};
11677

117-
module.exports = LiveMysql;
78+
module.exports = LiveMysqlSelect;

‎package.json‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "mysql-live-select",
33
"version": "0.0.1",
44
"description": "Live updating MySQL SELECT statements",
5-
"main": "index.js",
5+
"main": "lib/LiveMysql.js",
6+
"repository": "https://github.com/numtel/mysql-live-select",
67
"scripts": {
78
"test": "nodeunit --reporter=minimal test"
89
},
@@ -14,5 +15,8 @@
1415
"update"
1516
],
1617
"author": "Ben Green <ben@latenightsketches.com> (http://github.com/numtel)",
17-
"license": "MIT"
18+
"license": "MIT",
19+
"dependencies": {
20+
"mysql": "^2.5.4"
21+
}
1822
}

0 commit comments

Comments
 (0)