Skip to content
freewil edited this page Mar 16, 2012 · 5 revisions

Clients are responsible for creating Queries via the factory method Client#query. The Client can create a new query before the client is connected to the server or while other queries are executing. Internally the Client maintains a queue of Query objects which are popped and executed as the preceding Query completes. When the Client's internal query queue is emptied, the Client raises the drain event. It can be useful to monitor this event, and when the Client goes idle, disconnect it from the server. The [pg|pg] helper also monitors the drain event. When a pg created Client's internal query-queue drops to 0 it is returned to the connection pool.

example

    var client = new Client(...);
    var query1 = client.query("SELECT * FROM NOW()"); //query is queued.  client is not connected
    query1.on('end', function() {
      console.log('query 1 completed');
    });
    var query2 = client.query("SELECT * FROM NOW()"); //also queued
    query2.on('end', function() {
      console.log('query 2 completed');
    });
    client.on('drain', function() {
      console.log("drained");
    });
    //at this point nothing has been printed to the console
    client.connect();
    //this will print the following:
    //query 1 completed
    //query 2 completed
    //drained
Clone this wiki locally