Skip to content
brianc edited this page Dec 18, 2010 · 19 revisions

Not to be created directly from its constructor, the Query is returned from Client#query. It functions primarily as an EventEmitter allowing you to handle returned rows.

Events

### Row : (__object__ row)

Emitted by the query whenever a row is received from the PostgreSQL server upon query execution, the event listeners are passed the parsed, type-coerced row .

example

    var query = client.query('SELECT name, age as user_age FROM users');
    query.on('row', function(row) {
      console.log('user "%s" is %d years old', row.name, row.user_age);
    });
### Error : (__object__ error)

Emitted by the query when an error is encountered within the context of query execution, the event listeners are passed the error event from the PostgreSQL server.

If the Query object was created with an optional callback function, the error event will not fire. This prevents you from having to handle the error both in the callback function and on the event listener.

note: If this event (or any event with the name 'error') is not handled it will propagate to the global event loop. This can potentially crash your node process. This is standard node.js behavior..

examples

no callback function
    var query = client.query('SELECT asdfasdfasdf');
    query.on('error', function(error) {
      //handle the error
    });
callback function
    var query = client.query('SELECT LAKJDLSKJF', function(err, result) {
      //err is the error returned from the PostgreSQL server
      //handle the error here
    });
    query.on('error', function() {
      //this code will never execute
      assert.ok(false, "This will never be called because you supplied the optional query callback function");
    })
### End

Emitted by the query when all rows have been returned or when an error has been encountered. In either circumstance, the query's execution is finished and it is no longer interacting with the connection.

todo: have this return the postgresql server status on insert/update/delete etc commands

Prepared statements

I'm still working on the API for prepared statements. Check out the tests for more up to date examples.

Clone this wiki locally