Bird
0
0

You want to update a user's email only if the user exists in the database using Prisma in NestJS. Which approach correctly handles this?

hard📝 Application Q8 of 15
NestJS - Database with Prisma
You want to update a user's email only if the user exists in the database using Prisma in NestJS. Which approach correctly handles this?
AUse update() directly
BUse delete() then create() with new email
CUse upsert() with update and create data
DUse findUnique() first, then update() if user exists
Step-by-Step Solution
Solution:
  1. Step 1: Understand update() behavior

    update() throws error if record not found, so direct use risks exceptions.
  2. Step 2: Consider safe update approach

    Using findUnique() first checks existence, then update() safely.
  3. Step 3: Evaluate other options

    upsert() creates if missing, not just update; delete() then create() is inefficient.
  4. Final Answer:

    Use findUnique() first, then update() if user exists -> Option D
  5. Quick Check:

    Check existence before update to avoid errors [OK]
Quick Trick: Check record exists before update to avoid errors [OK]
Common Mistakes:
  • Using update() without existence check
  • Misusing upsert() when only update needed
  • Deleting then creating instead of updating

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes