Bird
Raised Fist0
NestJSframework~10 mins

Prisma setup in NestJS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import PrismaService in a NestJS module.

NestJS
import { Module } from '@nestjs/common';
import { [1] } from './prisma.service';

@Module({
  providers: [PrismaService],
  exports: [PrismaService],
})
export class PrismaModule {}
Drag options to blanks, or click blank then click option'
APrismaService
BPrismaModule
CPrismaClient
DPrismaController
Attempts:
3 left
💡 Hint
Common Mistakes
Importing PrismaClient directly instead of PrismaService.
Using PrismaModule instead of PrismaService.
2fill in blank
medium

Complete the PrismaService class to extend the correct Prisma client.

NestJS
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { [1] } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
  async onModuleInit() {
    await this.$connect();
  }

  async onModuleDestroy() {
    await this.$disconnect();
  }
}
Drag options to blanks, or click blank then click option'
APrismaModule
BPrismaService
CPrismaController
DPrismaClient
Attempts:
3 left
💡 Hint
Common Mistakes
Extending PrismaService instead of PrismaClient.
Importing wrong class from @prisma/client.
3fill in blank
hard

Fix the error in injecting PrismaService into a controller constructor.

NestJS
import { Controller, Get } from '@nestjs/common';
import { PrismaService } from './prisma.service';

@Controller('users')
export class UserController {
  constructor(private readonly [1]: PrismaService) {}

  @Get()
  async findAll() {
    return this.prisma.user.findMany();
  }
}
Drag options to blanks, or click blank then click option'
APrismaService
Bprisma
CuserService
Dservice
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name as variable name causing confusion.
Using unrelated variable names like userService.
4fill in blank
hard

Fill both blanks to create a Prisma query that finds users with age greater than 18.

NestJS
async findAdults() {
  return this.prisma.user.findMany({
    where: { age: { [1]: [2] } }
  });
}
Drag options to blanks, or click blank then click option'
Agt
Bgte
Clt
D18
Attempts:
3 left
💡 Hint
Common Mistakes
Using lt or lte which mean less than.
Using gte which includes 18, not strictly greater.
5fill in blank
hard

Fill all three blanks to create a Prisma query that updates a user's email by id.

NestJS
async updateEmail([1]: number, [2]: string) {
  return this.prisma.user.update({
    where: { id: [1] },
    data: { email: [2] }
  });
}
Drag options to blanks, or click blank then click option'
AuserId
BnewEmail
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in parameters and query.
Forgetting to update the email field.

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

  1. Step 1: Understand PrismaService role

    PrismaService wraps the Prisma Client to provide a single database connection instance across the app.
  2. Step 2: Differentiate from other roles

    It does not handle HTTP or define schema; those are handled by controllers and Prisma schema files respectively.
  3. Final Answer:

    To provide a reusable database client instance for querying the database -> Option A
  4. 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

  1. 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.
  2. Step 2: Verify module metadata

    It should be listed under providers and exports to be injectable and reusable in other modules.
  3. Final Answer:

    import { PrismaService } from './prisma.service'; @Module({ providers: [PrismaService], exports: [PrismaService] }) -> Option B
  4. 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

  1. Step 1: Understand findUnique behavior

    findUnique returns a single record matching the unique filter or null if none found.
  2. Step 2: Given user with ID 1 exists

    The method returns the user object with ID 1, so console.log prints that object.
  3. Final Answer:

    An object containing the user's data with ID 1 -> Option C
  4. 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?
async getAllUsers() {
  return this.prisma.user.findMany();
}
Options:
medium
A. The Prisma Client instance was not initialized in PrismaService
B. The user model is missing in the Prisma schema
C. The method should be static to access prisma
D. findMany() requires parameters and cannot be empty

Solution

  1. Step 1: Analyze error message

    'Cannot read property user of undefined' means this.prisma is undefined in the service.
  2. Step 2: Check PrismaService initialization

    Prisma Client must be instantiated and assigned to this.prisma in the constructor; missing this causes the error.
  3. Final Answer:

    The Prisma Client instance was not initialized in PrismaService -> Option A
  4. Quick Check:

    Uninitialized prisma client = undefined error [OK]
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 } });
C. return this.prisma.post.findUnique({ where: { id: 1 }, include: { author: true } });
D. return this.prisma.post.findMany({ include: { author: true } });

Solution

  1. Step 1: Understand fetching related data

    To get posts with their authors, use findMany with include: { author: true } to load related author data.
  2. Step 2: Check other options

    return this.prisma.post.findMany({ select: { authorId: true } }); only selects authorId, not author object. return this.prisma.post.findUnique({ where: { id: 1 }, include: { author: true } }); fetches one post, not all. return this.prisma.post.findMany({ where: { author: true } }); uses invalid where clause.
  3. Final Answer:

    return this.prisma.post.findMany({ include: { author: true } }); -> Option D
  4. Quick Check:

    Include loads related models in findMany [OK]
Hint: Use include to fetch related models in Prisma queries [OK]
Common Mistakes:
  • Using select instead of include for relations
  • Using findUnique when multiple records needed
  • Incorrect where clause for relations
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