Skip to content

Commit 6fbe974

Browse files
committed
Error handler for connection implemented
#2
1 parent 4d580f3 commit 6fbe974

File tree

4 files changed

+51
-33
lines changed

4 files changed

+51
-33
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,15 @@ This package has been tested to work in MySQL 5.5.40 and 5.6.19. Expected suppor
3737

3838
The `LiveMysql` constructor makes 3 connections to your MySQL database:
3939

40-
* Connection for executing `SELECT` queries (exposed on instance as `db` property)
40+
* Connection for executing `SELECT` queries (exposed `node-mysql` instance as `db` property)
4141
* Replication slave connection
4242
* `information_schema` connection for column information
4343

44-
One argument, an object defining the settings. In addition to the [`node-mysql` connection settings](https://github.com/felixge/node-mysql#connection-options), the following settings are available:
44+
45+
Argument | Type | Description
46+
---------|------|---------------------------
47+
`settings` | `object` | An object defining the settings. In addition to the [`node-mysql` connection settings](https://github.com/felixge/node-mysql#connection-options), the additional settings below are available.
48+
`callback` | `function` | Optional callback on connection success/failure. Accepts one argument, `error`.
4549

4650
Setting | Type | Description
4751
--------|------|------------------------------

lib/LiveMysql.js

+33-29
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ var mysql = require('mysql');
55

66
var LiveMysqlSelect = require('./LiveMysqlSelect');
77

8-
function LiveMysql(settings){
8+
function LiveMysql(settings, callback){
99
var self = this;
10-
var zongji = new ZongJi(settings);
1110
var db = mysql.createConnection(settings);
1211

1312
self.settings = settings;
14-
self.zongji = zongji;
13+
self.zongji = null;
1514
self.db = db;
1615
self._select = [];
1716
// Cache query results for any new, duplicate SELECT statements
@@ -24,42 +23,47 @@ function LiveMysql(settings){
2423
includeSchema: {}
2524
};
2625

27-
db.connect();
26+
db.connect(function(error){
27+
if(error && callback) return callback(error);
2828

29-
zongji.on('binlog', function(event) {
30-
if(event.getEventName() === 'tablemap') return;
31-
if(self._select.length === 0) return;
29+
var zongji = self.zongji = new ZongJi(settings);
3230

33-
// Cache query results within this update event
34-
var eventResults = {};
31+
zongji.on('binlog', function(event) {
32+
if(event.getEventName() === 'tablemap') return;
33+
if(self._select.length === 0) return;
3534

36-
function _nextSelect(index){
37-
var select;
38-
if(index < self._select.length){
39-
select = self._select[index];
40-
if(select.matchRowEvent(event)){
41-
if(select.query in eventResults){
42-
select._setRows(eventResults[select.query]);
43-
_nextSelect(index + 1);
44-
}else{
45-
select.update(function(error, rows){
46-
if(error === undefined){
47-
eventResults[select.query] = rows;
48-
}
35+
// Cache query results within this update event
36+
var eventResults = {};
37+
38+
function _nextSelect(index){
39+
var select;
40+
if(index < self._select.length){
41+
select = self._select[index];
42+
if(select.matchRowEvent(event)){
43+
if(select.query in eventResults){
44+
select._setRows(eventResults[select.query]);
4945
_nextSelect(index + 1);
50-
});
46+
}else{
47+
select.update(function(error, rows){
48+
if(error === undefined){
49+
eventResults[select.query] = rows;
50+
}
51+
_nextSelect(index + 1);
52+
});
53+
}
54+
}else{
55+
_nextSelect(index + 1);
5156
}
52-
}else{
53-
_nextSelect(index + 1);
5457
}
5558
}
56-
}
5759

58-
_nextSelect(0);
60+
_nextSelect(0);
5961

60-
});
62+
});
6163

62-
zongji.start(self.zongjiSettings);
64+
zongji.start(self.zongjiSettings);
65+
if(callback) return callback();
66+
});
6367
}
6468

6569
LiveMysql.prototype.select = function(query, triggers){

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mysql-live-select",
3-
"version": "0.0.13",
3+
"version": "0.0.14",
44
"description": "Live updating MySQL SELECT statements",
55
"main": "lib/LiveMysql.js",
66
"repository": "https://github.com/numtel/mysql-live-select",

test/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,17 @@ module.exports = {
176176
});
177177
});
178178
},
179-
179+
error_invalid_connection: function(test){
180+
var myTest = new LiveMysql({
181+
host: '127.0.0.1',
182+
port: 12345,
183+
user: 'not-working',
184+
password: 'hahhaha'
185+
}, function(error){
186+
test.equal(error.code, 'ECONNREFUSED');
187+
test.done();
188+
});
189+
},
180190
error_no_db_selected: function(test){
181191
server.on('ready', function(conn, esc, escId, queries){
182192

0 commit comments

Comments
 (0)