question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support transaction per request with TypeORM

See original GitHub issue

[x] Feature request

What is the motivation / use case for changing the behavior?

We would like to make all TypeORM database operations during a certain request/endpoint transactional. Based on https://github.com/typeorm/typeorm/issues/404#issuecomment-295855913 we could create a transactional entity manager and pass it around to all our services, but that doesn’t feel very nestjs like. More recently @pleerock suggested the use of scoped services in https://github.com/typeorm/typeorm/issues/1895#issuecomment-380556498 which would enable a transaction-per-request and use the same transaction manager instance across all services without having to pass it around. A concrete example is in this doc.

Are scoped services something that the nestjs team has thought about implementing?

Is there a better way of handling TypeORM transactions in nestjs? Keeping in mind that custom repositories should not be singleton services and it is likely transaction decorators will be removed from TypeORM core.

For example:

UserController.ts

@Controller("/user")
export class UserController {
    constructor(
        private userService: UserService,
    ) {}

    @Post("/")
    @Transaction()
    async createNewUser(@Body() body: UserDTO) {        
        return await this.userService.createNewUser(body);
    }
}

UserService.ts

@Component()
export class UserService {
    constructor(private userRepository: UserRepository) {}

    async createNewUser(userDTO: UserDTO): Promise<User> {
        const user = new User();
        user.username = userDTO.username;
        ...

        const saved = await this.userRepository.save(user);

        await this.userRepository
            .createQueryBuilder()
            .relation(User, "departments")
            .of(saved)
            .add(userDTO.departmentIds);

        return saved;
    }
}

Where the dependency injected userRepository automagically is using the transactional entity manager under the hood.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:15
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
odavidcommented, Sep 24, 2018

Hi All, I’ve created a small project that adds a Spring Style Transactional Decorator and can control propagation. See https://github.com/odavid/typeorm-transactional-cls-hooked

Hope this helps, Ohad

7reactions
kamilmysliwieccommented, Apr 25, 2018

We’ll get back to this feature once async-hooks functionality moves into stable API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Per-Request Database Transactions with NestJS and TypeORM
NestJS provides a very powerful dependency injection mechanism that allows us to create a new transaction, scoped by request and then allow for ......
Read more >
Handling Transactions in TypeORM and Nest.js With Ease
Aritcle shows how to work with database transactions in TypeORM and Nest.js in a simple and convenient way.
Read more >
Build a Reliable Node API with TypeORM using Transactions
Using transactions with TypeORM. Fortunately, TypeORM is a pretty complete framework, which also supports transactions. Following the official documentation, ...
Read more >
nestjs / TypeOrm database transaction - Stack Overflow
js, but you can create an instance of TransactionManager (for example, in a nest middleware) and store it per each request context. You...
Read more >
Transactions - typeorm - GitBook
The most important restriction when working in a transaction is to ALWAYS use the provided instance of entity manager - transactionalEntityManager in this ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found