denodrivers/postgres

Suppost for `using` and `async using` syntax

Closed this issue · 2 comments

Is your feature request related to a problem? Please describe.

Deno already supports using and await using syntax with some APIs

Describe the solution you'd like

Instead of this:

{
  const client = await pgPool.connect();
  try {
    // ... await some work
  } finally {
    client.release()
  }
}

I wish write this:

{
  using client = await pgPool.connect();
  // ... await some work
} // client is released automatically

In addition, second step

Similar syntax can be used for transactions too, for example:

{
  using client = await pgPool.connect();
  await using transaction = await client.begin();
  await transaction.queryArray(...);
  await transaction.commit(); // otherwise it will rollback
}
// - automatically rollback transaction if not commited and
//   report, that transaction was not properly closed (either commited or rolled back)
// - release client

Advanced example:

{
  using client = await pgPool.connect();
  try {
    await using transaction = await client.begin();
    await transaction.queryArray(...);
    if (somethingBadHappened) {
      await transaction.rollback();
    }
    await transaction.commit();
  }
  // automatically rollback transaction if not commited and
  // report, that transaction was not properly closed (either commited or rolled back)
  catch(error)
  {
    // transaction is rolled back
    console.log(error);
  }
} // release client

I already prepared PR for the pool client #473

There is still second step missing – transaction abstraction (with Disposable or AsyncDisposable).

I still don't know what can help best in this case.

Also I discovered very usefull helper statement COMMIT AND CHAIN.

It's relatively easy to use without need of abstraction, pooling do the task well, so I'm closing this and I'm satisfied.

There is benefit of wrapping transaction in standalone lifecycle object,
but for now I'm not able to catch them all…

So, I'm closing this issue as resolved, but I'm open to talk about posible implementations!