Learn how fetch data from your Supabase database in NextKit and Next.js
When fetching data from the Supabase Postgres Database, we use the Supabase JS client.
To make data-fetching from Supabase more reusable, our convention is to create client-agnostic functions that receive a Supabase client as a parameter. Then, we can use these functions in both the browser and the server by injecting in the appropriate client.
For example, let's take a look at the hook to fetch an organization from Supabase.
First, we want to write a query that receives two parameters:
a Supabase client interface
an organization ID
It's important that we pass the client as a parameter so that we can use the function in both the browser and the server.
There are two ways to use this function:
Server: directly, in a server-side function such as: a Route Handler, a Server Action, a Server Component, or a Middleware
Client: indirectly, in a React Hook in a client-side component
Retrieving data in a server-side function
Let's see some scenarios where we can use this function in a server-side.
Route Handler
When you are fetching data from a Route Handler, you can use the getSupabaseRouteHandlerClient function.
Server Action
When you are performing mutations using React Server Actions, you can use the getSupabaseServerActionClient function to read data from the Supabase database.
Server Component
When you are fetching data from a Server Component, you can use the getSupabaseServerComponentClient function.
Retrieving data in a client-side function
Now, if we want to use a React Hook to fetch the organization from one of our components, we can use the useSWR hook from swr:
Retrieving data using a React hook
Now that we have a hook to fetch an organization, we can use it in our components to retrieve the data.