Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hodfords-solutions/nestjs-transaction
nestjs-transaction makes managing database transactions in NestJS easy and straightforward
https://github.com/hodfords-solutions/nestjs-transaction
nestjs transaction typeorm
Last synced: 3 days ago
JSON representation
nestjs-transaction makes managing database transactions in NestJS easy and straightforward
- Host: GitHub
- URL: https://github.com/hodfords-solutions/nestjs-transaction
- Owner: hodfords-solutions
- Created: 2022-04-15T13:07:50.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-09-24T03:17:08.000Z (4 months ago)
- Last Synced: 2024-12-27T00:56:09.484Z (29 days ago)
- Topics: nestjs, transaction, typeorm
- Language: TypeScript
- Homepage: https://opensource.hodfords.uk/nestjs-transaction
- Size: 163 KB
- Stars: 41
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
nestjs-transaction makes managing database transactions in NestJS easy and straightforward. It provides simple tools to start, commit, and roll back transactions, helping you ensure that multiple database operations are completed successfully or not at all. With `nestjs-transaction`, you can handle complex transaction scenarios with ease, making your data handling more reliable and error-free.
## Installation 🤖
Install the `nestjs-transaction` package with:
```ts
npm install @hodfords/nestjs-transaction --save
```## Usage 🚀
First, extend the `TransactionService` imported from the library in your service, and then use the `withTransaction`
method within the transaction callback to call your service.### How to use
##### your-service.service.ts
```typescript
@Injectable()
export class YourService extends TransactionService {
public constructor(
@InjectRepository(YourRepository) private repository: Repository,
private yourCustomRepository: CustomRepository,
private yourService: Service,
// Let's say you don't want to rebuild this service in the transaction
private yourCacheService: CacheService,
@Inject(forwardRef(() => ForwardService)) private yourForwardService: ForwardService
) {
super();
}async theMethodWillUseTransaction(payload: SomePayload) {
// logic code here
}
}
```##### your-controller.controller.ts
```typescript
import { DataSource } from 'typeorm';@Controller()
export class SomeController {
constructor(
private readonly yourService: YourService,
private dataSource: DataSource
) {}async method(payload: SomePayload): Promise {
return this.dataSource.transaction(async (entityManager) => {
return await this.yourService
.withTransaction(entityManager, { excluded: [CacheService] })
.theMethodWillUseTransaction(payload);
});
}
}
```### Exclude services from transaction
You can configure services to be excluded from transactions by specifying them in `transactionConfig` and importing it
into `AppModule````typescript
export const transactionConfig = TransactionModule.forRoot([MailService, I18nService, StorageService, DataSource]);
```## License 📝
This project is licensed under the MIT License