📖 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
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
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
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
Hint
Use export class BooksService at the start of the class definition.
Practice
(1/5)
1. What is the main purpose of Prisma Client in a NestJS application?
easy
A. To interact with the database using simple JavaScript calls
B. To handle HTTP requests and routing
C. To manage application state in the frontend
D. To style components with CSS
Solution
Step 1: Understand Prisma Client's role
Prisma Client is a tool that helps you talk to your database easily using JavaScript.
Step 2: Compare with other options
Handling HTTP requests and routing, managing frontend state, and styling components relate to other parts of an app, not database interaction.
Final Answer:
To interact with the database using simple JavaScript calls -> Option A
Quick Check:
Prisma Client = database interaction [OK]
Hint: Prisma Client is for database queries, not UI or routing [OK]
Common Mistakes:
Confusing Prisma Client with HTTP routing
Thinking Prisma Client manages frontend state
Assuming Prisma Client styles components
2. Which of the following is the correct way to import and instantiate Prisma Client in a NestJS service?
easy
A. import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient();
B. const prisma = require('prisma-client');
C. import Prisma from 'prisma'; const prisma = Prisma();
D. import { Prisma } from '@nestjs/prisma'; const prisma = new Prisma();
Solution
Step 1: Check the official import syntax
The correct import is from '@prisma/client' and creating a new PrismaClient instance.
Step 2: Identify incorrect options
Using require('prisma-client'), importing from 'prisma', or using '@nestjs/prisma' with incorrect instantiation do not match Prisma Client usage.
Final Answer:
import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); -> Option A
Quick Check:
Correct import and instantiation = import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); [OK]
Hint: Always import PrismaClient from '@prisma/client' and instantiate with new [OK]
Common Mistakes:
Using require instead of import
Wrong package names like 'prisma-client'
Confusing NestJS modules with Prisma Client
3. Given this Prisma Client query in a NestJS service:
D. PrismaClient should not be instantiated directly
Solution
Step 1: Check async function usage
prisma.user.findMany() returns a Promise, so it must be awaited inside async functions.
Step 2: Identify missing await
The code misses 'await' before the call, so it returns a Promise instead of resolved data.
Final Answer:
Missing await before prisma.user.findMany() -> Option B
Quick Check:
Async DB calls need await [OK]
Hint: Await all Prisma async calls inside async functions [OK]
Common Mistakes:
Forgetting await on async Prisma calls
Thinking PrismaClient can't be instantiated
Assuming findMany() is invalid
5. In a NestJS app, you want to ensure Prisma Client connects once on app start and disconnects on shutdown. Which approach is best?
hard
A. Call prisma.$connect() before every query and prisma.$disconnect() after every query
B. Create a new PrismaClient instance inside every controller method
C. Create PrismaClient instance in a service, call prisma.$connect() in onModuleInit(), and prisma.$disconnect() in onModuleDestroy()
D. Use PrismaClient without calling connect or disconnect methods
Solution
Step 1: Understand PrismaClient lifecycle
PrismaClient should connect once when app starts and disconnect on shutdown to manage resources efficiently.
Step 2: Evaluate options
Creating a single PrismaClient instance in a service and using $connect() in onModuleInit() and $disconnect() in onModuleDestroy() leverages NestJS lifecycle hooks properly. Connecting/disconnecting per query, creating new instances per method, or skipping explicit management leads to overhead or resource leaks.
Final Answer:
Create PrismaClient instance in a service, call prisma.$connect() in onModuleInit(), and prisma.$disconnect() in onModuleDestroy() -> Option C
Quick Check:
Use lifecycle hooks for Prisma connection [OK]
Hint: Connect once on start, disconnect on shutdown using lifecycle hooks [OK]
Common Mistakes:
Creating PrismaClient multiple times
Connecting/disconnecting on every query
Ignoring connect/disconnect lifecycle
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.