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

This is the second part of the tutorial on developing a NodeJS application which uses Express, TypeORM and MySQL Server for data persistence.

The benefit of using TypeORM for the programmer is the ability to focus on the business logic and worry about persistence only as a secondary problem.

You can download the source code from here.

In the previous part we successfully created tables which are having relationships. In this tutorial let us focus on how to persist data in those tables. You have to install a few additional packages. Go to the root directory of the application where package.json file exists and run below command from powershell or command prompt.

add below import  in app.ts

add below lines of code in app.ts after creating the express app.

Project Structure
The project structure is as shown below.

Add a new class file base-response.ts in src folder. Below are the contents.

Add a new class file user-repository.ts in src/repositories folder and add below contents.

Create a new class file user-controller.ts in src/controllers folder and add below code in it.

Add new routes to the app.ts file.

That’s it. We have added a repository class , a controller class and added routes for User related operations. Now let us test this api. I am going to use Postman for this. First compile the application by running tsc command from the root folder. Then change directory to dist and run “node app.js ” command.

Next open postman and create a new POST request. The url is “localhost:3000/SaveUser”. Go to “Headers” section and set “Content-Type” as “application.json”.

Next go to Body tab and chose raw and JSON as  data format option. Click on send button

If all goes well you should see a success message in the results tab.

And you should also see a record in MySQL table.

If you want to update one record then you can supply an Id which is greater than 0.

Below is the updated record in MySQL server.

Let us add a repository class for Product entity.

Next add a controller class for Product.

Let us save a product in DB. Below is the body of the JSON object that we need to pass.

Next we are going to add Repository and Controller classes for Order.

Create a new typescript file order-repository.ts and add below code in it.

Add a repository class order-details-repository.ts in src/repositories.

Create a new typescript file order-controller.ts in src/controllers folder and add below code in it.

Let us save an Order using Postman.

Here in the json data you can see that we are passing order details as an array.

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

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

3 comments on “TypeORM Express Application Using Repository Pattern – Part 2 – Persist Data into Tables”:

Leave a Reply

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