odavid / typeorm-transactional-cls-hooked

A Transactional Method Decorator for typeorm that uses cls-hooked to handle and propagate transactions between different repositories and service methods. Inpired by Spring Trasnactional Annotation and Sequelize CLS
MIT License
524 stars 86 forks source link

use @Transactional() with @Res of nestjs transaction won't close #79

Closed ymchun closed 3 years ago

ymchun commented 3 years ago

Hello everyone,

I'm using @Transactional() decorator in controller methods but with problem when using @Res decorator to inject express's response object into the method.

I am serving raw file content with some controller method so that I inject express response object and pipe the file read stream to client.

Before that, I update my file entity but the request stuck at typeorm repository save method and the transaction won't close.

The workaround for me is remove @Transactional() from the controller method.

The call is something like the following:

// request comes in
//  |
//  V
// controller method getFile
@Get(':id')
@Transactional() // originally has @Transactional(), but removed
public async getFile(@Param('id') fileId: string, @Res() res: Response): Promise<void> {
    // update file entity
    await this.fileService.update(/* some params */);
    // get file readable stream
    // and then
    stream.pipe(res);
}
//  |
//  V
// file service update method (has @Transactional())
@Injectable()
export class FileService {
    @Transactional()
    public async update(file: FileEntity): Promise<FileEntity> {
        // update file
        return this.fileRepo.save( // stuck at here if add @Transactional() decorator to controller method
            this.fileRepo.create({
                ...file,
                updatedAt: new Date(),
            }),
        );
    }
}
nestjs: v7.6.5
typeorm-transactional-cls-hooked: v0.1.19
ymchun commented 3 years ago

seems like not related to typeorm, nestjs nor this library. I’ll further investigate, closing this issue.