Cloudflare 中文文档
D1
编辑这个页面
跳转官方原文档
Set theme to dark (⇧+D)

Query D1 from Remix

Query your D1 database from a Remix application.

Remix is a full-stack web framework that operates on both client and server. You can query your D1 database(s) from Remix using Remix’s data loading API with the useLoaderData hook.

To set up a new Remix site on Cloudflare Pages that can query D1:

  1. Refer to the Remix guide.
  2. Bind a D1 database to your Pages Function.
  3. Pass the --d1 BINDING_NAME=DATABASE_ID flag to wrangler dev when developing locally. BINDING_NAME should match what call in your code, and DATABASE_ID should match the database_id defined in your wrangler.toml: for example, --d1 DB=xxxx-xxxx-xxxx-xxxx-xxxx.

The following example shows you how to define a Remix loader that has a binding to a D1 database.

  • Bindings are passed through on the context.env parameter passed to a LoaderFunction.
  • If you configured a binding named DB, then you would access D1’s client API methods via context.env.DB.