Just starting with Supabase + Cloudflare Pages and...
# help
r
Just starting with Supabase + Cloudflare Pages and am reading the code at https://github.com/supabase/supabase/tree/master/examples/with-cloudflare-workers It seems like we are creating a new supabase client for each and every htttp request, which seems very unintuitive as normally we try to reuse a client (db connection) as much as possible in other servers. So, is it really ok to make a new supabse client every single http request, or should I try to reuse this
db
declaration in other parts of the app as well?
Copy code
import { createClient } from '@supabase/supabase-js'

addEventListener('fetch', (event) => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const db = createClient(
    'YOUR_SUPABASE_URL', // Replace with your project's URL
    'YOUR_SUPABASE_KEY', // Replace with your project's anon/service_role key
    {
      fetch: fetch.bind(globalThis), // Tell Supabase Client to use Cloudflare Workers' global `fetch` API to make requests
    }
  )
  const { data, error } = await db.from('YOUR_TABLE_NAME').select('*')

  if (error) {
    console.log(error)
    return new Response(error.message || error.toString(), {
      status: 500,
    })
  }

  return new Response(JSON.stringify(data), {
    status: 200,
    headers: { 'Content-Type': 'application/json' },
  })
}
n
Hello @rishav! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
x
A "supabase client" is simply a set of methods. You contact supabase through rest, so as long as you do not interact with supabase, your client isn't doing anything other than being an instance of a class
n
rishav (2022-03-23)
x
There's no "db connection" to speak of
A supabase client is a set of typed helpers around
fetch
. You could do away with the client completely, and just use
fetch
directly
r
Thanks @User
n
Thread was archived by @rishav. Anyone can send a message to unarchive it.