Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppost for using and async using syntax #472

Closed
langpavel opened this issue Feb 27, 2024 · 2 comments
Closed

Suppost for using and async using syntax #472

langpavel opened this issue Feb 27, 2024 · 2 comments

Comments

@langpavel
Copy link
Contributor

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
@langpavel
Copy link
Contributor Author

I already prepared PR for the pool client #473

@langpavel
Copy link
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant