Like other Node.js frameworks, Nest.js provides a comprehensive toolkit for building robust and scalable backend services. Nonetheless, it is important to understand how to implement the create, read, update, and delete (CRUD) operations in Nest.js efficiently—these are the most fundamental operations in the development of APIs.

Learn how to build a Nest.js CRUD REST API using TypeORM and a PostgreSQL database.

ElephantSQL website’s homepage

Getting Started With Nest.js

To get started, install the Nest.js command line tool:

Next, create a new project by running:

The CLI tool will prompt you to choose a package manager, choose the option that you find most preferable. We’ll usenpm, the Node package manager.

The CLI will scaffold a basic Nest.js project with all the required configuration files and initial dependencies required to run the application.

Create New PostgreSQL Instance button on ElaphantSQL cloud platform

Finally, navigate to the project directory and start the development server.

You can find this project’s code in itsGitHubrepository.

Create a PostgreSQL Database

This tutorial uses a cloud PostgreSQL instance, but you can set up a local PostgreSQL database instead. You caninstall PostgreSQL on Windows,on macOS, or on Linux.

To set up a cloud PostgreSQL instance:

Configure the Database Connection

In your project’s root directory, create a.envfile and paste the database connection URL as follows:

Now install these packages:

Next, go ahead and create a database module using the CLI tool.

Open thedatabase/database.module.tsfile and add the following database configuration code:

This database module handles the connection by configuring the TypeORM module with the required connection parameter, the database URL.

Additionally, it defines the User entity as part of the configuration that specifies the structure and properties of the data stored in the PostgreSQL database table.

At this stage, your code will probably throw an error because you haven’t created the users entity yet. You’ll do that in the following steps.

Update the app.module.ts File

Lastly, update the main application module to include the configuration for the database module.

Define a Users Module

The users module serves as a centralized component, responsible for encapsulating and managing the logic required to implement the API’s CRUD functionality.

Run this terminal command to create the API’s users module.

The CLI tool automatically updates theapp.module.tsfile to reflect the changes made, in addition to creating the user module. This ensures that the newly created module, users, is properly integrated into the application’s module configuration.

Create a User Entity

TypeORM is an Object-Relational Mapping (ORM) library that simplifies database interactions in applications that use TypeScript by mapping JavaScript objects to database tables.

By creating a User entity using TypeORM, you define the structure and properties of the user data in the PostgreSQL database.

In the users directory, create a newmodels/user.entity.tsand add the following code.

TheUserentity defines the structure of the user data stored in the database. In this case, that’s theidas the primary key column, and thenameandemailcolumns and their corresponding properties.

Create the CRUD API Service

Now, create the API service that will manage the logic for the CRUD operations by running the command below:

Open theuser-auth.service.tsfile and add this code:

This UsersService class defines various API methods dedicated to handling CRUD operations. These methods include fetching all users' data, finding a specific user using their ID number, creating a new user, updating an existing user, and a method for deleting a specific user’s data in the database.

Define a Controller for the API

Create a controller that will manage the API endpoints for the user-related operations.

Next, add the code below to theusers.controller.tsfile.

The controller manages API endpoints for user operations. It handles GET requests to retrieve all users, POST requests to create new users, PUT requests to update existing users, and DELETE requests to delete users.

By utilizing theUsersServiceand interacting with theUserentity, this controller provides a complete API for managing user-related operations on the data stored in the database.

Update the users.module.ts File

Lastly, update theusers.module.tsfile as shown below to ensure you incorporate theUser entityand the TypeORM module, which establishes the connection to the database.

Finally, go ahead, and spin up the development server to test the CRUD operations using Postman.

The server will start on port 3000, and you can send API requests to it athttp://localhost:3000/api/users.

Building Backend Applications With Nest.js

Whether you’re developing a simple REST API or a complex web app, Nest.js offers a comprehensive set of features and capabilities to construct a reliable and robust backend system.

Nest.js offers a more structured approach to project development than Express.js does. This ensures that you can confidently build, scale, and maintain complex applications, thanks to its organized and modular design pattern.