Bird
Raised Fist0
NestJSframework~20 mins

CRUD with Prisma in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Prisma CRUD Master
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 create operation?
Given the following NestJS Prisma service code, what will be the output when creating a new user with name 'Alice' and email 'alice@example.com'?
NestJS
async createUser() {
  const user = await this.prisma.user.create({
    data: { name: 'Alice', email: 'alice@example.com' }
  });
  return user;
}
AReturns null because no user was found
B{"id": 1, "name": "Alice", "email": "alice@example.com"}
CThrows a runtime error because 'id' is missing in data
D{"name": "Alice", "email": "alice@example.com"}
Attempts:
2 left
💡 Hint
Prisma automatically generates the 'id' field if it is set as an auto-increment primary key.
❓ state_output
intermediate
2:00remaining
What is the result of this Prisma update operation?
Consider this update method in a NestJS Prisma service. What will be the returned value after updating the user with id 5 to have name 'Bob'?
NestJS
async updateUser() {
  const updatedUser = await this.prisma.user.update({
    where: { id: 5 },
    data: { name: 'Bob' }
  });
  return updatedUser;
}
A{"id": 5, "name": "Bob", "email": "previous_email@example.com"}
Bnull because user with id 5 does not exist
C{"name": "Bob"}
DThrows a PrismaClientKnownRequestError because user with id 5 does not exist
Attempts:
2 left
💡 Hint
Prisma throws an error if you try to update a record that does not exist.
🔧 Debug
advanced
2:00remaining
Why does this Prisma delete operation fail?
This NestJS Prisma service method tries to delete a user by id but fails with a runtime error. What is the cause?
NestJS
async deleteUser(id: number) {
  await this.prisma.user.delete({
    where: { userId: id }
  });
}
AThe field 'userId' does not exist in the Prisma schema; it should be 'id'.
BThe method is missing an await keyword before the delete call.
CThe delete method requires a 'data' property, not 'where'.
DPrisma delete cannot delete by id, only by unique email.
Attempts:
2 left
💡 Hint
Check the exact field names in your Prisma schema for the User model.
📝 Syntax
advanced
2:00remaining
Which option correctly fetches all users with Prisma in NestJS?
Select the correct Prisma query to fetch all users from the database.
Aconst users = await this.prisma.user.getMany();
Bconst users = await this.prisma.user.findAll();
Cconst users = await this.prisma.user.findMany();
Dconst users = await this.prisma.user.fetchAll();
Attempts:
2 left
💡 Hint
Check the Prisma Client API for fetching multiple records.
🧠 Conceptual
expert
2:00remaining
What happens if you call Prisma update with no matching record?
In NestJS Prisma, what is the behavior when you call prisma.user.update with a where clause that matches no records?
APrisma throws a PrismaClientKnownRequestError indicating no record found.
BPrisma returns null without error.
CPrisma creates a new record with the update data.
DPrisma silently ignores the update and returns the previous data.
Attempts:
2 left
💡 Hint
Consider how Prisma handles updates on non-existent records.

Practice

(1/5)
1. In NestJS with Prisma, which method is used to retrieve multiple records from the database?
easy
A. create
B. update
C. findMany
D. delete

Solution

  1. Step 1: Understand Prisma CRUD methods

    Prisma provides create to add, findMany to get multiple records, update to change, and delete to remove records.
  2. Step 2: Identify method for retrieving multiple records

    The method findMany is specifically designed to fetch many records from the database.
  3. Final Answer:

    findMany -> Option C
  4. Quick Check:

    Retrieve multiple records = findMany [OK]
Hint: Remember: findMany fetches many records [OK]
Common Mistakes:
  • Confusing create with findMany
  • Using update to retrieve data
  • Trying to delete to get records
2. Which of the following is the correct syntax to create a new user record using Prisma in a NestJS service?
easy
A. this.prisma.user.create({ data: { name: 'Alice' } })
B. this.prisma.user.findMany({ data: { name: 'Alice' } })
C. this.prisma.user.update({ data: { name: 'Alice' } })
D. this.prisma.user.delete({ data: { name: 'Alice' } })

Solution

  1. Step 1: Identify the method to add a record

    The create method is used to add new records in Prisma.
  2. Step 2: Check the syntax for creating a user

    The correct syntax is this.prisma.user.create({ data: { ... } }) where data holds the new record's fields.
  3. Final Answer:

    this.prisma.user.create({ data: { name: 'Alice' } }) -> Option A
  4. Quick Check:

    Create new record = create method [OK]
Hint: Use create with data object to add records [OK]
Common Mistakes:
  • Using findMany instead of create
  • Passing data to update without where clause
  • Using delete method to add records
3. Given this Prisma update call in a NestJS service:
this.prisma.user.update({ where: { id: 5 }, data: { email: 'new@example.com' } })

What will this operation do?
medium
A. Delete the user with id 5
B. Update the email of the user with id 5 to 'new@example.com'
C. Create a new user with id 5 and email 'new@example.com'
D. Return all users with email 'new@example.com'

Solution

  1. Step 1: Understand the update method parameters

    The update method requires a where clause to find the record and data to specify changes.
  2. Step 2: Analyze the given call

    It targets user with id: 5 and changes the email field to 'new@example.com'.
  3. Final Answer:

    Update the email of the user with id 5 to 'new@example.com' -> Option B
  4. Quick Check:

    Update with where and data changes record [OK]
Hint: Update needs where to find record, data to change [OK]
Common Mistakes:
  • Thinking update creates new records
  • Confusing update with delete
  • Missing where clause causes errors
4. What is wrong with this Prisma delete call in a NestJS service?
this.prisma.user.delete({ data: { id: 3 } })
medium
A. The delete method should use a where clause, not data
B. The delete method cannot delete by id
C. The syntax is correct and will delete user with id 3
D. The delete method requires an update object

Solution

  1. Step 1: Check Prisma delete method syntax

    The delete method requires a where clause to specify which record to remove.
  2. Step 2: Identify the error in the code

    The code incorrectly uses data instead of where, causing a syntax error.
  3. Final Answer:

    The delete method should use a where clause, not data -> Option A
  4. Quick Check:

    Delete needs where, not data [OK]
Hint: Delete uses where to find record, not data [OK]
Common Mistakes:
  • Using data instead of where in delete
  • Trying to update in delete method
  • Omitting where clause causes errors
5. You want to update a user's name only if the user exists, otherwise create a new user with that name using Prisma in NestJS. Which approach correctly handles this logic?
hard
A. Use delete then create to replace the user
B. Call update and if it fails, call create manually
C. Use findMany then create without update
D. Use upsert with where, update, and create objects

Solution

  1. Step 1: Understand Prisma's upsert method

    The upsert method updates a record if it exists or creates it if not, combining both actions safely.
  2. Step 2: Identify correct usage for conditional update or create

    Using upsert with where to find the user, update for changes, and create for new record is the best practice.
  3. Final Answer:

    Use upsert with where, update, and create objects -> Option D
  4. Quick Check:

    Conditional update or create = upsert [OK]
Hint: Use upsert to update or create in one call [OK]
Common Mistakes:
  • Manually catching update errors to create
  • Using findMany without update
  • Deleting before creating wastes operations
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