Proper way to store Supabase client in Hono Cloudflare Workers

I'm building a Cloudflare Worker using Hono and Supabase, and I'm trying to understand the best way to handle the Supabase client initialization. Specifically, I want to know whether storing the Supabase client in Hono's context is appropriate, given how contexts are handled in the Cloudflare Workers environment. Are Hono contexts generated for every request or are they maintained at the machine/worker level? This affects whether I should initialize the Supabase client for each request or keep a single instance. I know I can store the Supabase client in the context like this:
typescript
import { Hono } from 'hono'
import { createClient } from '@supabase/supabase-js'

type Variables = {
supabase: SupabaseClient
}

const app = new Hono<{ Variables: Variables }>()

app.use('*', async (c, next) => {
// Is this the right approach?
const supabase = createClient(
'SUPABASE_URL',
'SUPABASE_KEY'
)
c.set('supabase', supabase)
await next()
})
typescript
import { Hono } from 'hono'
import { createClient } from '@supabase/supabase-js'

type Variables = {
supabase: SupabaseClient
}

const app = new Hono<{ Variables: Variables }>()

app.use('*', async (c, next) => {
// Is this the right approach?
const supabase = createClient(
'SUPABASE_URL',
'SUPABASE_KEY'
)
c.set('supabase', supabase)
await next()
})
What I'm unsure about: - Is this creating a new Supabase client for every request? - If so, is there a more efficient way to handle this in Cloudflare Workers? - What's the recommended pattern for sharing database clients in Hono?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server