An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
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, well 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
Then we create the docker.env
file to store environment variables:
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 TypeORMpg
: 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:
Beside the MessageModule
, you can see I configured the schema validation with Joi
and the databases module and import them all to our root module ( app )
Entity
If you know about databases, youll probably know about tables, in TypeORM, the most important concept about it is Entity, it maps to a database table:
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
Lets 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
With TypeORMs 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 tablegetMessageById
: we can use the find method above, but I suggest the findOne method that only return the first record that match our criteriacreateMessage
: 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
I used Postman to create new message, it works fine
Conclusion
In this article, weve 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 isnt clear
Last Words
Although my content is free for everyone, but if you find this article helpful, you can buy me a coffee here
Original Link: https://dev.to/leduc1901/set-up-a-postgresql-database-and-connect-to-nestjs-with-typeorm-35np
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To