NestJS — How to use TypeOrm?

Rui Coelho
3 min readDec 12, 2020

TypeORM is definitely the most mature Object Relational Mapper (ORM) available in the node.js world. Since it’s written in TypeScript, it works pretty well with the Nest framework.

The reference database for this article will be PostgreSQL.

Getting Started

npm install --save @nestjs/typeorm typeorm pg

Deploy Database

To deploy our database we will use docker. To do it we simply need to run:

docker pull postgres
docker run --name [container_name] -e POSTGRES_PASSWORD=[database_password] -p 5432:5432 -d postgres

After that we just need to login and create a database:

docker exec -it postgresdb psql -U postgres
create database nestjs;

Now our database is ready to use.

Configuration Service

To inject our credentials from environment variables we have to create a configuration service.

npm i --save @nestjs/config
mkdir src/config
touch src/config/configuration.ts

We have now our dependencies installed and our configuration file created.

Let’s now import our credentials from environment variables:

Configuration file with environment variables

On our .env file placed on our root folder let’s add:

POSTGRES_URL = postgresql://postgres:<Your_Password>@localhost:5432/nestjs
NODE_ENV = development
NPM_CONFIG_PRODUCTION = false
PORT = 3000

Configure Project Using Environment Variables

You can find more detail about this in my other article.

Configuration Service on main.ts

Create Database Connection

First we need to inject our config service using ConfigModule and after that we can inject our TypeOrm config.

Injection of ConfigModule and TypeOrmModule

After this process our NestJS App is now able to connect to our database.

Create Entities

In order to use TypeOrm we need to create entities:

mkdir src/database/entities
touch src/database/entities/items.entity.ts

With our file created let’s define our entity:

Now let’s add it to our app.module.ts :

Item entity on app.module.ts

We simply added our entity on entites:[Items] .

Work with Entities

To interact with our entity we need to create a new service and a new controller. I will use a module to encapsulate our service and controller:

nest g module items
nest g controller items
nest g service items

Nest scaffold mechanism will automatically import everything.

Add our entity to items module

Let’s add Items Entity to Items Module. Note: I already added the export line to export our ItemsService.

Validate content from input

To validate content I like to use class validator:

npm install class-validator --save

After install let’s add it to our controller and call items service:

This will validate if body content checks out with our dto, if the content checks it will call our service otherwise return an error message.

Item creation service

On our service we need no inject our entity on the constructor and call the entity on the service method:

Now we can insert items on the database:

Insert successfully done
Insert failed

If something isn’t correct on our body the server will return the predefined error message.

List all items

Now we can list all items to check if everything is working. First we need to create our get endpoint and request info from Items Service:

Get endpoint

Let’s perform an http request:

Get request results

Conclusions

The integration of TypeOrm with the NestJS framework is very simple and fast and allows the configuration of several relational databases. The documentation on the official webpage is quite good and complete so you can find more information about possible connections.

Source Code

The code for the entire project is available on my github.

--

--