DEV Community

Mehretab Hailegebriel
Mehretab Hailegebriel

Posted on

Build a CRUD API: Hands-on with NestJS, Prisma & Docker

This project will guide you through building a simple CRUD API. We'll use NestJs, Prisma ORM, and Docker to create a solid foundation for your understanding. Join in, whether you're new to coding or want to refresh your skills!

Before we start, ensure you've got:

  • Node.js and npm (or yarn): If not, download them from their official sources.
  • Docker: Install it according to your operating system.
  • Nest CLI: Globally install it using npm install -g @nestjs/cli.

1. Setting Up a New NestJS Project:

  • Open your terminal and navigate to your preferred project directory.
  • Execute nest new my-app in the terminal and select your preferred package manager when prompted.

2. Set Up Docker for PostgreSQL:

  • Inside your project's main directory, create a file named docker-compose.yaml. This file tells Docker how to set up our development environment.
  • Inside the file, define a service called postgres:
services:
  postgres:
    image: postgres:latest
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    networks:
      - network
    restart: always

networks:
  network:
Enter fullscreen mode Exit fullscreen mode
  • Create a .env file in the same directory as your Docker Compose file.
  • Define your environment variables in the .env file like this:
POSTGRES_USER=your_username
POSTGRES_PASSWORD=your_secret_password
POSTGRES_DB=your_database_name
Enter fullscreen mode Exit fullscreen mode
  • Replace your_username, your_secret_password, and your_database_name with something you can remember (but keep it secure!).

This way, Docker Compose will read the values from the .env file and use them to configure the PostgreSQL container.

3. Start the PostgreSQL Service:

  • Go back to your project's main directory and type docker-compose up -d in your terminal. This command runs the postgres service, creating a dedicated database container in the background.

4. Prisma, Our Database ORM:

  • Install Prisma and its client, (here using npm):
npm install --save-dev prisma@latest
npm install @prisma/client
Enter fullscreen mode Exit fullscreen mode

What sets this ORM apart from the others?

Prisma stands out with its unique schema approach, combining database and application models.

In traditional development, you typically define your database schema separately from your application's code. However, Prisma streamlines this process by allowing you to define your data models in a single place using its schema definition language (SDL).

With Prisma, you create a schema that describes your data structures, including entities, relationships, and constraints. This schema serves as the blueprint for both your application's data model and the underlying database schema.

Personally, it's my go to ORM when working on Node.Js projects. Though do not let your tools dictate your requirements, should be the other way around.

To know more about Prisma...

5. Initialize Prisma:

  • Run npx prisma init in your project directory. This command does two things:
    • Creates a schema.prisma file where you'll define the structure of your data (like tables in a traditional database).
    • Creates a .env file to store environment variables, which includes the connection URL for the database. In our case we already created our .env file so, it’ll append the database url variable.

6. Configure the Connection (Part 1):

  • Open your .env file and add the following line, replacing the placeholders with your actual values:
DATABASE_URL="postgresql://POSTGRES_USER:POSTGRES_PASSWORD@localhost:5432/POSTGRES_DB?schema=public"
Enter fullscreen mode Exit fullscreen mode

Make sure these values match what you used in your docker-compose.yaml file.

In our case, we've already set these values in our .env file, so simply replace the placeholders with the corresponding environment variables you've defined.

POSTGRES_USER=your_username
POSTGRES_PASSWORD=your_secret_password
POSTGRES_DB=your_database_name
Enter fullscreen mode Exit fullscreen mode

Working with NestJS and Prisma (Sneak Peek):

We'll dive deeper into working with NestJS and Prisma in the next post, but here's a quick glimpse to test if you configured prisma correctly:

  • Edit your schema.prisma file to define your data models. For example:
model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}
Enter fullscreen mode Exit fullscreen mode
  • Run npx prisma generate to generate the types of your models, allowing you to use them in your NestJS code.
  • Run npx prisma migrate dev to generate and migrate your data models to the database.

Stay tuned for Part 2!

In the next post, we'll explore the full steps of working with NestJS and Prisma, enabling you to create, read, update, and delete data in your database. We'll also delve into generating migrations and other essential aspects.

Feel free to leave any questions you have in the comments below, and share your experiences with this awesome tech stack!

Top comments (0)