Giter Site home page Giter Site logo

Comments (5)

luismeyer avatar luismeyer commented on June 8, 2024 1

I guess theoretically this would be possible but the problem with that is that you have to expose your Postgres connection string to your client, which you don't want because then any user can just do anything with your DB. Your connection string should be a secret and therefor has to live on the server.

If you don't want to bother writing a REST API you should just use server actions to query your database. As in my example these server action can also be used inside SWR.

from storage.

luismeyer avatar luismeyer commented on June 8, 2024

hey @anaclumos,

you can use Vercel Postgres with SWR. If you are using Next.js you can just put the sql call into a route handler or a server action. Here is a quick example how I made it work.

In a page.tsx you can load the intial data since it's a server component:

import { loadTitles } from "./load-titles";
import { Titles } from "./titles";

export default async function Home() {
  const titles = await loadTitles();

  return (
    <main>
      <h1>Titles</h1>
      <Titles titles={titles} />
    </main>
  );
}

Then you can pass the data to a client component which calls useSWR with fallbackData and revalidateOnMount: false since we already fetched the data initially.

"use client";

import useSWR from "swr";
import { loadTitles } from "./load-titles";

export async function Titles({ titles }: { titles: { title: string }[] }) {
  const { data } = useSWR("titles", loadTitles, {
    fallbackData: titles,
    revalidateOnMount: false,
  });

  return (
    <div>
      {data.map((title, i) => (
        <div key={`${title} ${i}`}>
          {i} {title.title}
        </div>
      ))}
    </div>
  );
}

The loadTitles function is a server action which looks like this:

"use server";
import { sql } from "@vercel/postgres";

export async function loadTitles() {
  const titles = await sql<{ title: string }>`SELECT * FROM titles`;
  return titles.rows;
}

Does this work for you?

from storage.

anaclumos avatar anaclumos commented on June 8, 2024

I see... but in this case we still need an API layer for SWR right? I was wanting to get rid of API altogether and only use sql from Vercel Postgres. I wanted to have some polling features in this case. Do you know if this is possible?

from storage.

luismeyer avatar luismeyer commented on June 8, 2024

I assume you want to run the sql query inside a server component then, which is technically your API.
Server Components are not meant to maintain a connection to the client. So for this kind of polling or revalidating on focus you need to introduce some client side fetching.

This post points out some restriction which server components have: https://vercel.com/blog/understanding-react-server-components

from storage.

anaclumos avatar anaclumos commented on June 8, 2024

I understand. Then is there a way to use useSWR directly to the sql query? (Without writing much more boilderplate code for REST APIs

from storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.