Skip to content

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

Open
@fmarcheski

Description

@fmarcheski

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions