Bird
Raised Fist0
NestJSframework~20 mins

Prisma Client usage in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Prisma Client Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Prisma Client query in NestJS?
Consider a Prisma Client query fetching a user by ID. What will be the output if the user does not exist?
NestJS
const user = await prisma.user.findUnique({ where: { id: 999 } });
console.log(user);
A{} (empty object)
Bundefined
CThrows an error
Dnull
Attempts:
2 left
💡 Hint
Think about what Prisma returns when no record matches the query.
❓ state_output
intermediate
2:00remaining
What is the value of 'createdUser' after this Prisma create call?
Given the following Prisma Client code in a NestJS service, what does 'createdUser' contain?
NestJS
const createdUser = await prisma.user.create({
  data: { email: 'test@example.com', name: 'Test User' }
});
console.log(createdUser);
A{ email: 'test@example.com', name: 'Test User' }
B{ id: number, email: 'test@example.com', name: 'Test User', createdAt: Date }
Cnull
DThrows a runtime error
Attempts:
2 left
💡 Hint
Prisma returns the full created record including auto-generated fields.
📝 Syntax
advanced
2:00remaining
Which option correctly updates a user's name using Prisma Client in NestJS?
Select the correct Prisma Client syntax to update the name of a user with id 5 to 'Alice'.
Aawait prisma.user.update({ where: { id: 5 }, data: { name: 'Alice' } });
Bawait prisma.user.update({ data: { name: 'Alice' }, where: { id: 5 } });
Cawait prisma.user.update({ id: 5, name: 'Alice' });
Dawait prisma.user.update({ where: { id: 5 }, name: 'Alice' });
Attempts:
2 left
💡 Hint
Check the order and keys required by Prisma's update method.
🔧 Debug
advanced
2:00remaining
What error does this Prisma Client code produce?
Analyze the following code snippet and identify the error it causes.
NestJS
const user = await prisma.user.findUnique({ where: { email: 'notfound@example.com' } });
console.log(user.name);
ANo error, prints undefined
BReferenceError: user is not defined
CTypeError: Cannot read property 'name' of null
DSyntaxError: Unexpected token '.'
Attempts:
2 left
💡 Hint
Consider what happens when findUnique returns null and you try to access a property.
🧠 Conceptual
expert
2:00remaining
Which option best describes Prisma Client's transaction behavior in NestJS?
When using Prisma Client's $transaction method in NestJS, what happens if one query inside the transaction fails?
AAll queries inside the transaction are rolled back; no changes are saved.
BOnly the failed query is rolled back; others remain committed.
CThe transaction ignores errors and commits all queries anyway.
DPrisma Client throws an error but commits successful queries.
Attempts:
2 left
💡 Hint
Think about the atomic nature of transactions.

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

  1. Step 1: Understand Prisma Client's role

    Prisma Client is a tool that helps you talk to your database easily using JavaScript.
  2. 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.
  3. Final Answer:

    To interact with the database using simple JavaScript calls -> Option A
  4. 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

  1. Step 1: Check the official import syntax

    The correct import is from '@prisma/client' and creating a new PrismaClient instance.
  2. 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.
  3. Final Answer:

    import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); -> Option A
  4. 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:
const user = await prisma.user.findUnique({ where: { id: 5 } });
console.log(user);

What will be logged if a user with id 5 exists in the database?
medium
A. An array of all users in the database
B. A syntax error
C. null
D. An object containing the user's data with id 5

Solution

  1. Step 1: Understand findUnique behavior

    findUnique returns a single record matching the condition or null if none found.
  2. Step 2: Given user with id 5 exists

    The query returns the user object with id 5, so console.log prints that object.
  3. Final Answer:

    An object containing the user's data with id 5 -> Option D
  4. Quick Check:

    findUnique with existing id = user object [OK]
Hint: findUnique returns one object or null, not array [OK]
Common Mistakes:
  • Expecting an array instead of an object
  • Assuming null even if user exists
  • Thinking it causes syntax error
4. Identify the error in this NestJS Prisma Client usage:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

async function getUsers() {
const users = prisma.user.findMany();
return users;
}
medium
A. Function getUsers should not be async
B. Missing await before prisma.user.findMany()
C. findMany() does not exist on prisma.user
D. PrismaClient should not be instantiated directly

Solution

  1. Step 1: Check async function usage

    prisma.user.findMany() returns a Promise, so it must be awaited inside async functions.
  2. Step 2: Identify missing await

    The code misses 'await' before the call, so it returns a Promise instead of resolved data.
  3. Final Answer:

    Missing await before prisma.user.findMany() -> Option B
  4. 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

  1. Step 1: Understand PrismaClient lifecycle

    PrismaClient should connect once when app starts and disconnect on shutdown to manage resources efficiently.
  2. 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.
  3. Final Answer:

    Create PrismaClient instance in a service, call prisma.$connect() in onModuleInit(), and prisma.$disconnect() in onModuleDestroy() -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard