DEV Community

Cover image for TypeScript CRUD Rest API, using Nest.js, TypeORM, Postgres, Docker and Docker Compose

TypeScript CRUD Rest API, using Nest.js, TypeORM, Postgres, Docker and Docker Compose

Francesco Ciulla on March 26, 2023

Let's create a CRUD Rest API in Typescript, using: NestJS (NodeJS framework) TypeORM (ORM: Object Relational Mapper) Postgres (relational databas...
Collapse
 
vladyslavshapoval profile image
Vladyslav Shapoval

Some crucial points are missing, like DTOs to validate incoming requests and transform responses.
And the general approach is far from a perfect one.
If you're a novice, then this article might be helpful, yet it doesn't follow best practices.
For instance, you shouldn't have any business logic inside controllers: you should get a user and check if it exists inside a service.
Next, better to have a data service to work with database. A service shouldn't know anything about database and ORM you use. All those things should be in a data service

Collapse
 
jakeroid profile image
Ivan Karabadzhak

Agree

However, for newbies this article should be okay

Collapse
 
francescoxx profile image
Francesco Ciulla

that's the point of the article

Collapse
 
francescoxx profile image
Francesco Ciulla

yes this is not intended to be a production ready code. thanks for the feedback

Collapse
 
ozifirebrand profile image
Ozifirebrand

This article is awesome for newbies who just want to bootstrap an app real fast. Thank you for sharing your thoughts too.

Collapse
 
samchon profile image
Jeongho Nam

What about adapting nestia?

With it, you can use pure TypeScript interface type as DTO, and also can generate advanced SDK (something like tRPC) and swagger than ever.

Collapse
 
francescoxx profile image
Francesco Ciulla

the goal of this artricle is to focus on the basics for beginners

Collapse
 
langstra profile image
Wybren Kortstra

Nice article, thanks for your help. May I ask if you've considered using MikroORM instead of TypeORM. I have used both and found MikroORM to work a lot better, but I am interested in your opinion.

Collapse
 
vladyslavshapoval profile image
Vladyslav Shapoval

I haven't heard about MikroORM, yet personally I prefer Prisma. You needn't create classes for your tables. It generates classes/types automatically. It's interesting that all auto generated stuff is located in node_modules, so that your sources don't have them. I strongly recommend to try it out

Collapse
 
francescoxx profile image
Francesco Ciulla

never heard fo that

Collapse
 
langstra profile image
Wybren Kortstra

It is a typescript ORM, like TypeORM, but with far fewer issues, more active development, better support and overall just better in my opinion. However, I think with the current state of typescript orm's you should also have a look at Prisma. In general I believe that TypeORM should not be used anymore.

Thread Thread
 
phuoctrungppt profile image
Trung Phan

why don't use TypeORM? Please help me clarify this

Thread Thread
 
langstra profile image
Wybren Kortstra

In my experience MikroORM has far fewer bugs. The support by the maintainer is very good, the maintainer is active in development, responds quickly to problems. Actually just the same reasons as I already stated.

Collapse
 
gseriche profile image
Gonzalo Seriche Vega

I'm a DevOps trying to understand and make life easy by creating some API. This tutorial helps me create a single API and understand the code for the container deployment in a local env.
Thanks for the guide.

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome

Collapse
 
marumaki profile image
Gen 2 SSI

I found your article very helpful. Can you make a real world example using nestjs + postgres + typeorm? I really need to dive deep into the ioc mechanism of nestjs. Really good article! Congratulates!

Collapse
 
francescoxx profile image
Francesco Ciulla

Thank you. here are the examples

Manage products, categories, orders, and inventory in an e-commerce platform.
User management and blog post CRUD operations in a blogging platform.
User profiles, friends/followers system, and messaging in a social media app.
Task and project management with real-time notifications in a task management tool.

Collapse
 
franciscogilt profile image
Francisco Gil

Francesco, I found this article very helpful and well structured. Thank you.

Collapse
 
francescoxx profile image
Francesco Ciulla

thank you Francisco

Collapse
 
shoban12 profile image
Mude Shoban Babu

thank you Francisco, very detailed explaination and code. thanks a lot.

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome Shoban.