6 Replies
In
TemplatesController
, I call TemplatesService that initializes db in the constructor.hi @SPS | Shootmail! to inform the hono app/context type about variables you've set in middleware, you can either add them to the
Variables
property of your app type, or declare a ContextVariableMap
Context - Hono
Web framework built on Web Standards for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
using either of these approaches will categorically type the context though, so if you need to make it clear that the value is guaranteed to be set, but only after a given middleware has been executed, you could manually type handlers
Thanks for replying. Yes i use context variable map. My concern was how to init the db using env vars outside of the route methods. I misunderstood the context storage. I thought, the context storage will be available outside the route chain but that's not the case. So i am setting db in variables in a middleware and accessing it in the controllers.
But this was, if i implement the MVC architecture, all the methods of the model layer are dependent on the variable that contains the db.
Probably i can use a dependency injection framework like tsyringe but that looks like overkill to me.
gotcha. what runtime are you deploying on?