Creating a TypeScript NodeJS Express App using TypeORM and MySQL – Part2

This tutorial is the second part of ‘Creating a TypeScript NodeJS Express App using TypeORM and MySQL’.

The first part is accessible in below link. Please read first part to understand how to set up the project and install the dependencies.

Creating a TypeScript NodeJS Express App using TypeORM and MySQL – Part1

The seed project can be downloaded here.

There are other posts which explains more advanced topics like cascade save and inner joins.  Please refer below links for that.

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

TypeORM is an Object Relational Mapper (ORM) for Node.js written in TypeScript that can be used with TypeScript or JavaScript (ES5, ES6, ES7). It support latest JavaScript features and provide features that help you to develop any kind of applications that use database – from small applications with a few tables to large scale enterprise applications.

TypeORM uses the Data Mapper pattern, unlike all other JavaScript ORMs that currently exist, which means you can write loosely coupled, scalable, maintainable applications with fewer problems.

Install TypeORM package and dependencies

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.

Install reflect-metadata shim and import it somewhere in the global place of your app (for example in app.ts):

Install node typings

Install MySQL database driver

TypeScript configuration
Also make sure you are using version 2.1 or greater of the TypeScript compiler, and you have enabled the following settings in tsconfig.json:

Entities

In TypeORM, tables are created from Entities. Entity is your model decorated by an @Entity decorator. You can get entities from the database and insert/update/remove them from there.

You can make any property of your model a column by using a @Column decorator.
Use @PrimaryColumn decorator, to make a column a primary.
@PrimaryGeneratedColumn decorator specifies  auto-increment primary column.

Add a new TypeScript file  ’employee-entity.ts’ in entities folder and add below code in it.

Creating connection to database

Add a new file ‘app-config.ts’ in src/common folder and add below code in it.

app.ts

Edit app.ts and replace code with below code.

employee-repository.ts

Add a new TypeScript file ’employee-repository.ts’ in src/repository folder and add below code in it.

employee-controller.ts

Now edit employee-controller.ts in src/controllers folder. Below are the contents of the file.

Project Structure

The project structure is shown below.

 

Run and Test the application

Go to ‘dist’ folder and run the application again.
Let’s use ‘Postman’ for testing the application. Postman is REST client which can be used to test REST APIs. Its simple and easy to install. You can get it from here.

Open Postman and add a new POST request for ‘localhost:3000/SaveEmployee’.
Add a new key-value entry (Content-Type , application/json) in Headers section.

 

In body add below json content. Select row check box.

 

Click on send button and if everything goes well you should receive the response from node app

 

Below is the output in command window.

 

 

Also ’employee’ table is created automatically in ‘hivetestdb’ database when the request is executed.

 

Now let’s add a get request for ‘GetAllEmployees’.

 

 

You can download the source code from here.

There are other posts which explains more advanced topics like cascade save and inner joins.  Please refer below links for that.

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

9 comments on “Creating a TypeScript NodeJS Express App using TypeORM and MySQL – Part2”:

  1. Hello ,
    nice post, but i wonder how to fix routing, i tried to make routing in a separated directory “routes” but it wont work, also i tried to setup gulp so i stop droing tsc and node app.js command but i wasnt able to make it work. can you please help me with that?

  2. no man i dont know what happen i get the following error now (node:23092) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): RepositoryNotFoundError: No repository for “EmployeeEntity” was found. Looks like th
    is entity is not registered in current “default” connection?
    (node:23092) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process
    with a non-zero exit code.
    its seems like the entities are not registred

  3. I am getting an error while trying to access to DB. My error message is “No repository for “EmployeeEntity” was found”. Can you please solve me or give me any reference to solve this issue.

Leave a Reply

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