0
0
NestJSframework~10 mins

Prisma Client usage in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Prisma Client usage
Import PrismaClient
Create PrismaClient instance
Call Prisma methods (e.g., findMany)
Receive data from DB
Use data in app (e.g., return response)
Disconnect PrismaClient when done
This flow shows how to import, create, use, and disconnect Prisma Client in a NestJS app to interact with the database.
Execution Sample
NestJS
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function getUsers() {
  const users = await prisma.user.findMany();
  await prisma.$disconnect();
  return users;
}
This code creates a PrismaClient instance, fetches all users from the database asynchronously, disconnects, and returns the data.
Execution Table
StepActionCode LineResult/State
1Import PrismaClient classimport { PrismaClient } from '@prisma/client';PrismaClient class available
2Create PrismaClient instanceconst prisma = new PrismaClient();prisma object created, connected to DB
3Fetch users from DBconst users = await prisma.user.findMany();Query sent to DB, waiting for response
4Receive data from DB(await completes)Array of user objects received into users
5Return data to callerreturn users;Users data ready to use or send in response
6Disconnect PrismaClientawait prisma.$disconnect();Connection to DB closed
7EndFunction completesNo open DB connections
💡 Function ends after data is fetched and PrismaClient disconnects to free resources
Variable Tracker
VariableStartAfter Step 2After Step 4Final
prismaundefinedPrismaClient instancePrismaClient instanceDisconnected PrismaClient
usersundefinedundefinedArray of user objectsArray of user objects
Key Moments - 3 Insights
Why do we create a new PrismaClient instance instead of using PrismaClient directly?
Because PrismaClient is a class, we must create an instance to connect to the database and run queries. See Step 2 in execution_table where prisma is created.
What happens if we don't disconnect PrismaClient after queries?
The database connection stays open, which can cause resource leaks. Step 6 shows disconnecting to close the connection properly.
Why is 'await' used before prisma.user.findMany()?
Because database queries are asynchronous, 'await' waits for the data before continuing. Step 3 shows the query sent and waiting for response.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'prisma' after Step 2?
AUndefined
BPrismaClient instance connected to DB
CDisconnected PrismaClient
DArray of user objects
💡 Hint
Check the 'Result/State' column for Step 2 in execution_table
At which step does the app receive the user data from the database?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for when 'Array of user objects received' appears in execution_table
If we remove 'await' before prisma.user.findMany(), what changes in the execution?
APrismaClient disconnects earlier
BThe database query runs synchronously
CThe function returns a Promise instead of user data immediately
DNo change in behavior
💡 Hint
Consider the asynchronous nature shown in Step 3 of execution_table
Concept Snapshot
Prisma Client usage in NestJS:
- Import PrismaClient from '@prisma/client'
- Create an instance: const prisma = new PrismaClient();
- Use async calls like prisma.user.findMany() with await
- Disconnect with prisma.$disconnect() to free resources
- Always handle async properly to get DB data
Full Transcript
This visual execution trace shows how to use Prisma Client in a NestJS app. First, we import the PrismaClient class. Then, we create an instance which connects to the database. Next, we call an async method like prisma.user.findMany() to fetch data. We await the result to get the user array. After using the data, we disconnect the PrismaClient to close the database connection. Variables like 'prisma' hold the client instance, and 'users' hold the fetched data. Key points include creating the instance before queries, awaiting async calls, and disconnecting after use to avoid resource leaks.