Skip to content

Commit

Permalink
[KV] Adding info on using KV bindings when developing locally (#16369)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Oxyjun and thomasgauvin authored Aug 27, 2024
1 parent f2143ce commit bf5e9fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/content/docs/kv/concepts/kv-bindings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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).

Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/kv/get-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
:::
Expand Down

0 comments on commit bf5e9fa

Please sign in to comment.