Proper way to store Supabase client in Hono Cloudflare Workers #3718
-
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: 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:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I find this little middleware helper to be very useful for this specific usecase: https://github.com/maou-shonen/hono-simple-DI. It allows you to define whether your dependency should be request scoped or reused across requests. |
Beta Was this translation helpful? Give feedback.
I find this little middleware helper to be very useful for this specific usecase: https://github.com/maou-shonen/hono-simple-DI. It allows you to define whether your dependency should be request scoped or reused across requests.