Next.js Discord

Discord Forum

Best way to get and cache data from db with prismsa in app router.

Answered
Plott Hound posted this in #help-forum
Open in Discord
Plott HoundOP
Say I have a simple table of static data in Postgres with prisma. What is the best way to simply list the entries in a nextjs server component?

This is how I’m currently doing it but not sure if there is a better way. I’m not tied to prisma but it seemed like a good ORM:

import { cache } from 'react';
import prisma from "@/lib/prisma";

export const revalidate = 86400; //Revalidate every day since data rarely changes

export const fetchExercises = cache(async () => {
  return await prisma.tablename.findMany({
    select: {
        id: true,
        name: true,
      },
  });
}); 


In my previous project I was using fetch with my CMS and had event based revalidation working great. I’m new to working with dbs. Thanks!
Answered by riský
react cache only cache's for same request, so you could use unstable_cache for global (and has similar cache controls to nextjs fetch)
View full answer

16 Replies

react cache only cache's for same request, so you could use unstable_cache for global (and has similar cache controls to nextjs fetch)
Answer
btw revalidate is also only for pages (not components and such)
@riský btw revalidate is also only for pages (not components and such)
Plott HoundOP
I'm starting to lean toward setting up an endpoint on my vps and use fetch so i can easily cache it. i'd love to keep everything self contained in next but i know you're not supposed to fetch from your own route handlers in server components. I know this is a super simple question but i want my fetching to be rock solid before building out the rest of my app
if you are going to fetch your own api in server component, dont do that: https://nextjs-faq.com/fetch-api-in-rsc
@riský if you are going to fetch your own api in server component, dont do that: https://nextjs-faq.com/fetch-api-in-rsc
Plott HoundOP
That’s what I meant. Not on the nextjs server but another server to separate my api. I wouldn’t fetch my own internal api with localhost. I just want to use nexts caching when using Prisma or any ORM
that is what unstable_cache can help with
@riský that is what unstable_cache can help with
Plott HoundOP
Warning: This API is being developed, and we do not recommend using it in production. It's listed here to show the future direction of the Data Cache.
How stable is it?
personaly it has worked well for me, but it does add fear when updating
but basic fetch can't hurt (it is the same nextjs instance, unstable_cache better, but external can be fin)
Plott HoundOP
Thanks for all the info risky. I’m going to spin up two versions of my app. One using internal server fetching and one using unstable cache and compare the results. Thanks for your time.
I’m obviously cautious about using features in the core of my app that I’m told do not use for production
yeah thats fine
Plott HoundOP
Generally I’ve found next perfect for production. Just this project is heavy on CRUD so it’s been a headache
i would say if it is more of a hoby project (no money loss if fail), then its prob fine, but if down time is very important, wait for stable
Plott HoundOP
Most likely I’ll run the api endpoint on an external server for prod but hopefully unstable cache becomes less erm unstable lol. Thanks!