NestJS decorator to undo all changes a method made to the database in case of failures

I want to create a NestJS decorator @Transactional() for service methods that will undo all changes made to the database in case of failures.

import { ConflictException, Inject, Injectable, NotFoundException } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { Brand } from './brand.entity';
import { BRANDS_REPOSITORY } from './brands.repository';
import { USERS_REPOSITORY } from '../users/users.repository';
import { User } from '../users/user.entity';
import { DATA_SOURCE } from '../database/database.providers';
import { Transactional } from '../../decorators/transactional.decorator';

@Injectable()
export class BrandsService {
  constructor(
    @Inject(BRANDS_REPOSITORY) private brandsRepository: Repository<Brand>,
    @Inject(USERS_REPOSITORY) private usersRepository: Repository<User>,
  ) {}

  @Transactional() // <<==
  async create(user: User, name: string) { // shouldn't make any changes in the database
    let brand = await this.brandsRepository.create({ name });
    brand = await this.brandsRepository.save(brand);

    user.permittedBrands.push(brand);
    await this.usersRepository.save(user);

    throw new Error('Something went wrong');

    return brand;
  }
}

This would be the answer to the question.

import { DataSource } from 'typeorm';

export function Transactional() {
  return function (target: any, propertyName: string, descriptor: TypedPropertyDescriptor<any>) {
    const method = descriptor.value;

    descriptor.value = async function (...args: any[]) {
      const dataSource: DataSource = this.dataSource;

      const queryRunner = dataSource.createQueryRunner();
      await queryRunner.startTransaction();

      const createQueryRunner = dataSource.createQueryRunner;
      const release = queryRunner.release;

      dataSource.createQueryRunner = () => queryRunner;
      queryRunner.release = () => Promise.resolve();

      try {
        const result = await method.apply(this, args);
        await queryRunner.commitTransaction();
        return result;
      } catch (err) {
        await queryRunner.rollbackTransaction();
        throw err;
      } finally {
        dataSource.createQueryRunner = createQueryRunner;
        queryRunner.release = release;
        await queryRunner.release();
      }
    };
  };
}

But I also had to add private dataSource: DataSource parameter to the constructor of BrandsService, because it is used in the decorator.

import { ConflictException, Inject, Injectable, NotFoundException } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { Brand } from './brand.entity';
import { BRANDS_REPOSITORY } from './brands.repository';
import { USERS_REPOSITORY } from '../users/users.repository';
import { User } from '../users/user.entity';
import { DATA_SOURCE } from '../database/database.providers';
import { Transactional } from '../../decorators/transactional.decorator';

@Injectable()
export class BrandsService {
  constructor(
    @Inject(BRANDS_REPOSITORY) private brandsRepository: Repository<Brand>,
    @Inject(USERS_REPOSITORY) private usersRepository: Repository<User>,
    @Inject(DATA_SOURCE) private dataSource: DataSource, // <<==
  ) {}

  @Transactional()
  async create(user: User, name: string) {
    let brand = await this.brandsRepository.create({ name });
    brand = await this.brandsRepository.save(brand);

    user.permittedBrands.push(brand);
    await this.usersRepository.save(user);

    throw new Error('Something went wrong');

    return brand;
  }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật