0
0
NestJSframework~30 mins

Prisma Client usage in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Prisma Client usage in NestJS
📖 Scenario: You are building a simple NestJS service to manage a list of books in a database. You will use Prisma Client to interact with the database.
🎯 Goal: Create a NestJS service that uses Prisma Client to fetch all books from the database.
📋 What You'll Learn
Create a Prisma Client instance
Configure Prisma Client in the service
Write a method to fetch all books using Prisma Client
Export the service class properly
💡 Why This Matters
🌍 Real World
This project shows how to use Prisma Client in a NestJS backend to interact with a database, a common task in building APIs.
💼 Career
Understanding Prisma Client usage in NestJS is valuable for backend developers working with modern Node.js frameworks and databases.
Progress0 / 4 steps
1
Create Prisma Client instance
Import PrismaClient from @prisma/client and create a constant called prisma that is a new instance of PrismaClient.
NestJS
Need a hint?

Use import { PrismaClient } from '@prisma/client' and then const prisma = new PrismaClient().

2
Create NestJS service class
Create a class called BooksService and add a constructor that assigns the prisma instance to a private readonly property called prisma.
NestJS
Need a hint?

Define class BooksService with a constructor() that sets this.prisma = prisma.

3
Add method to fetch all books
Inside the BooksService class, add an async method called getAllBooks that returns the result of this.prisma.book.findMany().
NestJS
Need a hint?

Write async getAllBooks() method that returns this.prisma.book.findMany().

4
Export the service class
Make sure the BooksService class is exported using export class BooksService so it can be used in other parts of the NestJS app.
NestJS
Need a hint?

Use export class BooksService at the start of the class definition.