Query D1 from Hono
Query D1 from the Hono web framework
Hono is a fast web framework for building API-first applications, and it includes first-class support for both Workers and Pages.
When using Workers:
- Ensure you have configured
wrangler.tomlto bind your D1 database to your Worker. - You can access your D1 databases via Hono’s
Contextparameter: bindings are exposed oncontext.env. If you configured a binding namedDB, then you would access D1’s client API methods viac.env.DB. - Refer to the Hono documentation for Cloudflare Workers .
If you are using Pages Functions:
- Bind a D1 database to your Pages Function.
- Pass the
--d1 BINDING_NAME=DATABASE_IDflag towrangler devwhen developing locally.BINDING_NAMEshould match what call in your code, andDATABASE_IDshould match thedatabase_iddefined in your wrangler.toml: for example,--d1 DB=xxxx-xxxx-xxxx-xxxx-xxxx. - Refer to the Hono guide for Cloudflare Pages .
The following examples show how to access a D1 database bound to DB from both a Workers script and a Pages Function: