DEV Community

Cover image for Set up a PostgreSQL database and connect to NestJS with TypeORM
Duc Le
Duc Le

Posted on

Set up a PostgreSQL database and connect to NestJS with TypeORM

Introduction

The most important thing when learning backend development is how to store the data. In this article, we look into how to create a database with PostgreSQL and connect it to NestJS. To manage a database easier, we’ll use an Object-relational mapping (ORM) tool called TypeORM.

Create the database

PostgreSQL, often simply “Postgres”, is an object-relational database management system. To quickly create a PostgreSQL database, we will use Docker ( make sure you installed Docker )

First, we create the docker-compose.yml file with these configurations

Image description

Then we create the docker.env file to store environment variables:

Image description

Okay, so now we have Docker and PostgreSQL, the next thing we want to do is to connect the database with NestJS

Connecting database

Before connecting NestJS app with the database, we need to install some dependencies:

  • @nestjs/typeorm typeorm : 2 libraries about TypeORM
  • pg : PostgreSQL client
  • @hapi/joi @types/hapi__joi : schema validation

Then, we need to create .env file with more variables about our database and the database.module.ts to get all of the env and config TypeORM:

Image description

Image description

Image description

Beside the MessageModule , you can see I configured the schema validation with Joi and the database’s module and import them all to our root module ( app )

Entity

If you know about databases, you’ll probably know about tables, in TypeORM, the most important concept about it is Entity, it maps to a database table:

Image description

I created a simple entity called Message , you can think about it like a message table with ID is its primary key and another column is content

Let’s write some APIs

Enough of the setup, we have connected NestJS with the database and create a table. Next thing we want to do is creating the controller and the service

Image description

Image description

Image description

With TypeORM’s repository, I created some RESTful APIs with simple methods:

  • getAllMessages : I used the find method, you can pass a lot of options to it, if not, It will get all of the records in the table
  • getMessageById: we can use the find method above, but I suggest the findOne method that only return the first record that match our criteria
  • createMessage: a simple create method to create new message

Testing

First, we need to start Docker with docker-compose up

Then, we start our NestJS server with yarn start:dev

Image description

I used Postman to create new message, it works fine

Conclusion

In this article, we’ve gone through the basics of connecting our NestJS application with a PostgreSQL database and used TypeORM for easily managing queries.

You can find the source code here if the article isn’t clear

Last Words

Although my content is free for everyone, but if you find this article helpful, you can buy me a coffee here

Top comments (0)