<!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG-spv-->{"id":2023697,"date":"2021-11-01T01:15:00","date_gmt":"2021-10-31T23:15:00","guid":{"rendered":"http:\/\/nhub.news\/?p=2023697"},"modified":"2021-11-01T06:03:37","modified_gmt":"2021-11-01T04:03:37","slug":"how-to-perform-a-nestjs-typeorm-transaction-using-queryrunner","status":"publish","type":"post","link":"http:\/\/nhub.news\/ru\/2021\/11\/how-to-perform-a-nestjs-typeorm-transaction-using-queryrunner\/","title":{"rendered":"How to Perform a NestJS TypeORM Transaction Using QueryRunner?"},"content":{"rendered":"<p style=\"text-align: justify;\"><b>In this post, we will look at how to perform a NestJS TypeORM Transaction using the TypeORM QueryRunner class with examples. This is also the recommended &#8230;<\/b><br \/>\nJoin the DZone community and get the full member experience. In this post, we will look at how to perform a NestJS TypeORM Transaction using the TypeORM QueryRunner class. This is also the recommended way of handling transactions in NestJS. If you are new to using TypeORM with NestJS, refer to this detailed post about NestJS TypeORM Integration. Transactions are one of the most important concepts while dealing with databases. Basically, a transaction is a unit of work. We should treat it as a whole. Either it happens completely or it does not happen. For example, you can think of transferring money from one account to another. At face value, this process involves multiple steps such as withdrawing money from the first account and then transferring it into the recipient\u2019s account. The transfer fails if even one of the steps fails. Hence, we should handle such operations in the form of transactions. A transaction must have the below properties. They are commonly known as ACID properties. The TypeORM package comes with the Connection class. We will inject this class into our LibraryService example. See below code: The Connection class resides in the typeorm package. The below import statement will be needed. The Connection object does not represent a single database connection. It basically points to a pool of connections. To refer to a single database collection, we use the QueryRunner class. Basically, every instance of the QueryRunner is a connection. We can now use the Connection object to create a transaction. See the below example: Basically, we use an instance of QueryRunner to establish a connection and start a transaction. Then, we save all the books (in this case 2 books) as part of the same transaction. If something goes wrong, we rollback everything. Else, we commit the transaction and release the connection. With this, we are done with NestJS TypeORM Transaction using QueryRunner. You can find the code for the same on Github. If you have any comments or queries about the post, please feel free to write in the comments section below. Published at DZone with permission of Saurabh Dashora. See the original article here. Opinions expressed by DZone contributors are their own.<\/p>\n<script>jQuery(function(){jQuery(\".vc_icon_element-icon\").css(\"top\", \"0px\");});<\/script><script>jQuery(function(){jQuery(\"#td_post_ranks\").css(\"height\", \"10px\");});<\/script><script>jQuery(function(){jQuery(\".td-post-content\").find(\"p\").find(\"img\").hide();});<\/script>","protected":false},"excerpt":{"rendered":"<p>In this post, we will look at how to perform a NestJS TypeORM Transaction using the TypeORM QueryRunner class with examples. This is also the recommended &#8230; Join the DZone community and get the full member experience. In this post, we will look at how to perform a NestJS TypeORM Transaction using the TypeORM QueryRunner [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2023696,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/posts\/2023697"}],"collection":[{"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/comments?post=2023697"}],"version-history":[{"count":1,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/posts\/2023697\/revisions"}],"predecessor-version":[{"id":2023698,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/posts\/2023697\/revisions\/2023698"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/media\/2023696"}],"wp:attachment":[{"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/media?parent=2023697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/categories?post=2023697"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nhub.news\/ru\/wp-json\/wp\/v2\/tags?post=2023697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}