Building your first Rest API with Nestjs and TypeORM and test it with Postman
A REST API is a web API that uses HTTP requests to allow clients to access and manipulate resources, identified by URIs, using standard HTTP methods. It’s designed to be stateless and is commonly used to expose data and functionality from a server-side application to client-side applications or third-party developers.
NestJS is a Node.js framework for building scalable and efficient server-side applications, with built-in features and modules for dependency injection, middleware, routing, and more, as well as support for multiple databases and testing tools.
Let’s get started
Firstly you have to install the Nestjs CLI globally
and then create a new project
Before creating the project, nestjs will ask which package manager you use I choose npm but you have the choice between npm, yarn, or pnpm.
So let’s see what Nestjs has generated for us.
- node_modules: a folder for our dependencies
- test: is where nestjs suggest you put your end-to-end tests.
- boilerplate files: for package management, typescript configuration, and static code checking.
- src: a folder that contains several core files it’s where we will write our code.
Once the installation is complete you can run the following command to run your app
This command will run your app on the port indicated in the main.ts file. You can also run
This command will run your app and reload every time you edit a file.
Now our app is created, we know how to run it, let’s build. We want an app that handles users and basic crud operations. Those users should be stored in a database.
Database set up
We are going to use TypeORM and MySQL for storage. Nestjs provides tight integration with TypeORM out-of-the-box with the
@nestjs/typeorm package. We just have to install the packages.
and then connect the database by using the
@nestjs/typeormalongside the different database params (host, user name, user password, database name):
Now our database is connected to the project.
Nestjs manage resources, a resource is a folder or module that represents a part of your app’s domain. For each resource of your app, you have a controller, service, dto, and entity. In our case we want to manage users, so users are our resource.
To create a new resource we tap the command below by adding the name of the resource we are creating.
The cli will ask you two questions:
- What transport layer do you use?: choose REST API
- Would you like to generate CRUD entry points?: Yes
And then the following folder and files will be created.
Now we have all the boilerplate set up, we need to define the user entity, to do that we will edit the user.entity.ts file and use the decorator provide by typeOrm.
First, we need to add the
@Entity decorator before the class definition to tell typeOrm that the next class will be an entity and then for the attributes you have two decorators,
@PrimaryGeneratedColumn to create a primary and auto-increment key, and
@Column to create other table columns. To define column type, we have just to define it in the
@Columndecorator, in our case the three attributes are text, but you can use number, boolean, and so on.
When the user's resources have been generated for us, there had a folder called dto with two files
Dto is a class that shapes the data we want to receive while creating a new entry for our database. The
create-user.dto.ts will contain the attribute we want to receive to create a new user and if those attributes aren’t available the request won’t reach the service layer. In this case, we have only three attributes we want to receive so let’s update the
The service layer is the layer in which we implement our business logic, its also this layer that communicates with the layer that interacts with the database mostly called the Repository layer and the controller layer.
Typically, the service layer receives the data that has been sent by the user from the controller, the service layer applies the business logic on those data and then calls the repository layer to store it in the database.
When you generate a new resource through the cli, you have a service that is created with few methods.
We will edit this class. In the introduction above we say that the service layer needs to call the repository layer to interact with the database.
Since we are using TypeOrm, it provides a built-in class that comes with the method to handle the data related to a given entity.
Nestjs favor dependency injection to allow two different class to interact, dependency injection is an inversion of control (IoC) technique wherein you delegate the instantiation of dependencies to the IoC container, instead of doing it in your own code imperatively. Let’s edit the user service.
To inject the repository we have to do that in the constructor, by using the InjectRepository decorator, and declaring a private variable that has the type Repository provide by typeOrm with the entity we want to manage.
But every time we use an external dependency we need to import it, so that will be available in the service we wanted to use it. To import an external dependency we have to edit the module of the resource we are working with.
Once it’s done the userRepository variable will be available in every function of that class.
Now the repository is set up, we can use it to read, create, update, and delete the users.
We have converted all methods to async methods since the repository’s methods return Promises and we have to handle those promises.
Now we have set up the service, inject the repository and each method within the service communicates with the repository to store the data it receives, we can move to the next layer.
The controller layer is the layer in which we handle the user request and send data to the service layer. Each method in the controller will be converted to an endpoint with which the final user will interact.
We have used the cli to generate the user's resource, so we already have a controller ready to be used without any changes. Let’s see how it looks:
Since the controller has to interact with the service layer, we follow the same method as in the service layer, we inject the service class by using the dependency injection.
Each decorator before each method defines the method that will be used to call the endpoint.
The Body decorator in the update and create method are used to validate the request body. You can provide a type, here we provide the DTO class that we have created in the first part of the article to be sure that the data we receive is shaped as we want.
The decorator Param is used to getting the request’s params by defining the name of the param with its type.
Since the endpoint method is defined, and the request params and body are validated, we call the service method that will apply our business logic on it and then call the repository to interact with the database.
Testing the API
We will use postman to test our API.
Postman is a tool for building, testing and documenting APIs. It simplifies the process of making HTTP requests and inspecting responses, allows for easy collaboration among team members, and supports automated testing and integration with CI/CD pipelines.
Get the users list: to get the users list, we have to set the endpoint URL and the GET method.
Create a new user: to create a new user we use the same URL, but we change the method to POST, we also need to add a Content-Type attribute in the header of the request to indicate that the data being sent in the request body is in JSON format.
Once the content-type attribute is set up. we can edit the request body to send our data.
The request body is where we set the user’s data and as you can see we receive a 201 status which means the user has been successfully created and we also receive the user data with his id.
Delete a user: to delete a user we have to add his id to the URL and change the method to DELETE.
Edit a user: to edit a user we have to add his id to the URL, change the method to PATCH, and set the value we wanted to edit in the request body.
As we can see in the image above, the user’s email has been changed.
In conclusion, building a REST API with NestJS, TypeORM, and MySQL can be a powerful combination for creating robust and scalable web applications. With NestJS, you can take advantage of its modular architecture and dependency injection to build highly testable and maintainable code. TypeORM simplifies the process of working with databases by providing a rich set of tools and features for handling data access and manipulation. Postman provides a user-friendly interface to test your Rest API.
You can find the final code for this project here.
I hope this article has been helpful in guiding you through the process of building a REST API with these technologies.