{"action":"created","issue":{"url":"https://api.github.com/repos/nestjs/nest/issues/560","repository_url":"https://api.github.com/repos/nestjs/nest","labels_url":"https://api.github.com/repos/nestjs/nest/issues/560/labels{/name}","comments_url":"https://api.github.com/repos/nestjs/nest/issues/560/comments","events_url":"https://api.github.com/repos/nestjs/nest/issues/560/events","html_url":"https://github.com/nestjs/nest/issues/560","id":313682656,"number":560,"title":"Support transaction per request with TypeORM ","user":{"login":"shusson","id":2106638,"avatar_url":"https://avatars2.githubusercontent.com/u/2106638?v=4","gravatar_id":"","url":"https://api.github.com/users/shusson","html_url":"https://github.com/shusson","followers_url":"https://api.github.com/users/shusson/followers","following_url":"https://api.github.com/users/shusson/following{/other_user}","gists_url":"https://api.github.com/users/shusson/gists{/gist_id}","starred_url":"https://api.github.com/users/shusson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shusson/subscriptions","organizations_url":"https://api.github.com/users/shusson/orgs","repos_url":"https://api.github.com/users/shusson/repos","events_url":"https://api.github.com/users/shusson/events{/privacy}","received_events_url":"https://api.github.com/users/shusson/received_events","type":"User","site_admin":false},"labels":[{"id":851403279,"url":"https://api.github.com/repos/nestjs/nest/labels/scope:%20core","name":"scope: core","color":"cbbcf4","default":false},{"id":532263510,"url":"https://api.github.com/repos/nestjs/nest/labels/type:%20enhancement%20:wolf:","name":"type: enhancement :wolf:","color":"84b6eb","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-04-12T11:17:08Z","updated_at":"2018-04-23T07:45:52Z","closed_at":null,"author_association":"NONE","body":"<pre><code>\r\n[x] Feature request\r\n</code></pre>\r\n\r\n## What is the motivation / use case for changing the behavior?\r\nWe 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](https://github.com/typestack/typedi#using-multiple-containers-and-scoped-containers).\r\n\r\nAre `scoped services` something that the nestjs team has thought about implementing?\r\n\r\nIs there a better way of handling TypeORM transactions in nestjs? Keeping in mind that custom repositories [should not be singleton services](https://github.com/typeorm/typeorm/issues/404) and it is likely transaction decorators will be [removed from TypeORM core](https://github.com/typeorm/typeorm/issues/1895#issuecomment-380419156).\r\n\r\nFor example: \r\n\r\nUserController.ts\r\n```typescript\r\n@Controller(\"/user\")\r\nexport class UserController {\r\n    constructor(\r\n        private userService: UserService,\r\n    ) {}\r\n\r\n    @Post(\"/\")\r\n    @Transaction()\r\n    async createNewUser(@Body() body: UserDTO) {        \r\n        return await this.userService.createNewUser(body);\r\n    }\r\n}\r\n```\r\n\r\nUserService.ts\r\n```typescript\r\n@Component()\r\nexport class UserService {\r\n    constructor(private userRepository: UserRepository) {}\r\n\r\n    async createNewUser(userDTO: UserDTO): Promise<User> {\r\n        const user = new User();\r\n        user.username = userDTO.username;\r\n        ...\r\n\r\n        const saved = await this.userRepository.save(user);\r\n\r\n        await this.userRepository\r\n            .createQueryBuilder()\r\n            .relation(User, \"departments\")\r\n            .of(saved)\r\n            .add(userDTO.departmentIds);\r\n\r\n        return saved;\r\n    }\r\n}\r\n```\r\nWhere the dependency injected `userRepository` automagically is using the transactional entity manager under the hood.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/nestjs/nest/issues/comments/383484617","html_url":"https://github.com/nestjs/nest/issues/560#issuecomment-383484617","issue_url":"https://api.github.com/repos/nestjs/nest/issues/560","id":383484617,"user":{"login":"shusson","id":2106638,"avatar_url":"https://avatars2.githubusercontent.com/u/2106638?v=4","gravatar_id":"","url":"https://api.github.com/users/shusson","html_url":"https://github.com/shusson","followers_url":"https://api.github.com/users/shusson/followers","following_url":"https://api.github.com/users/shusson/following{/other_user}","gists_url":"https://api.github.com/users/shusson/gists{/gist_id}","starred_url":"https://api.github.com/users/shusson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shusson/subscriptions","organizations_url":"https://api.github.com/users/shusson/orgs","repos_url":"https://api.github.com/users/shusson/repos","events_url":"https://api.github.com/users/shusson/events{/privacy}","received_events_url":"https://api.github.com/users/shusson/received_events","type":"User","site_admin":false},"created_at":"2018-04-23T07:45:52Z","updated_at":"2018-04-23T07:45:52Z","author_association":"NONE","body":"Thanks for the update @kamilmysliwiec, `async-hooks` sounds like a promising way forward. "}}
{"actor":{"display_login":"shusson"}}