One database query per item in a list
DB-PERF-01Each post is loaded in its own query inside the loop, so a page of 50 posts makes 50 round trips.
lib/posts.ts
Flagged
import { prisma } from './db';
export async function getAuthors(postIds: string[]) {
const authors = [];
for (const id of postIds) {
const post = await prisma.post.findUnique({ where: { id }, include: { author: true } });
authors.push(post?.author);
}
return authors;
}
Passes
import { prisma } from './db';
export async function getAuthors(postIds: string[]) {
const posts = await prisma.post.findMany({ where: { id: { in: postIds } }, include: { author: true } });
return posts.map((post) => post.author);
}