Should I cache Prisma callls
Answered
Rhinelander posted this in #help-forum
RhinelanderOP
This code is same across many pages...
Could I cache that with React cache? Does it make sense? Is approach bellow correct or should I make dedicated file where I put in this function and cache it there and then call cached version?
const project = await prisma.project.findFirst({
where: { ownerId: user.id },
});
if (!project) {
return <ProjectNotFound />;
}Could I cache that with React cache? Does it make sense? Is approach bellow correct or should I make dedicated file where I put in this function and cache it there and then call cached version?
const project = await cache(prisma.project.findFirst({
where: { ownerId: user.id },
});
)Answered by B33fb0n3
I like to create either a
A react cache makes sense, when you use them multiple times inside multiple components at the same request. Else there won't be a good effect for wrapping it in a react cache
fetcher.ts file or a crud.ts file that contains all these kind of functions.A react cache makes sense, when you use them multiple times inside multiple components at the same request. Else there won't be a good effect for wrapping it in a react cache
5 Replies
@Rhinelander This code is same across many pages...
ts
const project = await prisma.project.findFirst({
where: { ownerId: user.id },
});
if (!project) {
return <ProjectNotFound />;
}
Could I cache that with React cache? Does it make sense? Is approach bellow correct or should I make dedicated file where I put in this function and cache it there and then call cached version?
ts
const project = await cache(prisma.project.findFirst({
where: { ownerId: user.id },
});
)
I like to create either a
A react cache makes sense, when you use them multiple times inside multiple components at the same request. Else there won't be a good effect for wrapping it in a react cache
fetcher.ts file or a crud.ts file that contains all these kind of functions.A react cache makes sense, when you use them multiple times inside multiple components at the same request. Else there won't be a good effect for wrapping it in a react cache
Answer
RhinelanderOP
Well I use it several times across many pages so it makes sense. i call my file prisma.queries.ts because it makes sense. My caching looks like this
import prisma from "@/prisma/db";
import { cache } from "react";
export const findProject = cache(async (userId: string) => {
return await prisma.project.findFirst({
where: { ownerId: userId },
});
});I knew the solution but wasn't sure. Thank you!
happy to help