Get to Know Cloudflare — Workers KV

What is Workers KV — key-value data is stored to centralized data centers, then caches that data in Cloudflare’s data centers after access.

Workers KV supports exceptionally high read volumes with low latency.

Thanks all, you could found another topic of Cloudflare Workers from this referral: Get to Know Cloudflare — Workers Series

Cloudflare Workers KV Logo

Setup KV

  • Prerequisite
  • KV Operation

Binding KV

  • Workspace
  • wrangler.toml
  • Deployment Practical
  • POV

Setup KV

This section, I’d like to make preset before continuing…

Prerequisite

  • Login to cloudflare
  • Verify you're identical with wrangler command wrangler login
  • Terminal return link, and redirect to your default browser (or copy that link and open it on your browser)
  • Please allow
  • Return Successfully logged in.
wrangler login, allow, and success

KV Operation

Next step you have 2 options to operate KV, let’s see both options
A.) Cloudflare Console
B.) Wrangler Command

  • A.) Cloudflare Console

Create

## Cloudfalre - WorkerKV

### Craete KV
1. goto navigate side
2. expand "Workers and Pages"
3. click "KV" on navigate side
4. click "create a namespace" button
5. type "my-worker-kv"
6. click "Add" button
7. then kv namespace created, available as list below
1, 2, 3
4, 5, 6
7

View, Add

## Cloudfalre - WorkerKV

### View, Add KV
1. list avaialbe 2 columns instance as name and id
2. click "View" button
3. detail for each namespace from this page
4. add key and value
5. click "Add entry" button
6. then <key, value> or KV avaialbe under that namespace,
typing key as "A" and value as "worker-kv-a"
1, 2
3, 4, 5, 6
after 6

Delete

## Cloudfalre - WorkerKV

### Delete KV
1. you can see "•••" three dot
2. click "Delete"
3. your kv was deleted
  • B.) Wrangler Command

Create
Have been seen, created by console below A.) Cloudfalre Console.

## Wrangler - WorkerKV

### CREATE
```
wrangler kv:namespace create <YOUR_NAMESPACE>
```

Example
`wrangler kv:namespace create user-kv`

Result return namespace as binding, and serialized-id as id
{ binding = "user-kv", id = "c4cff0582d4444019e78c124fc15ecd7" }

Put
This looks like “Add Entry” below A.) Cloudfalre Console

## Wrangler - WorkerKV

### PUT
#### 1. Binding
```
wrangler kv:key put --binding=<YOUR_BINDING> "<KEY>" "<VALUE>"
wrangler kv:key put --env=staging --binding=<YOUR_BINDING> "<KEY>" "<VALUE>"
```
Example `wrangler kv:key put --binding=user-kv "A1" "12313123"`

#### 2. Namespace
```
wrangler kv:key put --namespace-id=<YOUR_ID> "<KEY>" "<VALUE>"
wrangler kv:key put --env=staging --namespace-id=<YOUR_ID> "<KEY>" "<VALUE>"
```
Example `wrangler kv:key put --namespace-id=c4cff0582d4444019e78c124fc15ecd7 "A1" "12313123"`

Get

## Wrangler - WorkerKV

### GET
#### 1. Binding
```
wrangler kv:key get --binding=<YOUR_BINDING> "<KEY>"
wrangler kv:key get --env=staging --binding=<YOUR_BINDING> "<KEY>"
```
Example `wrangler kv:key get --binding=user-kv "A1" "12313123"`

### 2. Namespace
```
wrangler kv:key get --namespace-id=<YOUR_ID> "<KEY>"
wrangler kv:key get --env=staging --namespace-id=<YOUR_ID> "<KEY>"
```
Example `wrangler kv:key get --namespace-id=c4cff0582d4444019e78c124fc15ecd7 "A1"`

Binding KV

Workspace

I reorganized the project structure from Get to Know Cloudflare — Workers

From

hello-world (project)
|-src/index.ts
|-wrangler.toml

To

hello-world (project)
|./worker/src/index.ts
|./wrangler/wrangler.toml

wrangler.toml

Update toml file.
*Hint, Let’s bring namespace id put into value of both “id” and “preview_id” to avoid trouble and mind only worker.

# wrangler.toml
name = "hello-world"
main = "../worker/src/index.ts"
compatibility_date = "2023-06-08"

## env.dev was assigned as a default
kv_namespaces = [
{ binding = "user-kv", "preview_id" = "c4cff0582d4444019e78c124fc15ecd7", id = "c4cff0582d4444019e78c124fc15ecd7" },
]

Deployment Practical

When executed the command immediately looks for wrangler.toml, then go to the right directory before.

wrangler deploy [SCRIPT] [OPTIONS]

wrangler deploy would like to assign kv_namespaces as default (as dev) then you be able to control deployment with option env

wrangler deploy --env prod

That means you have to provide various environments into toml file.

# wrangler.toml
name = "hello-world"
main = "../worker/src/index.ts"
compatibility_date = "2023-06-08"

## env.dev was assigned as a default
kv_namespaces = [
{ binding = "user-kv", "preview_id" = "c4cff0582d4444019e78c124fc15ecd7", id = "c4cff0582d4444019e78c124fc15ecd7" },
]

[env.prod]
vars = { ENVIRONMENT = "prod" }
kv_namespaces = [
{ binding = "user-kv"", "preview_id" = "...", id = "..." },
]

POV

This development Workers KV, however in the deployment needs to be optimized process to prevent leak information from toml file with expected or unexpected situations.

We could override binding values into kv_namespace with --var or --define instead.

--

--

Chutipon Pongpanit (Aof)
EarthSeaTech - Software Engineering

Aoftionstyle idea here #idea is anything happen faster speed of light then captured in the chest. no ✔ no ✘ just Casually open chest beacause world need you