Skip to content

Commit ad7203a

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

File tree

1 file changed

+124
-9
lines changed

1 file changed

+124
-9
lines changed

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

+124-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,46 @@ 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+
- `key`: `string[]`
112+
- The key of the KV pair. 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+
You cannot request over 25MB of data. If you do, the request will fail with a `400 Bad Request`.
130+
131+
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.
132+
79133
### `getWithMetadata()` method
80134
135+
The `getWithMetadata()` method can be used to get a single value or multiple values, if given multiple keys.
136+
137+
#### Requesting a single key
138+
81139
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:
82140
83141
```js
@@ -88,7 +146,7 @@ env.NAMESPACE.getWithMetadata(key, options?);
88146
89147
Metadata is a serializable value you append to each KV entry.
90148
91-
#### Parameters
149+
##### Parameters
92150
93151
- `key`: `string`
94152
- The key of the KV pair.
@@ -100,7 +158,7 @@ Metadata is a serializable value you append to each KV entry.
100158
}`
101159
- 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.
102160
103-
#### Response
161+
##### Response
104162
105163
- `response`: `Promise<{
106164
value: string | Object | ArrayBuffer | ReadableStream | null,
@@ -117,7 +175,7 @@ If there is no metadata associated with the requested key-value pair, `null` wil
117175
118176
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.
119177
120-
#### Example
178+
##### Example
121179
122180
An example of reading a key with metadata from within a Worker:
123181
@@ -139,6 +197,63 @@ export default {
139197
};
140198
```
141199
200+
#### Requesting multiple keys
201+
202+
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:
203+
204+
```js
205+
env.NAMESPACE.getWithMetadata(keys, type?);
206+
// OR
207+
env.NAMESPACE.getWithMetadata(keys, options?);
208+
```
209+
210+
Metadata is a serializable value you append to each KV entry.
211+
212+
##### Parameters
213+
214+
- `key`: `string[]`
215+
- The keys of the KV pairs. Max: 100 keys
216+
- `type`: `"text" | "json"`
217+
- Optional. The type of the value to be returned. `text` is the default.
218+
- `options`: `{
219+
cacheTtl?: number,
220+
type?: "text" | "json"
221+
}`
222+
- 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.
223+
224+
##### Response
225+
226+
- `response`: `Promise<Map<string, {
227+
value: string | Object | null,
228+
metadata: string | Object | null
229+
}>`
230+
231+
- 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:
232+
- `text`: A `string` (default).
233+
- `json`: An object decoded from a JSON string.
234+
- The type of the metadata will just depend on what is stored, which can be either a string or an object.
235+
236+
If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.
237+
238+
You cannot request over 25MB of data. If you do, the request will fail with a `400 Bad Request`.
239+
##### Example
240+
241+
An example of reading a key with metadata from within a Worker:
242+
243+
```js
244+
export default {
245+
async fetch(request, env, ctx) {
246+
try {
247+
const response = await env.NAMESPACE.getWithMetadata(["first-key"]);
248+
const firstValue = response.get("first-key");
249+
return new Response("value: " + firstValue?.value + " metadata: " + firstValue?.metadata);
250+
} catch (e) {
251+
return new Response(e.message, { status: 500 });
252+
}
253+
},
254+
};
255+
```
256+
142257
## Guidance
143258
144259
### Type parameter

0 commit comments

Comments
 (0)