Prisma is a modern database toolkit that helps NestJS apps talk to databases easily and safely. It generates code to access your database with simple commands.
Click to reveal answer
beginner
Which command initializes Prisma in a NestJS project?
You run npx prisma init to create the Prisma setup files like schema.prisma and environment configs.
Click to reveal answer
intermediate
How do you integrate Prisma Client into a NestJS service?
You create a PrismaService that extends PrismaClient and inject it into other services to use Prisma's database methods.
Click to reveal answer
intermediate
Why should PrismaService extend PrismaClient in NestJS?
Extending PrismaClient lets PrismaService use all database methods directly and manage connection lifecycle cleanly within NestJS.
Click to reveal answer
beginner
What is the purpose of the schema.prisma file?
It defines your database models and connection details. Prisma uses it to generate the client code for database access.
Click to reveal answer
Which command creates the Prisma client after defining models?
Anpx prisma generate
Bnpm run start
Cnpx prisma migrate
Dnpm install prisma
✗ Incorrect
npx prisma generate creates the Prisma client code based on your schema.
Where do you put your database connection URL for Prisma in NestJS?
A.env file
Bmain.ts
Cpackage.json
Dprisma.service.ts
✗ Incorrect
The .env file holds environment variables like the database URL for Prisma.
What does PrismaService usually extend in NestJS?
AController
BHttpService
CModule
DPrismaClient
✗ Incorrect
PrismaService extends PrismaClient to access database methods.
Which NestJS feature helps inject PrismaService into other parts of the app?
AMiddleware
BDependency Injection
CGuards
DInterceptors
✗ Incorrect
Dependency Injection allows PrismaService to be used in other services or controllers.
What file defines your database tables and relations in Prisma?
Amain.ts
Bapp.module.ts
Cschema.prisma
Dpackage.json
✗ Incorrect
schema.prisma defines models representing tables and relations.
Explain the steps to set up Prisma in a new NestJS project.
Think about installation, configuration, code generation, and integration.
You got /6 concepts.
Describe how PrismaService works inside a NestJS app and why it is useful.
Focus on PrismaService role and benefits in NestJS.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of PrismaService in a NestJS application?
easy
A. To provide a reusable database client instance for querying the database
B. To handle HTTP requests and responses
C. To define the database schema
D. To manage application configuration settings
Solution
Step 1: Understand PrismaService role
PrismaService wraps the Prisma Client to provide a single database connection instance across the app.
Step 2: Differentiate from other roles
It does not handle HTTP or define schema; those are handled by controllers and Prisma schema files respectively.
Final Answer:
To provide a reusable database client instance for querying the database -> Option A
Quick Check:
PrismaService = database client provider [OK]
Hint: PrismaService is your database helper in NestJS [OK]
Common Mistakes:
Confusing PrismaService with controllers
Thinking PrismaService defines schema
Assuming PrismaService manages config
2. Which of the following is the correct way to import and provide PrismaService in a NestJS module?
easy
A. import { PrismaService } from '@nestjs/common';
@Module({ controllers: [PrismaService] })
B. import { PrismaService } from './prisma.service';
@Module({ providers: [PrismaService], exports: [PrismaService] })
C. import PrismaService from 'prisma';
@Module({ providers: [PrismaService] })
D. import { PrismaService } from './prisma.service';
@Module({ imports: [PrismaService] })
Solution
Step 1: Check import source
PrismaService is a custom service, so it must be imported from its local file, not from '@nestjs/common' or external packages.
Step 2: Verify module metadata
It should be listed under providers and exports to be injectable and reusable in other modules.
Final Answer:
import { PrismaService } from './prisma.service';
@Module({ providers: [PrismaService], exports: [PrismaService] }) -> Option B
Quick Check:
Local import + providers and exports = correct setup [OK]
Hint: PrismaService is a provider, import locally and add to providers [OK]
Common Mistakes:
Importing PrismaService from wrong package
Adding PrismaService to controllers instead of providers
Using imports array instead of providers
3. Given this PrismaService method in NestJS, what will console.log(user) output if the user with ID 1 exists?
async getUserById(id: number) {
return this.prisma.user.findUnique({ where: { id } });
}
const user = await prismaService.getUserById(1);
console.log(user);
medium
A. A syntax error because findUnique is not a function
B. An array of all users in the database
C. An object containing the user's data with ID 1
D. null, because findUnique returns nothing
Solution
Step 1: Understand findUnique behavior
findUnique returns a single record matching the unique filter or null if none found.
Step 2: Given user with ID 1 exists
The method returns the user object with ID 1, so console.log prints that object.
Final Answer:
An object containing the user's data with ID 1 -> Option C
Quick Check:
findUnique returns single object or null [OK]
Hint: findUnique returns one object or null, not array [OK]
Common Mistakes:
Thinking findUnique returns an array
Assuming it always returns null
Confusing findUnique with findMany
4. You wrote this PrismaService method but get a runtime error: Cannot read property 'user' of undefined. What is the likely cause?
Hint: Initialize prisma client in constructor to avoid undefined errors [OK]
Common Mistakes:
Forgetting to instantiate PrismaClient in PrismaService
Assuming findMany needs parameters
Thinking method must be static
5. You want to create a NestJS service method that fetches all posts with their authors using Prisma. Which code snippet correctly uses Prisma to fetch posts including author data?
hard
A. return this.prisma.post.findMany({ where: { author: true } });
B. return this.prisma.post.findMany({ select: { authorId: true } });