Skip to content

Commit

Permalink
Hook should not say it is connected or listen for queries until postg…
Browse files Browse the repository at this point in the history
…res is ready
  • Loading branch information
paulbellamy committed Feb 14, 2012
1 parent 02a17f4 commit dcc3166
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ var PostgresHook = exports.PostgresHook = function(options){
self.on('hook::ready', function(){
self._start();
});

self.on('*::postgres::query', function(sql, callback) {
self._query(sql, callback);
});
};

// Postgres inherits from Hook
Expand All @@ -31,11 +27,25 @@ util.inherits(PostgresHook, Hook);
PostgresHook.prototype._start = function() {
var self = this;

self.client = new pg.Client(self.options.database);
self.client.connect();

self.client.on('error', function(err) {
self._error(err);
// We have to use the callback API for this bit because
// pg currently doesn't emit a 'connected' or 'ready'
// event.
pg.connect(self.options.database, function(err, client) {
if (err) {
self._error(err);
} else {
self.client = client;

self.client.on('error', function(err) {
self._error(err);
});

self.on('**::postgres::query', function(sql, callback) {
self._query(sql, callback);
});

self.emit('postgres::connected');
}
});
};

Expand Down

0 comments on commit dcc3166

Please sign in to comment.