Open hrueger opened 6 months ago
Never mind, I figured it out by looking at the code of https://github.com/authhero/auth. For anyone interested:
index.ts
file wrapping app.fetch
:
import { ExecutionContext } from "hono";
import { Env } from "./_helpers/Env";
import app from "./app";
import { PrismaClient } from "@prisma/client";
import { PrismaD1 } from "@prisma/adapter-d1";
const server = { async fetch(request: Request, env: Env, ctx: ExecutionContext) { const adapter = new PrismaD1(env.D1_MAIN); return app.fetch( request, { ...env, prisma: new PrismaClient({ adapter }) }, ctx, ); }, };
export default server;
everything attached to the env here can be used by adding `@Request() request: RequestWithContext` as a parameter to your route handler. Now, you can use `request.ctx.env.prisma`.
@markusahlstrand I had to patch tsoa-hono
since wrangler complained about now knowing about Buffer
here:
[ERROR] ReferenceError: Buffer is not defined
nodejs_compat
was enabled. Maybe you want to include this?
tsoa-hono-npm-1.2.1-04da50d363.patch
Hi! Yes, it's on my todo list to see if we somehow can remove that dependency..
Great to know, thanks! It works fine with the patch though, so no worries.
Hi @markusahlstrand, I'm currently building an api and looking for a framework.
tsoa
+tsoa-hono
looks awesome! I just cannot figure out how to access Bindings for deploying to Cloudflare Workers... Is there an example on how to do this? Specifically, I'd like to talk to a D1 database. Thanks in advance!Best Hannes