使用其他服务中的方法进行 TypeOrm 事务

TypeOrm transaction with method in other service

我在 nestjs 中使用了 typeorm。 但我不知道如何使用方法

进行交易

例如,

async createTrack(track) {
try {

  const createdTrack = await this.createTrack(track);

  //this.boardService.createBoard will work by transaction.
  const board = await this.boardService.createBoard({
      ...track.board
  });

  return board;
} catch (e) {
  throw e;
}

我想将 boardSerivce 的方法与交易一起使用。

怎么办?


我解决了。

试试吧。 TypeORM transaction with query builder

使用https://github.com/odavid/typeorm-transactional-cls-hooked

你查过文档了吗? https://docs.nestjs.com/techniques/database#transactions

从上面的文档中复制

const queryRunner = this.connection.createQueryRunner();

  await queryRunner.connect();
  await queryRunner.startTransaction();
  try {
    await queryRunner.manager.save(users[0]);
    await queryRunner.manager.save(users[1]);

    await queryRunner.commitTransaction();
  } catch (err) {
    // since we have errors lets rollback the changes we made
    await queryRunner.rollbackTransaction();
  } finally {
    // you need to release a queryRunner which was manually instantiated
    await queryRunner.release();
  }