Skip to content

Commit d5a732c

Browse files
author
talves
committed
KV: add bulk gets documentation
1 parent decbdee commit d5a732c

File tree

2 files changed

+124
-10
lines changed

2 files changed

+124
-10
lines changed

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"editor.defaultFormatter": "esbenp.prettier-vscode",
55
"typescript.tsdk": "node_modules/typescript/lib",
66
"cSpell.enableFiletypes": ["mdx"],
7-
"files.associations": { "__redirects": "plaintext", "_headers": "plaintext" }
7+
"files.associations": { "__redirects": "plaintext", "_headers": "plaintext", "*.mdx": "markdown" },
88
}

src/content/docs/kv/api/read-key-value-pairs.mdx

+123-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ To get the value for a given key, call the `get()` method of the [KV binding](/k
1111
env.NAMESPACE.get(key);
1212
```
1313

14-
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`.
14+
The `get()` method returns a promise you can `await` on to get the value.
15+
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`.
16+
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.
1517

1618
#### Example
1719

@@ -34,6 +36,22 @@ export default {
3436
};
3537
```
3638

39+
Or if you request multiple keys:
40+
41+
```js
42+
export default {
43+
async fetch(request, env, ctx) {
44+
try {
45+
const value = await env.NAMESPACE.get(["first-key", "second-key"]);
46+
47+
return new Response("values: " + value.get("second-key") + " " + value.get("second-key"));
48+
} catch (e) {
49+
return new Response(e.message, { status: 500 });
50+
}
51+
},
52+
};
53+
```
54+
3755
## Reference
3856

3957
The following methods are provided to read from KV:
@@ -43,17 +61,19 @@ The following methods are provided to read from KV:
4361

4462
### `get()` method
4563

46-
To get the value for a given key, call the `get()` method on any KV namespace you have bound to your Worker code:
64+
The `get()` method can be used to get a single value, or multiple values, if given multiple keys
65+
66+
#### Requesting a single key
67+
68+
To get the value for a single key, call the `get()` method on any KV namespace you have bound to your Worker code with:
4769

4870
```js
4971
env.NAMESPACE.get(key, type?);
5072
// OR
5173
env.NAMESPACE.get(key, options?);
5274
```
5375
54-
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`.
55-
56-
#### Parameters
76+
##### Parameters
5777
5878
- `key`: `string`
5979
- The key of the KV pair.
@@ -65,7 +85,7 @@ The `get()` method returns a promise you can `await` on to get the value. If the
6585
}`
6686
- 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.
6787
68-
#### Response
88+
##### Response
6989
7090
- `response`: `Promise<string | Object | ArrayBuffer | ReadableStream | null>`
7191
- The value for the requested KV pair. The response type will depend on the `type` parameter provided for the `get()` command as follows:
@@ -76,8 +96,47 @@ The `get()` method returns a promise you can `await` on to get the value. If the
7696
7797
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.
7898
99+
#### Requesting multiple keys
100+
101+
To get the values for multiple keys, call the `get()` method on any KV namespace you have bound to your Worker code with:
102+
103+
```js
104+
env.NAMESPACE.get(keys, type?);
105+
// OR
106+
env.NAMESPACE.get(keys, options?);
107+
```
108+
109+
##### Parameters
110+
111+
- `keys`: `string[]`
112+
- The keys of the KV pairs. Max: 100 keys
113+
- `type`: `"text" | "json"`
114+
- Optional. The type of the value to be returned. `text` is the default.
115+
- `options`: `{
116+
cacheTtl?: number,
117+
type?: "text" | "json"
118+
}`
119+
- 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.
120+
121+
##### Response
122+
123+
- `response`: `Promise<Map<string, string | Object | null>`
124+
- The value for the requested KV pair. The response type will depend on the `type` parameter provided for the `get()` command as follows:
125+
- `text`: A `string` (default).
126+
- `json`: An object decoded from a JSON string.
127+
128+
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, similarly to what happens for a single get.
129+
130+
You cannot request over 25MB of data. If you do, the request will fail with a `413` Error.
131+
132+
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.
133+
79134
### `getWithMetadata()` method
80135
136+
The `getWithMetadata()` method can be used to get a single value or multiple values, if given multiple keys.
137+
138+
#### Requesting a single key
139+
81140
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:
82141
83142
```js
@@ -88,7 +147,7 @@ env.NAMESPACE.getWithMetadata(key, options?);
88147
89148
Metadata is a serializable value you append to each KV entry.
90149
91-
#### Parameters
150+
##### Parameters
92151
93152
- `key`: `string`
94153
- The key of the KV pair.
@@ -100,7 +159,7 @@ Metadata is a serializable value you append to each KV entry.
100159
}`
101160
- 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.
102161
103-
#### Response
162+
##### Response
104163
105164
- `response`: `Promise<{
106165
value: string | Object | ArrayBuffer | ReadableStream | null,
@@ -117,7 +176,7 @@ If there is no metadata associated with the requested key-value pair, `null` wil
117176
118177
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.
119178
120-
#### Example
179+
##### Example
121180
122181
An example of reading a key with metadata from within a Worker:
123182
@@ -139,6 +198,61 @@ export default {
139198
};
140199
```
141200
201+
#### Requesting multiple keys
202+
203+
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:
204+
205+
```js
206+
env.NAMESPACE.getWithMetadata(keys, type?);
207+
// OR
208+
env.NAMESPACE.getWithMetadata(keys, options?);
209+
```
210+
211+
##### Parameters
212+
213+
- `keys`: `string[]`
214+
- The keys of the KV pairs. Max: 100 keys
215+
- `type`: `"text" | "json"`
216+
- Optional. The type of the value to be returned. `text` is the default.
217+
- `options`: `{
218+
cacheTtl?: number,
219+
type?: "text" | "json"
220+
}`
221+
- 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.
222+
223+
##### Response
224+
225+
- `response`: `Promise<Map<string, {
226+
value: string | Object | null,
227+
metadata: string | Object | null
228+
}>`
229+
230+
- 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:
231+
- `text`: A `string` (default).
232+
- `json`: An object decoded from a JSON string.
233+
- The type of the metadata will just depend on what is stored, which can be either a string or an object.
234+
235+
If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.
236+
237+
You cannot request over 25MB of data. If you do, the request will fail with a `413` Error .
238+
##### Example
239+
240+
An example of reading a key with metadata from within a Worker:
241+
242+
```js
243+
export default {
244+
async fetch(request, env, ctx) {
245+
try {
246+
const response = await env.NAMESPACE.getWithMetadata(["first-key"]);
247+
const firstValue = response.get("first-key");
248+
return new Response("value: " + firstValue?.value + " metadata: " + firstValue?.metadata);
249+
} catch (e) {
250+
return new Response(e.message, { status: 500 });
251+
}
252+
},
253+
};
254+
```
255+
142256
## Guidance
143257
144258
### Type parameter

0 commit comments

Comments
 (0)