Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.enableFiletypes": ["mdx"],
"files.associations": { "__redirects": "plaintext", "_headers": "plaintext" }
"files.associations": { "__redirects": "plaintext", "_headers": "plaintext", "*.mdx": "markdown" },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an intentional change? I don't actually know what this does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was me, I was having some issues rendering the mdx files locally. Reverted it :)

}
145 changes: 136 additions & 9 deletions src/content/docs/kv/api/read-key-value-pairs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ To get the value for a given key, call the `get()` method of the [KV binding](/k
env.NAMESPACE.get(key);
```

The `get()` method returns a promise you can `await` on to get the value. If the key is not found, the promise will resolve with the literal value `null`.
The `get()` method returns a promise you can `await` on to get the value.

If you request a single key as a string, you will get a single response in the promise. If the key is not found, the promise will resolve with the literal value `null`.

Instead of a single key, you can also request an array of keys. In this case, the response is a map with the key-value pairs.

#### Example

Expand All @@ -34,6 +38,22 @@ export default {
};
```

Or if you request multiple keys:

```js
export default {
async fetch(request, env, ctx) {
try {
const value = await env.NAMESPACE.get(["first-key", "second-key"]);

return new Response("values: " + value.get("second-key") + " " + value.get("second-key"));
} catch (e) {
return new Response(e.message, { status: 500 });
}
},
};
```

## Reference

The following methods are provided to read from KV:
Expand All @@ -43,17 +63,19 @@ The following methods are provided to read from KV:

### `get()` method

To get the value for a given key, call the `get()` method on any KV namespace you have bound to your Worker code:
Use the `get()` method to get a single value, or multiple values if given multiple keys.

#### Request a single key

To get the value for a single key, call the `get()` method on any KV namespace you have bound to your Worker code with:

```js
env.NAMESPACE.get(key, type?);
// OR
env.NAMESPACE.get(key, options?);
```

The `get()` method returns a promise you can `await` on to get the value. If the key is not found, the promise will resolve with the literal value `null`.

#### Parameters
##### Parameters

- `key`: `string`
- The key of the KV pair.
Expand All @@ -65,7 +87,7 @@ The `get()` method returns a promise you can `await` on to get the value. If the
}`
- Optional. Object containing the optional `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.

#### Response
##### Response

- `response`: `Promise<string | Object | ArrayBuffer | ReadableStream | null>`
- The value for the requested KV pair. The response type will depend on the `type` parameter provided for the `get()` command as follows:
Expand All @@ -76,8 +98,53 @@ The `get()` method returns a promise you can `await` on to get the value. If the

The `get()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.

#### Request multiple keys

To get the values for multiple keys, call the `get()` method on any KV namespace you have bound to your Worker code with:

```js
env.NAMESPACE.get(keys, type?);
// OR
env.NAMESPACE.get(keys, options?);
```

##### Parameters

- `keys`: `string[]`
- The keys of the KV pairs. Max: 100 keys
- `type`: `"text" | "json"`
- Optional. The type of the value to be returned. `text` is the default.
- `options`: `{
cacheTtl?: number,
type?: "text" | "json"
}`
- Optional. Object containing the optional `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.

:::note

The `.get()` function to read multiple keys does not support `arrayBuffer` or `stream` return types. If you need to read multiple keys of `arrayBuffer` or `stream` types, consider using the `.get()` function to read individual keys in parallel with `Promise.all()`.

:::

##### Response

- `response`: `Promise<Map<string, string | Object | null>`
- The value for the requested KV pair. The response type will depend on the `type` parameter provided for the `get()` command as follows:
- `text`: A `string` (default).
- `json`: An object decoded from a JSON string.

When requesting multiple keys, the promise will never resolve to a null value. It may, however, resolve to a map that contains a key that corresponds to a null value, similar to what happens for a single get.

You cannot request over 25 MB of data. If you do, the request will fail with a `413` Error.

The `get()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.

### `getWithMetadata()` method

Use the `getWithMetadata()` method to get a single value, or multiple values if given multiple keys.

#### Request a single key

To get the value for a given key along with its metadata, call the `getWithMetadata()` method on any KV namespace you have bound to your Worker code:

```js
Expand All @@ -88,7 +155,7 @@ env.NAMESPACE.getWithMetadata(key, options?);

Metadata is a serializable value you append to each KV entry.

#### Parameters
##### Parameters

- `key`: `string`
- The key of the KV pair.
Expand All @@ -100,7 +167,7 @@ Metadata is a serializable value you append to each KV entry.
}`
- Optional. Object containing the optional `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.

#### Response
##### Response

- `response`: `Promise<{
value: string | Object | ArrayBuffer | ReadableStream | null,
Expand All @@ -117,7 +184,7 @@ If there is no metadata associated with the requested key-value pair, `null` wil

The `getWithMetadata()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.

#### Example
##### Example

An example of reading a key with metadata from within a Worker:

Expand All @@ -139,6 +206,66 @@ export default {
};
```

#### Request multiple keys

To get the values for a given set of keys along with their metadata, call the `getWithMetadata()` method on any KV namespace you have bound to your Worker code with:

```js
env.NAMESPACE.getWithMetadata(keys, type?);
// OR
env.NAMESPACE.getWithMetadata(keys, options?);
```

##### Parameters

- `keys`: `string[]`
- The keys of the KV pairs. Max: 100 keys
- `type`: `"text" | "json"`
- Optional. The type of the value to be returned. `text` is the default.
- `options`: `{
cacheTtl?: number,
type?: "text" | "json"
}`
- Optional. Object containing the optional `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.

:::note
The `.get()` function to read multiple keys does not support `arrayBuffer` or `stream` return types. If you need to read multiple keys of `arrayBuffer` or `stream` types, consider using the `.get()` function to read individual keys in parallel with `Promise.all()`.
:::

##### Response

- `response`: `Promise<Map<string, {
value: string | Object | null,
metadata: string | Object | null
}>`

- An object containing the value and the metadata for the requested KV pair. The type of the value attribute will depend on the `type` parameter provided for the `getWithMetadata()` command as follows:
- `text`: A `string` (default).
- `json`: An object decoded from a JSON string.
- The type of the metadata will just depend on what is stored, which can be either a string or an object.

If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.

You cannot request over 25MB of data. If you do, the request will fail with a `413` Error .

##### Example

An example of reading a key with metadata from within a Worker:

```js
export default {
async fetch(request, env, ctx) {
try {
const response = await env.NAMESPACE.getWithMetadata(["first-key"]);
const firstValue = response.get("first-key");
return new Response("value: " + firstValue?.value + " metadata: " + firstValue?.metadata);
} catch (e) {
return new Response(e.message, { status: 500 });
}
},
};
```

## Guidance

### Type parameter
Expand Down
Loading