nestjs-boilerplate icon indicating copy to clipboard operation
nestjs-boilerplate copied to clipboard

Typeorm Transaction

Open lamberto15 opened this issue 9 months ago • 4 comments

Base on the current repository setup, is there an easy way to do transaction without rewriting the repository?

lamberto15 avatar Feb 27 '25 08:02 lamberto15

Base on the current repository setup, is there an easy way to do transaction without rewriting the repository?基于当前仓库设置,有没有一种简单的方法进行事务处理而无需重写仓库?

This is just what I wanted😭

MitchellRyrie-evanBuck avatar Feb 27 '25 16:02 MitchellRyrie-evanBuck

@MitchellRyrie-evanBuck Hello,I managed to accomplish this without modifying the repository by using the typeorm-transactional package.

  1. In main.ts, add the following configuration: Image
  2. In app.module.ts, initialize the transactional data source: addTransactionalDataSource(new DataSource(options)); Image
  3. To test it, annotate the validateLogin method in AuthService, which performs multiple read/write operations on the database: Image
  4. Now, all database read/write operations within the method are wrapped in a single transaction: Image

lamberto15 avatar Mar 04 '25 08:03 lamberto15

@lamberto15 Thanks

MitchellRyrie-evanBuck avatar Mar 04 '25 10:03 MitchellRyrie-evanBuck

@lamberto15 thanks for quick rundown

mtthsnc avatar May 05 '25 19:05 mtthsnc

@lamberto15 The typeorm-transactional package hasn’t been updated in two years. Is it still stable and reliable?

sheiy avatar Aug 18 '25 03:08 sheiy