Skip to content

Commit b1223f5

Browse files
committed
minor updates
1 parent cecf711 commit b1223f5

File tree

1 file changed

+39
-70
lines changed

1 file changed

+39
-70
lines changed

src/content/docs/kv/get-started.mdx

Lines changed: 39 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ A [KV namespace](/kv/concepts/kv-namespaces/) is a key-value database replicated
116116

117117
<Tabs syncKey = 'CLIvsDash'> <TabItem label='CLI'>
118118

119-
[Wrangler](/workers/wrangler/) allows you to put, list, get, and delete entries within your KV namespace.
119+
You can use [Wrangler](/workers/wrangler/) to create a new KV namespace. You can also use it to perform operations such as put, list, get, and delete within your KV namespace.
120120

121121
:::note
122122

@@ -171,6 +171,14 @@ To create a KV namespace via Wrangler:
171171

172172
You must create a binding to connect your Worker with your KV namespace. [Bindings](/workers/runtime-apis/bindings/) allow your Workers to access resources, like KV, on the Cloudflare developer platform.
173173

174+
:::note[Bindings]
175+
176+
A binding is how your Worker interacts with external resources such as [KV namespaces](/kv/concepts/kv-namespaces/). A binding is a runtime variable that the Workers runtime provides to your code. You can declare a variable name in your Wrangler file that binds to these resources at runtime, and interact with them through this variable. Every binding's variable name and behavior is determined by you when deploying the Worker.
177+
178+
Refer to [Environment](/kv/reference/environments/) for more information.
179+
180+
:::
181+
174182
To bind your KV namespace to your Worker:
175183

176184
<Tabs syncKey='CLIvsDash'><TabItem label='CLI'>
@@ -194,14 +202,6 @@ To bind your KV namespace to your Worker:
194202
- Your binding is available in your Worker at `env.<BINDING_NAME>` from within your Worker. For this tutorial, the binding is available at `env.USERS_NOTIFICATION_CONFIG`.
195203
</Steps>
196204

197-
:::note[Bindings]
198-
199-
A binding is how your Worker interacts with external resources such as [KV namespaces](/kv/concepts/kv-namespaces/). A binding is a runtime variable that the Workers runtime provides to your code. You can declare a variable name in your Wrangler file that binds to these resources at runtime, and interact with them through this variable. Every binding's variable name and behavior is determined by you when deploying the Worker.
200-
201-
Refer to [Environment](/kv/reference/environments/) for more information.
202-
203-
:::
204-
205205
</TabItem><TabItem label='Dashboard'>
206206

207207
<Steps>
@@ -232,6 +232,8 @@ To write a value to your empty KV namespace using Wrangler:
232232
npx wrangler kv key put --binding=<BINDING_NAME> "<KEY>" "<VALUE>"
233233
```
234234

235+
In this tutorial, you will add a key `user_1` with value `enabled` to the KV namespace you created in [step 2](/kv/get-started/#2-create-a-kv-namespace).
236+
235237
```sh
236238
npx wrangler kv key put --binding=USERS_NOTIFICATION_CONFIG "user_1" "enabled"
237239
```
@@ -240,7 +242,7 @@ To write a value to your empty KV namespace using Wrangler:
240242
```
241243
</Steps>
242244

243-
:::note
245+
:::note[Using `--namespace-id`]
244246
Instead of using `--binding`, you can also use `--namespace-id` to specify which KV namespace should receive the operation:
245247

246248
```sh "--namespace-id=<BINDING_ID>"
@@ -252,12 +254,16 @@ Writing the value "<VALUE>" to key "<KEY>" on namespace <BINDING_ID>.
252254
```
253255
:::
254256

255-
To create a key and a value in local mode, add the `--local` flag at the end of the command:
257+
:::note[Storing values in remote KV namespace]
256258

