Skip to main content

Remote development

还没翻译译自英文(developers.cloudflare.com)本页 13 段都还没译

D1 supports remote development using the dashboard playground. The dashboard playground uses a browser version of Visual Studio Code, allowing you to rapidly iterate on your Worker entirely in your browser.

1. Bind a D1 database to a Worker

Note

This guide assumes you have previously created a Worker, and a D1 database.

Users new to D1 and/or Cloudflare Workers should read the D1 tutorial to install wrangler and deploy their first database.

  1. In the Cloudflare dashboard, go to the Workers & Pages page.

    dash.cloudflare.com

  2. Select an existing Worker.

  3. Go to the Bindings tab.

  4. Select Add binding.

  5. Select D1 database > Add binding.

  6. Enter a variable name, such as DB, and select the D1 database you wish to access from this Worker.

  7. Select Add binding.

2. Start a remote development session

  1. On the Worker's page on the Cloudflare dashboard, select Edit Code at the top of the page.
  2. Your Worker now has access to D1.

Use the following Worker script to verify that the Worker has access to the bound D1 database:

export default {
  async fetch(request, env, ctx) {
    const res = await env.DB.prepare("SELECT 1;").run();
    return new Response(JSON.stringify(res, null, 2));
  },
};