Challenge - 5 Problems
Prisma NestJS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when querying all users with Prisma in NestJS?
Given a NestJS service using Prisma Client to fetch all users, what will be the output if the database has two users named Alice and Bob?
NestJS
import { Injectable } from '@nestjs/common'; import { PrismaClient } from '@prisma/client'; @Injectable() export class UserService { private prisma = new PrismaClient(); async getAllUsers() { return await this.prisma.user.findMany(); } } // Assume the database has two users: { id: 1, name: 'Alice' } and { id: 2, name: 'Bob' }
Attempts:
2 left
💡 Hint
Think about what prisma.user.findMany() returns when users exist in the database.
✗ Incorrect
The prisma.user.findMany() method returns an array of user objects with all fields by default. Since the database has two users, the output is an array with both user objects including their ids and names.
📝 Syntax
intermediate2:00remaining
Which option correctly imports and provides PrismaClient in a NestJS module?
You want to use PrismaClient in your NestJS app. Which code snippet correctly sets up PrismaClient as a provider in a module?
Attempts:
2 left
💡 Hint
Think about how to provide a class instance using useClass in NestJS providers.
✗ Incorrect
Option C correctly uses useValue with an instantiated PrismaClient, which is a common pattern to share a single instance. Option C uses useClass which causes NestJS to instantiate PrismaClient multiple times and can cause issues. Option C tries to provide the class directly without useClass or useValue, which is invalid. Option C uses a string token but does not match typical PrismaClient injection patterns.
❓ lifecycle
advanced2:00remaining
What happens if PrismaClient is not disconnected properly in NestJS?
Consider a NestJS service that creates a PrismaClient instance but never calls prisma.$disconnect(). What is the likely effect when the app shuts down?
Attempts:
2 left
💡 Hint
Think about what happens to open connections if they are not closed.
✗ Incorrect
If PrismaClient is not disconnected, open database connections remain active, which can cause the app to hang or delay shutdown as it waits for connections to close. PrismaClient does not automatically disconnect on app shutdown.
🔧 Debug
advanced2:00remaining
Why does this NestJS Prisma service throw 'Cannot read property findMany of undefined'?
Look at this service code snippet:
import { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class UserService {
constructor(private prisma: PrismaClient) {}
async getUsers() {
return await this.prisma.user.findMany();
}
}
What causes the error when calling getUsers()?
Attempts:
2 left
💡 Hint
Check how PrismaClient is injected into the service.
✗ Incorrect
The error occurs because PrismaClient is not provided in the NestJS dependency injection system, so the prisma parameter is undefined. Without registering PrismaClient as a provider, NestJS cannot inject it.
🧠 Conceptual
expert3:00remaining
What is the best way to share a single PrismaClient instance across multiple NestJS services?
You have multiple services in NestJS that need to use PrismaClient. Which approach ensures a single PrismaClient instance is shared app-wide?
Attempts:
2 left
💡 Hint
Think about NestJS modules and providers for sharing instances.
✗ Incorrect
Creating a PrismaModule that provides PrismaClient as a singleton using useClass and importing it where needed ensures one shared instance. Instantiating separately or per method wastes resources and can cause connection issues. Using global variables is not recommended in NestJS architecture.