257-
```sh ins="--local"
258-
npx wrangler kv key put --namespace-id=xxxxxxxxxxxxxxxx "<KEY>" "<VALUE>" --local
259+
By default, the values are written locally. To create a key and a value in your remote KV namespace, add the `--remote` flag at the end of the command:
260+
261+
```sh ins="--remote"
262+
npx wrangler kv key put --namespace-id=xxxxxxxxxxxxxxxx "<KEY>" "<VALUE>" --remote
259263
```
260264

265+
:::
266+
261267
</TabItem><TabItem label = 'Dashboard'>
262268
<Steps>
263269
1. Go to [**Storage & Databases** > **KV**](https://dash.cloudflare.com/?to=/:account/workers/kv/namespaces).
@@ -274,38 +280,26 @@ npx wrangler kv key put --namespace-id=xxxxxxxxxxxxxxxx "<KEY>" "<VALUE>" --loca
274280

275281
<Tabs syncKey='CLIvsDash'><TabItem label = 'CLI'>
276282

277-
To access the value using Wrangler:
283+
To access the value from your KV namespace using Wrangler:
278284

279285
<Steps>
280286
1. Run the `wrangler kv key get` subcommand in your terminal, and input your key value:
281287

282288
```sh
283-
# Replace [OPTIONS] with --binding or --namespace-id
284-
npx wrangler kv key get [OPTIONS] "<KEY>"
289+
npx wrangler kv key get --binding=<BINDING_NAME> "<KEY>"
285290
```
286291

287-
A KV namespace can be specified in two ways. Replace `[OPTIONS]` with `--binding` or `--namespace-id`:
288-
289-
<Details header="With a `--binding`">
290-
291-
```sh
292-
npx wrangler kv key get --binding=<BINDING_NAME> "<KEY>"
293-
```
294-
295-
</Details>
296-
297-
<Details header ="With a `--namespace-id`">
292+
In this tutorial, you will get the value of the key `user_1` from the KV namespace you created in [step 2](/kv/get-started/#2-create-a-kv-namespace).
298293

299294
```sh
300-
npx wrangler kv key get --namespace-id=<YOUR_ID> "<KEY>"
295+
npx wrangler kv key get --binding=USERS_NOTIFICATION_CONFIG "user_1" --text
301296
```
302-
</Details>
303297

304-
For example, using the `--binding` option:
298+
:::note
299+
To view the value directly within the terminal, you use the `--text` flag.
300+
:::
305301

306-
```sh
307-
npx wrangler kv key get --binding=USERS_NOTIFICATION_CONFIG "user_1"
308-
```
302+
Similar to the `put` command, the `get` command can also be used to access a KV namespace in two ways - with `--binding` or `--namespace-id`:
309303

310304
</Steps>
311305

@@ -316,9 +310,7 @@ To access the value using Wrangler:
316310
Exactly **one** of `--binding` or `--namespace-id` is required.
317311
:::
318312

319-
:::note
320-
To view the value directly within the terminal, add `--text`
321-
:::
313+
322314

323315
Refer to the [`kv bulk` documentation](/kv/reference/kv-commands/#kv-bulk) to write a file of multiple key-value pairs to a given KV namespace.
324316

@@ -345,7 +337,7 @@ To have `wrangler dev` connect to your Workers KV namespace running on Cloudflar
345337
:::
346338

347339
<Steps>
348-
1. In your Worker script, add your KV binding in the `Env` interface:
340+
1. In your Worker script, add your KV binding in the `Env` interface. If you have bootstrapped your project with JavaScript, this step is not required.
349341

350342
```ts
351343
interface Env {
@@ -354,16 +346,16 @@ To have `wrangler dev` connect to your Workers KV namespace running on Cloudflar
354346
}
355347
```
356348

357-
2. Use the `put()` method on `USERS_NOTIFICATION_CONFIG` to create a new key-value pair:
349+
2. Use the `put()` method on `USERS_NOTIFICATION_CONFIG` to create a new key-value pair. You will add a new key `user_2` with value `disabled` to your KV namespace.
358350

359351
```ts
360-
let value = await env.USERS_NOTIFICATION_CONFIG.put(key, value);
352+
let value = await env.USERS_NOTIFICATION_CONFIG.put("user_2", "disabled");
361353
```
362354

363-
3. Use the KV `get()` method to fetch the data you stored in your KV database:
355+
3. Use the KV `get()` method to fetch the data you stored in your KV namespace. You will fetch the value of the key `user_2` from your KV namespace.
364356

365357
```ts
366-
let value = await env.USERS_NOTIFICATION_CONFIG.get("KEY");
358+
let value = await env.USERS_NOTIFICATION_CONFIG.get("user_2");
367359
```
368360
</Steps>
369361

@@ -380,9 +372,11 @@ Your Worker code should look like this:
380372

381373
The code above:
382374

383-
1. Writes a key to `USERS_NOTIFICATION_CONFIG` binding using KV's `put()` method.
384-
2. Reads the same key using KV's `get()` method, and returns an error if the key is null (or in case the key is not set, or does not exist).
385-
3. Uses JavaScript's [`try...catch`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) exception handling to catch potential errors. When writing or reading from any service, such as Workers KV or external APIs using `fetch()`, you should expect to handle exceptions explicitly.
375+
1. Writes a key to your KV namespace using KV's `put()` method.
376+
2. Reads the same key using KV's `get()` method.
377+
3. It checks if the key is null, and returns a `404` response if it is.
378+
4. If the key is not null, it returns the value of the key.
379+
5. Uses JavaScript's [`try...catch`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) exception handling to catch potential errors. When writing or reading from any service, such as Workers KV or external APIs using `fetch()`, you should expect to handle exceptions explicitly.
386380

387381
</TabItem><TabItem label = 'Dashboard'>
388382

@@ -414,32 +408,7 @@ The code above:
414408

415409
</TabItem></Tabs>
416410

417-
## 6. Deploy your KV
418-
419-
You can deploy your project in one of two ways:
420-
421-
- **Locally:** Runs from your machine on a local server. The URL to your web application is only accessible while you are running the application from your terminal.
422-
- **Remotely:** Runs on Cloudflare's global network. The URL to your web application will be accessible to anyone with the URL.
423-
424-
:::note
425-
You can only deploy your project locally through the Wrangler CLI. You can only deploy remotely when deploying through the Cloudflare dashboard.
426-
:::
427-
428-
In this tutorial, we expect the URL to simply return the value `disabled` (which corresponds to the key `user_2`).
429-
430-
### Deploy your project locally
431-
432-
<Steps>
433-
1. Run the following command within your project directory:
434-
435-
```sh
436-
npm run dev
437-
```
438-
439-
2. Visit the URL for your newly created Workers KV application running on your local machine, as provided by Wrangler (usually `localhost:8787`).
440-
</Steps>
441-
442-
### Deploy your project globally
411+
## 6. Deploy your Worker
443412

444413
<Tabs syncKey = 'CLIvsDash'><TabItem label = 'CLI'>
445414

0 commit comments

Comments
 (0)