10.1.3 • Published 10 months ago

@hodfords/nestjs-transaction v10.1.3

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

Installation 🤖

Install the nestjs-transaction package with:

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
@Injectable()
export class YourService extends TransactionService {
    public constructor(
        @InjectRepository(YourRepository) private repository: Repository<Entity>,
        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
import { DataSource } from 'typeorm';

@Controller()
export class SomeController {
    constructor(
        private readonly yourService: YourService,
        private dataSource: DataSource
    ) {}

    async method(payload: SomePayload): Promise<SomeResponse> {
        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

export const transactionConfig = TransactionModule.forRoot([MailService, I18nService, StorageService, DataSource]);

License 📝

This project is licensed under the MIT License

10.1.0

11 months ago

10.1.1

10 months ago

10.0.2

1 year ago

10.1.2

10 months ago

10.0.3

1 year ago

10.1.3

10 months ago

10.0.4

1 year ago

10.0.1

1 year ago

10.0.0

1 year ago

8.0.0

1 year ago

1.0.10

2 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago