Set up a PostgreSQL database and connect to NestJS with TypeORM
Tags: webdev, javascript, node, typescript
Published: 2023-01-30 04:40:29

###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uu7p0alhe0krk8u7j2s6.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwtlu8e4f41jbb3pnw4w.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88wqvfxs22onvxstq9hh.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rz1nodxj36qo96tul4rz.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93kxjp6u2jiw7iaqgawj.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5aw879vs4dgfun3g8jfi.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3f7az02xla7wdjhbp7j4.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gum08rpsvz39wsgb9vx.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhhrmfvxlo5yrmb7vfh6.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/73o96r5g2vibbcpbbzgw.png)

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](https://github.com/leduc1901/nestjs-websocket) 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](https://www.buymeacoffee.com/kylele19)