denodrivers / postgres

PostgreSQL driver for Deno

Home Page:https://denodrivers.github.io/postgres

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Connection events

thesmart opened this issue · comments

Feature request

It would be great to have some way to monitor connection events for reporting purposes and to setup defaults for the connection. For example, in Node I would run:

dbPool.on("connect", (client: PoolClient) => {
  if (ENV.TEST) {
    // Prefer indexes even on small tables. This disables optimizations in PostgreSQL that 
    // prefers scanning small tables, thus changing how EXPLAIN works between test and prod environments. 
    // @see: https://www.postgresql.org/docs/9.4/static/runtime-config-query.html#GUC-ENABLE-SEQSCAN
    client.query(`SET enable_seqscan = OFF;`);
  }
});

I don't see a similar way to do this in the Deno driver.

@thesmart isn't

try {
  const client = await dbPool.connect()
  if (Deno.env.get("TEST") === true) {
    client.query(...)
  }
} catch (err) {
  // similar to on("error")
}

Easier to read and a more modern implementation, that might suit your need?

I'm not to keen to integrate uncontrollable async behaviour such as the EventEmitter style you are proposing

I find @ebebbington solution much more reliable for the current use case, and could be roughly translated to the following

export const getTestClient = async () => {
  const client = await pool.connect();
  if (Deno.env.get("TEST") === "true") {
    await client.queryArray`SET enable_seqscan = OFF`;
  }
  return client;
}

Humble thanks for the consideration this is getting!

I agree w/ the sentiment of style, I don’t like the event driven model either. However, setting this option for every PG connection reliably is my concern. Using a pool, I think I’d need be be certain I had popped every connection in the queue to run the SET on each. Further, if a connection goes down (assuming there is reconnection logic) I’d need to again run the SET query upon reconnect.

Some kind of connection hook might be helpful, especially for PG which has per-connection state.

Could you please point me towards an example of the behaviour you want here? I'm always open to add new features

Closing due to inactivity