0
0
NextJSframework~20 mins

CRUD operations with Prisma in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 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 in Next.js?

Consider this Next.js API route using Prisma to create a new user:

import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

export default async function handler(req, res) {
  if (req.method === 'POST') {
    const user = await prisma.user.create({
      data: { name: 'Alice', email: 'alice@example.com' }
    });
    res.json(user);
  } else {
    res.status(405).end();
  }
}

What will the response JSON contain after a successful POST request?

NextJS
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

export default async function handler(req, res) {
  if (req.method === 'POST') {
    const user = await prisma.user.create({
      data: { name: 'Alice', email: 'alice@example.com' }
    });
    res.json(user);
  } else {
    res.status(405).end();
  }
}
A{"name": "Alice", "email": "alice@example.com"}
B{"error": "Method not allowed"}
C{"id": 1, "name": "Alice", "email": "alice@example.com"}
D{"message": "User created successfully"}
Attempts:
2 left
💡 Hint

Prisma returns the full created record including the auto-generated ID.

state_output
intermediate
2:00remaining
What is the result of this Prisma update operation?

Given this Prisma update code snippet in a Next.js API route:

const updatedUser = await prisma.user.update({
  where: { id: 5 },
  data: { email: 'newemail@example.com' }
});
console.log(updatedUser);

Assuming a user with id = 5 exists, what will console.log(updatedUser) output?

NextJS
const updatedUser = await prisma.user.update({
  where: { id: 5 },
  data: { email: 'newemail@example.com' }
});
console.log(updatedUser);
A{"id": 5, "name": "Existing Name", "email": "newemail@example.com"}
B{"id": 5, "email": "newemail@example.com"}
Cundefined
DThrows Prisma.PrismaClientKnownRequestError
Attempts:
2 left
💡 Hint

The update method returns the full updated record.

🔧 Debug
advanced
2:00remaining
Why does this Prisma delete operation cause an error?

Review this Next.js API route snippet:

export default async function handler(req, res) {
  if (req.method === 'DELETE') {
    const deletedUser = await prisma.user.delete({
      where: { email: 'nonexistent@example.com' }
    });
    res.json(deletedUser);
  } else {
    res.status(405).end();
  }
}

What error will this code throw if no user has the given email?

NextJS
const deletedUser = await prisma.user.delete({
  where: { email: 'nonexistent@example.com' }
});
ANo error, returns null
BTypeError: Cannot read property 'email' of undefined
CSyntaxError: Unexpected token delete
DPrisma.PrismaClientKnownRequestError: Record to delete does not exist.
Attempts:
2 left
💡 Hint

Deleting a non-existing record causes a specific Prisma error.

🧠 Conceptual
advanced
2:00remaining
Which Prisma query returns all users with emails ending in '.org'?

Choose the Prisma query that correctly fetches all users whose email ends with '.org'.

Aprisma.user.findMany({ where: { email: { endsWith: '.org' } } })
Bprisma.user.findMany({ where: { email: { contains: '.org' } } })
Cprisma.user.findMany({ where: { email: { startsWith: '.org' } } })
Dprisma.user.findMany({ where: { email: { equals: '.org' } } })
Attempts:
2 left
💡 Hint

Look for the operator that matches the end of a string.

📝 Syntax
expert
2:00remaining
What error does this Prisma upsert code produce?

Analyze this Prisma upsert code snippet:

await prisma.user.upsert({
  where: { id: 10 },
  update: { name: 'Bob' },
  create: { email: 'bob@example.com' }
});

What error will this code raise?

NextJS
await prisma.user.upsert({
  where: { id: 10 },
  update: { name: 'Bob' },
  create: { email: 'bob@example.com' }
});
ASyntaxError: Unexpected token 'await'
BPrisma.PrismaClientValidationError: Missing required field 'name' in create data.
CNo error, operation succeeds
DPrisma.PrismaClientKnownRequestError: Unique constraint failed on the fields: (`id`)
Attempts:
2 left
💡 Hint

Check if the create data includes all required fields.