H
Hono•4mo ago
Jacob

Issues with Cloudflare KV

Hey everyone, I'm trying to make my KV namespaces accessible in my services using dep injection with tsyringe. Is this something that is known to be unsupported? I found that after injecting the kv namespace the put() and get() methods seemingly blocks execution of anything coming after it, without any errors and no data being set or retrieved. I would prefer for my services to have the kv namespaces available instead of having to pass it from the request handler (hono with trpc) through to my repository class. I have a middleware for Hono set up like this.
export const injectServices = createMiddleware(async (c: Context<{
Bindings: ContextBindings,
Variables: ContextVariables
}, any, {}>, next) => {
const env = validateEnv(c);

container.register('env', { useValue: env });
container.register('tokenStore', { useValue: c.env.TOKEN /* KV Namespace */ })
container.register('vision', GCPVisionService)

// REPOS
c.set('repositories', {
VisionRepository: container.resolve(VisionRepository)
});

await next();
})
export const injectServices = createMiddleware(async (c: Context<{
Bindings: ContextBindings,
Variables: ContextVariables
}, any, {}>, next) => {
const env = validateEnv(c);

container.register('env', { useValue: env });
container.register('tokenStore', { useValue: c.env.TOKEN /* KV Namespace */ })
container.register('vision', GCPVisionService)

// REPOS
c.set('repositories', {
VisionRepository: container.resolve(VisionRepository)
});

await next();
})
3 Replies
tombyrer
tombyrer•4mo ago
What exactly are you trying to do? & are you using a build tool like Wrangler &/or Vite?
Gg
Gg•3mo ago
did you ever get this working?
Jacob
JacobOP•3mo ago
Yep, it turned out I simply forgot to await my calls to KV 😅 Though I did create a class that adds some abstraction to the KV methods, so no longer passing it directly as shown above
Want results from more Discord servers?
Add your server