Skip to content

Commit

Permalink
override select method to store selected db in client instance and re…
Browse files Browse the repository at this point in the history
…store selected db on connect
  • Loading branch information
jhpinson committed Oct 20, 2011
1 parent 3e95c55 commit 0745553
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ function RedisClient(stream, options) {
this.server_info = {};
this.auth_pass = null;
this.parser_module = null;

this.selected_db = null; // save the selected db here, used when reconnecting

var self = this;

this.stream.on("connect", function () {
Expand Down Expand Up @@ -139,7 +140,12 @@ RedisClient.prototype.do_auth = function () {
self.auth_callback(err, res);
self.auth_callback = null;
}


// restore the selected db if needed
if (this.selected_db !== null) {
this.send_command('select', [this.selected_db]);
}

// now we are really connected
self.emit("connect");
if (self.options.no_ready_check) {
Expand Down Expand Up @@ -174,6 +180,12 @@ RedisClient.prototype.on_connect = function () {
if (this.auth_pass) {
this.do_auth();
} else {

// restore the selected db if needed
if (this.selected_db !== null) {
this.send_command('select', [this.selected_db]);
}

this.emit("connect");

if (this.options.no_ready_check) {
Expand Down Expand Up @@ -682,6 +694,24 @@ commands.forEach(function (command) {
Multi.prototype[command.toUpperCase()] = Multi.prototype[command];
});

// store db in this.select_db to restore it on reconnect
RedisClient.prototype.select = function (db, callback) {

var self = this;

this.send_command('select', [db], function (err, res) {
if (err === null) {
self.selected_db = db;
}
if (typeof(callback) !== 'undefined') {
callback(err, res);
}

});
}
RedisClient.prototype.SELECT = RedisClient.prototype.select;


// Stash auth for connect and reconnect. Send immediately if already connected.
RedisClient.prototype.auth = function () {
var args = to_array(arguments);
Expand Down

0 comments on commit 0745553

Please sign in to comment.