typeorm/typeorm

Abort DB operations / Transaction using AbortSignal

Open

#8552 aperta il 20 gen 2022

Vedi su GitHub
 (12 commenti) (21 reazioni) (0 assegnatari)TypeScript (6466 fork)batch import
good first issue

Metriche repository

Star
 (36.093 star)
Metriche merge PR
 (Merge medio 66g 23h) (16 PR mergiate in 30 g)

Descrizione

Feature Description

The Problem

Aborting DB operations is useful when the user abort some request when the DB operation is no longer needed (e.g. getting result for autocomplete and then the user type more characters (this is not my use case but rather a common use case that need the abort feature))

The Solution

Using AbortSignal to abort DB operation and transaction.

There are 2 approaches I have in mind:

  1. Passing signal
  2. Returning object with abort function

I prefer the 1st option as it keep the library usage design

Usage

When I use signal in the below example I mean an AbortSignal instance

Repository and EntityManager
userRepository.find({
    withDeleted: true,
    signal,
});
QueryBuilder
const firstUser = await connection
    .getRepository(User)
    .createQueryBuilder("user")
    .where("user.id = :id", { id: 1 })
    // here:
    .addSignal(signal)
    .getOne();
Transactions

Using Connection or EntityManager:


// An isolation level can be passed in the object near the signal property
await getManager().transaction({ signal }, async transactionalEntityManager => {

});

Using QueryRunner:

await queryRunner.startTransaction({ signal });

Using decorators: How this should be used? Maybe can set the abort signal on the passed manager

Passing the signal to the DB transaction will abort the transaction when the signal abort emit, passing

Considered Alternatives

Additional Context

Relevant Database Driver(s)

All of them basically

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✅ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Guida contributor