N
Nuxt5mo ago
cosbgn

Is there an easy way to get `event`?

I use drizzle and cloudflare so I have an util like this one:
export const useDb = (event) => drizzle(event.context.cloudflare.env.DB, { schema })
export const useDb = (event) => drizzle(event.context.cloudflare.env.DB, { schema })
My issue is that I need always event and it's annoying to pass it around betwen functions. I know there is useEvent() but it requires an experimental asyncContext which is not available on cloudflare. How is everyone solving this?
5 Replies
Single
Single5mo ago
I would create a module to expose event: https://nuxt.com/docs/guide/going-further/modules
cosbgn
cosbgnOP5mo ago
Found it!
const binding = process?.env?.DB || globalThis?.__env__?.DB || globalThis?.DB
const db = drizzle(binding, { schema: schema_file })
const binding = process?.env?.DB || globalThis?.__env__?.DB || globalThis?.DB
const db = drizzle(binding, { schema: schema_file })
Single
Single5mo ago
You said event but you meant environment variables. Glad you found your solution tho. 🙂
manniL
manniL5mo ago
you should use useRuntimeConfig
cosbgn
cosbgnOP5mo ago
Well originally I used event as event.context.cloudflare.env.DB but now I found an alternative without event (using environment variables) I don't think bindings would be available in useRuntimeConfig() no? That's some cloudflare specific thing, not normal env variables (they come from pi0/nitro-cloudflare-dev)
Want results from more Discord servers?
Add your server