Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leocode/nest-tx
Database transaction module for NestJS
https://github.com/leocode/nest-tx
Last synced: 10 days ago
JSON representation
Database transaction module for NestJS
- Host: GitHub
- URL: https://github.com/leocode/nest-tx
- Owner: leocode
- Created: 2020-11-23T23:33:02.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-10-07T08:02:15.000Z (about 2 years ago)
- Last Synced: 2024-10-30T03:39:40.667Z (2 months ago)
- Language: TypeScript
- Size: 400 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# nest-tx - database transaction manager for NestJS
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/)
## Motivation
Transaction management is usually done in application services, which should not have knowledge of implementation
details
like DB engine or DB access library (like TypeORM). This way you can change the underlying DB technology without
modifying
business logic. `@leocode/nest-tx-core` provides an abstract way of managing transactions - it is not tied to any
particular database, driver or ORM. It can be used with many libraries and drivers by using adapters (
like `@leocode/nest-tx-typeorm`).## Currently supported ORMs/query builders/drivers:
- Knex
- TypeORM## Installation
`@leocode/nest-tx-core` must always be installed:
```
yarn add @leocode/nest-tx-core
```Then you can install particular adapters (and its peer dependencies) for your application, for example:
```
yarn add @leocode/nest-tx-typeorm
```## Usage
First, you need to register an adapter:
```typescript
import { Module } from '@nestjs/common';
import { TypeORMTransactionManagerModule } from '@leocode/nest-tx-typeorm';@Module({
imports: [
TypeORMTransactionManagerModule.forRoot(),
],
})
export class AppModule {}
```Then you can use
```typescript
import { Injectable } from '@nestjs/common';
import { InjectTransactionManager, TransactionManager } from '@leocode/nest-tx-core';
import { getEntityManagerFromTypeORMTransaction } from '@leocode/nest-tx-typeorm';@Injectable()
class CatsService {
constructor(
@InjectTransactionManager() private transactionManager: TransactionManager,
) {}async save() {
await this.transactionManager.withTransaction(async (tx) => {
/**
* NOTE: Usually you should write code like this in a separate repository class -
* it's written here for the example brevity.
*/
const manager = getEntityManagerFromTypeORMTransaction(tx);await manager.query('SELECT * FROM "table"');
});
}
}
```Consult the README file in each package to see how to use it:
- [knex](./packages/knex/README.md)
- [typeorm](./packages/typeorm/README.md)