Comments (5)
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.
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.
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.
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.
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)
- Add the ability to get the latest updatedAt first in the https://vercel.com/docs/storage/vercel-blob/using-blob-sdk#list method HOT 2
- Cannot delete blob database HOT 2
- Is there a way to change timezone for a session HOT 2
- Vercel Blob: Access denied, please provide a valid token for this resource HOT 15
- Progress rate of upload to Blob HOT 2
- Support for Signed URLs in Blob HOT 2
- Getting a broken image file from Blob storage HOT 2
- How to get the url by pathname? HOT 4
- Kysely Migrations fail because VercelPostgresDialect fails to report that VercelPostgres does not support transactions HOT 2
- Cannot use @vercel/blob in Edge runtime HOT 4
- Intermittent Request Failures with Vercel Postgres and Next.js HOT 6
- Vercel KV Request vs Storage Limit
- insert query are cached HOT 5
- @vercel/kv Inconsistent increment behavior in Server Action HOT 5
- @vercel/postgres: Extended constant is not working HOT 5
- [vercel/blob] file should use the true mime type when uploading the file HOT 3
- Vercel KV not reachable HOT 2
- @vercel/postgres parameters bind error HOT 1
- X-Frame-Options set to DENY prevents embedding PDF blob in object, embed or iframe HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from storage.