Next.js Discord

Discord Forum

Scaling data with Next.js, @vercel/postgres (maybe even @vercel/postgres-kysely)

Unanswered
Exotic Shorthair posted this in #help-forum
Open in Discord
Exotic ShorthairOP
Hey all. My brain is melting a touch today and I could use some general 👍👎 advise while thinking about Next.js and a Vercel Postgres database. Does this make sense?:

Examples I see have views and queries tightly coupled (great for small examples):
import { sql } from "@vercel/postgres";

export default async function Cart({
  params
} : {
  params: { user: string }
}): Promise<JSX.Element> {
  const { rows } = await sql`SELECT * from CARTS where user_id=${params.user}`;

  return (
    <div>
      {rows.map((row) => (
        <div key={row.id}>
          {row.id} - {row.quantity}
        </div>
      ))}
    </div>
  );
}


However, for a scaling application that could change databases altogether at some point (no plans, yet been there), I was thinking about adding a MyComponentData.ts to live alongside MyCompoent.tsx, which could abstract the calls and be replaced as needed.

Does anyone have opinions on dealing with data connections in Next.js? Some of the server-side rendering with data is still blurring in mind on occasion. My seperation of concern habits and React->Next.js data patterns are still being ironed out on occasion.

Thanks for taking a look. 🙂

3 Replies

European sprat
I mean it definitely makes sense to move the various DB calls into their own file so they can be exported and reused in other components
And if you need to change the DB calls then it's done in one spot
Exotic ShorthairOP
Thanks for quick response!

It's starting to solidify here. I think I'll create a data folder next to api in /app and go from there, keeping all the data things together that way. Thanks again!