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?
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(); } }
Prisma returns the full created record including the auto-generated ID.
The prisma.user.create method returns the full user object including the auto-generated id. So the response JSON includes id, name, and email.
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?
const updatedUser = await prisma.user.update({ where: { id: 5 }, data: { email: 'newemail@example.com' } }); console.log(updatedUser);
The update method returns the full updated record.
The prisma.user.update method returns the entire updated user object, including unchanged fields like name. So the output includes id, name, and the updated email.
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?
const deletedUser = await prisma.user.delete({ where: { email: 'nonexistent@example.com' } });
Deleting a non-existing record causes a specific Prisma error.
Prisma throws PrismaClientKnownRequestError when trying to delete a record that does not exist, indicating the record to delete was not found.
Choose the Prisma query that correctly fetches all users whose email ends with '.org'.
Look for the operator that matches the end of a string.
The endsWith operator filters strings that end with the specified substring. contains matches anywhere, startsWith matches the beginning, and equals matches exact strings.
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?
await prisma.user.upsert({ where: { id: 10 }, update: { name: 'Bob' }, create: { email: 'bob@example.com' } });
Check if the create data includes all required fields.
The create object is missing the required name field, causing a validation error. Prisma requires all non-nullable fields in create data.