 noflo = require("noflo")
 
 # Pool all queries through a single connection for each database
-client = {}
+clients = {}
 
 class Client extends noflo.Component
 


      # Send to error port
      result.on "error", (e) =>
        e.query = query
        unless @outPorts.error.isAttached()
          throw new Error "No error port attached"

        @outPorts.error.send e
        @outPorts.error.disconnect()

      result.on "end", (result) =>
        @outPorts.out.beginGroup token
        if _.isEmpty rows
          @outPorts.out.send null


  startServer: (url) ->
    @endServer url
    clients[url] = new pg.Client url
    clients[url].connect()

  endServer: (url) ->
    if url?
      clients[url]?.end()
    else
      clients[url].end() for url in _.keys clients

exports.getComponent = -> new Client
