diff --git a/lib/client.js b/lib/client.js index 659e5468..0d0f2e39 100644 --- a/lib/client.js +++ b/lib/client.js @@ -98,9 +98,11 @@ p.pulseQueryQueue = function() { } }; -p.query = function(config) { +p.query = function(config, callback) { //can take in strings or config objects - var query = new Query((config.text || config.name) ? config : { text: config }); + config = (config.text || config.name) ? config : { text: config }; + config.callback = config.callback || callback; + var query = new Query(config); this.queryQueue.push(query); this.pulseQueryQueue(); return query; diff --git a/lib/index.js b/lib/index.js index 5dfcb4a1..5b5d8046 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,20 +4,32 @@ var net = require('net'); var Client = require(__dirname+'/client'); +//wrap up common connection management boilerplate var connect = function(config, callback) { var client = new Client(config); client.connect(); - var onError = function() { - - }; + + var onError = function(error) { + client.connection.removeListener('readyForQuery', onReady); + callback(error); + } + + var onReady = function() { + client.removeListener('error', onError); + callback(null, client); + client.on('drain', client.end.bind(client)); + } + client.once('error', onError); - client.connection.once('readyForQuery', function() { - callback - }); -}; + + //TODO refactor + //i don't like reaching into the client's connection for attaching + //to specific events here + client.connection.once('readyForQuery', onReady); +} module.exports = { - Client: Client + Client: Client, Connection: require(__dirname + '/connection'), connect: connect -}; +} diff --git a/lib/query.js b/lib/query.js index 4e5cb8bd..f4ebc66f 100644 --- a/lib/query.js +++ b/lib/query.js @@ -11,6 +11,7 @@ var Query = function(config) { //for code clarity purposes we'll declare this here though it's not //set or used until a rowDescription message comes in this.rowDescription = null; + this.callback = config.callback; EventEmitter.call(this); }; @@ -35,6 +36,7 @@ p.submit = function(connection) { } var converters = []; var names = []; + var rows = []; var handleRowDescription = function(msg) { for(var i = 0; i < msg.fields.length; i++) { converters[i] = dataTypeParsers[msg.fields[i].dataTypeID] || noParse; @@ -48,6 +50,11 @@ p.submit = function(connection) { result[names[i]] = rawValue === null ? null : converters[i](rawValue); } self.emit('row', result); + + //if no reciever, buffer rows + if(self.callback) { + rows.push(result); + } }; var onError = function(err) { @@ -56,6 +63,9 @@ p.submit = function(connection) { connection.removeListener('dataRow', handleDatarow); connection.removeListener('error', onError); connection.removeListener('readyForQuery', onReadyForQuery); + if(self.callback) { + self.callback(err); + } self.emit('error', err); self.emit('end'); }; @@ -66,6 +76,9 @@ p.submit = function(connection) { connection.removeListener('dataRow', handleDatarow); connection.removeListener('readyForQuery', onReadyForQuery); connection.removeListener('error', onError); + if(self.callback) { + self.callback(null, {rows: rows}); + } self.emit('end'); }; diff --git a/test/integration/client/api-tests.js b/test/integration/client/api-tests.js index addb37ac..af194141 100644 --- a/test/integration/client/api-tests.js +++ b/test/integration/client/api-tests.js @@ -1,9 +1,39 @@ var helper = require(__dirname + '/../test-helper'); var pg = require(__dirname + '/../../../lib'); -var connected = false; -pg.connect(helper.args, function(err) { - connected = true; -}); +var connected = false +var called = false +pg.connect(helper.args, function(err, client) { + connected = true + test('error is null', function() { + assert.equal(err, null) + }) + + test('query execution', function() { + client.query('CREATE TEMP TABLE band(name varchar(100))') + client.query("INSERT INTO band (name) VALUES ('dead black hearts')") + client.query("SELECT * FROM band WHERE name = 'dead black hearts'", function(err, result) { + called = true; + assert.equal(result.rows.pop().name, 'dead black hearts') + }) + }) + +}) + +process.on('exit', function() { + assert.ok(connected, 'never connected') + assert.ok(called, 'query result callback was never called') +}) + + + + + +test('raises error if cannot connect', function() { + pg.connect({database:'asdlfkajsdf there is no way this is a real database, right?!'}, function(err, client) { + assert.ok(err, 'error was null') + }) +}) + + -assert.ok(connected);