Service pattern with Hono.js
Hey Im coming from Expressjs backend dev background, and its a bit confusing to me how to implement service pattern with Hono js on top of Cloudflare workers.
What i want to achieve is create a singleton object of thirdparty SDK service in
/services/engine.ts
file.
And then inside hono routes use it like this:
How can I achieve this? Im getting error inside engine.ts
file, since it cannot access env vars for some reason.16 Replies
I’m not completely understanding, but since this is serverless you cannot have any objects that persist and are shared across workers. You will need to create that engine object each time it’s used. You get the env variable from the request context
oh shoot. so for each route, i need to create it again and again? i have tons of routes...
I think so. That’s how serverless works. It’s meant for stateless endpoints. What does creating that engine do? Does it need to persist data across multiple calls, or does it make a connection to something?
yeah, makes connection to a 3rd party service. creates a client. that i need to use inside routes
Yea, you would need to do that each time your worker handles an api call
Is that an expensive process or can it be done many times like this?
I haven’t used durable objects yet and I’m trying to learn about them. They may be able to help you here, since I think they can let you keep objects in memory or something.
Definitely worth taking a look at the docs, but I really do not understand them so 🤷🏼♂️
@IEatBeans thanks
not expensive, just too much boilerplate, considering i have 100s of routes
@rickyrobinett
You could put all the boilerplate and config for the engine in a function, and then just call it in each worker? The code will still run and create the client each time, but you won’t have to see that every time
yeah, thats what im doing right now, as i couldnt find any better way
It’s hard to migrate from a stateful backend like node or express to serverless, because you really have to think about it differently. It’s much easier to start a project completely serverless (if you can)
@yusukebe who will have the best insight on doing this with Hono
Something like this?
Don't mind the fake SDK there
Hi @flow
Indeed, you have to create the
engine
instance per route in the Cloudflare environment. Then, you can use a simple factory method to do it like the following:
Then you can use it in your routes:
Jinx!
Your example isn't per-route though, right? You create the
appWithDB
once, then it works in every route, no?
At least for that Worker anyway(and I guess as long as you aren't mixing multiple routers together/using other handlers).As a syntax, one app would cover every route as you imagine. But, actually, the
app
will be created per request.Oh, like that
Wait
If you aren't calling the
new Hono()
constructor within the fetch()
handler then wouldn't it be able to be reused over multiple requests, asuming they hit the same isolate?
It wouldn't reuse the DB client instance, but it would reuse the app
itselfSorry for confusing!
new Hono()
is called once outside the fetch()
as you mentioned. But the middleware that is defined in the factory is called everytime:
So you are right!