-
Notifications
You must be signed in to change notification settings - Fork 0
Query
incomplete documentation
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.
### 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 .
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);
});
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.
note: if this event is not handled it will propagate to the global event loop. This can potentially crash your node process.
var query = client.query('SELECT asdfasdfasdf');
query.on('error', function(error) {
//handle the error
});
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.
I'm still working on the API for prepared statements. Check out the tests for more up to date examples, but what I'm working towards is something like this:
var client = new Client({
user: 'brian',
database: 'test'
});
var query = client.query({
text: 'select * from person where age < $1',
values: [21]
});
query.on('row', function(row) {
console.log(row);
});
query.on('end', function() { client.end() });