Created
December 30, 2015 08:37
-
-
Save pegasuskim/0520adf71dc1484e9b42 to your computer and use it in GitHub Desktop.
mysql handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mysql = require('mysql'); | |
var inherits = require('util').inherits; | |
var EventEmitter = require('events').EventEmitter; | |
module.exports = MySql; | |
function MySql(config) { | |
var clusterConfig = { | |
removeNodeErrorCount: 1, // Remove the node immediately when connection fails. | |
defaultSelector: 'RR', | |
canRetry: true | |
}; | |
this.poolCluster = mysql.createPoolCluster(clusterConfig); | |
this.dirties = {}; | |
this.connections = {}; | |
this.config = config; | |
this.connect(config); | |
}; | |
inherits(MySql, EventEmitter); | |
MySql.prototype.connect = function(config) { | |
var self = this; | |
config.forEach(function(connectionGroup) { | |
self.poolCluster.add('MASTER', connectionGroup.master); | |
for( var i = 0; i < connectionGroup.slaves.length; i++) { | |
self.poolCluster.add('SLAVE'+(i+1), connectionGroup.slaves[i]); | |
} | |
}); | |
}; | |
MySql.prototype.getConnection = function(key, callback) { | |
var self = this; | |
self.poolCluster.getConnection(key, callback); | |
}; | |
MySql.prototype.query = function(statement, args, callback) { | |
var self = this; | |
self.getConnection('MASTER', function(error, connection) { | |
connection.query(statement, args, function(err, result) { | |
connection.release(); | |
callback(err, result); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment