Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle Supabase calls from server-side Lib folder? #434

Open
fmarcheski opened this issue Nov 20, 2024 · 1 comment
Open

How to handle Supabase calls from server-side Lib folder? #434

fmarcheski opened this issue Nov 20, 2024 · 1 comment
Labels
question Further information is requested

Comments

@fmarcheski
Copy link

I've been using Nuxt Supabase's serverSupabaseServiceRole role heavily within my app. I use Nuxt's Server API as a layer between my Client and my Supabase DB. So far so good.

Right now, most of my logic is built right into the API route where I create a 'const client' from serverSupabaseServiceRole from Nitro's event (see this example from https://supabase.nuxtjs.org/usage/services/serversupabaseservicerole)

import { serverSupabaseServiceRole } from '#supabase/server'

export default eventHandler(async (event) => {
  const client = serverSupabaseServiceRole(event)

  const { data } = await client.from('rls-protected-table').select()

  return { sensitiveData: data }
})

Now, as my app logic becomes more complex, I want to migrate some of my logic out of the API route and into the Server Lib folder where I have files of helper functions. The issue is that the only way for those functions to call Supabase is for me to pass either the Client variable or the event from the API route. As an example:

export default eventHandler(async (event) => {
  const { data } = await libraryFunction('some-id', event)
  return { someData: data }
})
// Example passing Event
import { serverSupabaseServiceRole } from '#supabase/server'

const libraryFunction = async (
  id: string,
  event: any
) => {

const { data } = await client.from('rls-protected-table').select().eq('id', id);
return { sensitiveData: data }
};

export { libraryFunction };

As a simple example, this seems fine but passing the Client/Event around doesn't feel right and will likely get more complex as the app grows. What is the proper way to handle this?
Thanks for the help.

@fmarcheski fmarcheski added the question Further information is requested label Nov 20, 2024
@GKaza
Copy link

GKaza commented Jan 17, 2025

had the same issue, thanks for bringing up the question. Why do we even have to pass the event in the serverSupabaseServiceRole? I thought the event was to check authentication, why do we need it with service role which bypasses rls? Am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants