TypeORM Express Application Using Repository Pattern – Part 4 – How to do Cascade Save

In Part 2 of this series, we have seen how to persist data if we have a parent child relationship between tables. In this post we discuss about cascade save.

You can download the source code from here.

The tables we have used are Order and OrderDetails. Below is the code which persist Order and OrderDetails data into tables.

Here Order is saved first and then order details is saved and in order to save Order Details an explicit call to OrderDetailsRepo’s save method is required. There is another way to save OrderDetails along with Order data using cascade. In cascade save only one repo call is required to save Order data and OrderDetails is saved along with Order.

Modify OrderEntity and add { cascade: true } for order_details property

Let us add a new method for cascade save.

Here you can see we are not using OrderDetailsRepository for saving OrderDetails. It is saved along with Order data when ‘saveorder’ is called.

Below is the input json.

You can download the source code from here.

Below are the links to other posts in this series.

TypeORM Express Application Using Repository Pattern – Part 1 – Create Tables with Relationships

TypeORM Express Application Using Repository Pattern – Part 2 – Persist Data into Tables

TypeORM Express Application Using Repository Pattern – Part 3 – How to perform InnerJoin

One comment on “TypeORM Express Application Using Repository Pattern – Part 4 – How to do Cascade Save

Leave a Reply

Your email address will not be published. Required fields are marked *