Performance: CRUD operations with Prisma
This affects server response time and client perceived loading speed when performing database operations.
Jump into concepts and practice - no test required
const usersWithPosts = await prisma.user.findMany({ include: { posts: true } });
const users = await prisma.user.findMany(); for (const user of users) { user.posts = await prisma.post.findMany({ where: { userId: user.id } }); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple sequential queries (N+1 problem) | 0 (server-side) | 0 | 0 | [X] Bad |
| Single optimized query with include/select | 0 (server-side) | 0 | 0 | [OK] Good |
prisma.user.create() method do in Next.js with Prisma?create() is used to add new data in Prisma.prisma.user.create() adds a new user.update() with where and data keys to update records.update() and proper keys where and data.const users = await prisma.user.findMany({ where: { active: true } });
console.log(users.length);console.log(users.length) output?findMany() method returns an array of records matching the where condition.active: true are returned, so users.length is count of active users.await prisma.user.delete({ id: 10 });where key specifying the record to delete.{ id: 10 } directly, missing where: { id: 10 }.upsert() fits perfectly.