From bf5e9faf43ed0592937930254385e655d62185ac Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Tue, 27 Aug 2024 19:12:52 +0100 Subject: [PATCH] [KV] Adding info on using KV bindings when developing locally (#16369) * Porting over docs for KV binding developing locally. * Changing heading names for style guide consistency. * Using existing component to handle toml file name. * thomasgauvin: content nits (#16434) --------- Co-authored-by: Thomas Gauvin <35609369+thomasgauvin@users.noreply.github.com> --- src/content/docs/kv/concepts/kv-bindings.mdx | 20 ++++++++++++++++++-- src/content/docs/kv/get-started.mdx | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/content/docs/kv/concepts/kv-bindings.mdx b/src/content/docs/kv/concepts/kv-bindings.mdx index 9677203739c5e3..11e6f78d02c27b 100644 --- a/src/content/docs/kv/concepts/kv-bindings.mdx +++ b/src/content/docs/kv/concepts/kv-bindings.mdx @@ -10,7 +10,7 @@ KV [bindings](/workers/runtime-apis/bindings/) allow for communication between a Configure KV bindings in the [wrangler.toml file](/workers/wrangler/configuration/#configure-wranglertoml). -## Reference KV from Workers +## Access KV from Workers A [KV namespace](/kv/concepts/kv-namespaces/) is a key-value database replicated to Cloudflare's global network. @@ -49,7 +49,23 @@ export default { }; ``` -## Reference KV from Durable Objects and Workers using ES modules format +## Use KV bindings when developing locally + +When you use Wrangler to develop locally with the `wrangler dev` command, Wrangler will default to using a local version of KV to avoid interfering with any of your live production data in KV. This means that reading keys that you have not written locally will return `null`. + +To have `wrangler dev` connect to your Workers KV namespace running on Cloudflare's global network, call `wrangler dev --remote` instead. This will use the `preview_id` of the KV binding configuration in the `wrangler.toml` file. This is how a `wrangler.toml` file looks with the `preview_id` specified. + +```toml title="wrangler.toml" +name = "worker" + +# ... + +kv_namespaces = [ + { binding = "TODO", id = "06779da6940b431db6e566b4846d64db", preview_id="06779da6940b431db6e566b484a6a769a7a" } +] +``` + +## Access KV from Durable Objects and Workers using ES modules format [Durable Objects](/durable-objects/) use ES modules format. Instead of a global variable, bindings are available as properties of the `env` parameter [passed to the constructor](/durable-objects/get-started/#3-write-a-class-to-define-a-durable-object). diff --git a/src/content/docs/kv/get-started.mdx b/src/content/docs/kv/get-started.mdx index 482317c0f39c74..d017685a2c415f 100644 --- a/src/content/docs/kv/get-started.mdx +++ b/src/content/docs/kv/get-started.mdx @@ -212,7 +212,7 @@ You can access the binding from within your Worker. When using [`wrangler dev`](/workers/wrangler/commands/#dev) to develop locally, wrangler will default to using a local version of KV to avoid interfering with any of your live production data in KV. This means that reading keys that you have not written locally will return null. -To have `wrangler dev` connect to your Workers KV namespace running on Cloudflare's global network, call `wrangler dev --remote` instead. +To have `wrangler dev` connect to your Workers KV namespace running on Cloudflare's global network, call `wrangler dev --remote` instead. This will use the `preview_id` of the KV binding configuration in the `wrangler.toml` file. See the [KV binding docs](/kv/concepts/kv-bindings/#using-the-kv-binding-when-developing-locally) for more information. :::