0
0
NestJSframework~20 mins

Prisma setup in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prisma NestJS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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' }
A[{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
BThrows a runtime error because prisma is not injected
C[]
D[{ name: 'Alice' }, { name: 'Bob' }]
Attempts:
2 left
💡 Hint
Think about what prisma.user.findMany() returns when users exist in the database.
📝 Syntax
intermediate
2: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?
A
import { PrismaClient } from '@prisma/client';

@Module({
  providers: [PrismaClient],
  exports: [PrismaClient],
})
export class PrismaModule {}
B
import { PrismaClient } from '@prisma/client';

@Module({
  providers: [{ provide: PrismaClient, useClass: PrismaClient }],
  exports: [PrismaClient],
})
export class PrismaModule {}
C
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

@Module({
  providers: [{ provide: PrismaClient, useValue: prisma }],
  exports: [PrismaClient],
})
export class PrismaModule {}
D
import { PrismaClient } from '@prisma/client';

@Module({
  providers: [{ provide: 'PRISMA', useClass: PrismaClient }],
  exports: ['PRISMA'],
})
export class PrismaModule {}
Attempts:
2 left
💡 Hint
Think about how to provide a class instance using useClass in NestJS providers.
lifecycle
advanced
2: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?
AThe app shuts down cleanly without any issues.
BThe app throws an error during shutdown.
CPrismaClient automatically disconnects, so no effect occurs.
DThe app may hang or delay shutdown due to open database connections.
Attempts:
2 left
💡 Hint
Think about what happens to open connections if they are not closed.
🔧 Debug
advanced
2: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()?
AThe user model does not exist in Prisma schema.
BPrismaClient is not registered as a provider and so prisma is undefined in constructor.
CfindMany is not a method on prisma.user.
DThe constructor parameter should be optional.
Attempts:
2 left
💡 Hint
Check how PrismaClient is injected into the service.
🧠 Conceptual
expert
3: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?
ACreate a PrismaModule that provides PrismaClient with useClass and import it in all modules needing Prisma.
BInstantiate PrismaClient separately in each service constructor.
CUse a global variable to store PrismaClient and import it in services.
DCreate PrismaClient inside each method call to avoid sharing.
Attempts:
2 left
💡 Hint
Think about NestJS modules and providers for sharing instances.