nestjs-boilerplate
nestjs-boilerplate copied to clipboard
Typeorm Transaction
Base on the current repository setup, is there an easy way to do transaction without rewriting the repository?
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 Hello,I managed to accomplish this without modifying the repository by using the typeorm-transactional package.
- In main.ts, add the following configuration:
- In app.module.ts, initialize the transactional data source:
addTransactionalDataSource(new DataSource(options)); - To test it, annotate the validateLogin method in AuthService, which performs multiple read/write operations on the database:
- Now, all database read/write operations within the method are wrapped in a single transaction:
@lamberto15 Thanks
@lamberto15 thanks for quick rundown
@lamberto15 The typeorm-transactional package hasn’t been updated in two years. Is it still stable and reliable?