How to handle Supabase calls from server-side Lib folder?
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.
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?
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.
When doing it, i got a error during the build process.