You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/kv/api/read-key-value-pairs.mdx
+124-9
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,9 @@ To get the value for a given key, call the `get()` method of the [KV binding](/k
11
11
env.NAMESPACE.get(key);
12
12
```
13
13
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.
The following methods are provided to read from KV:
@@ -43,17 +61,19 @@ The following methods are provided to read from KV:
43
61
44
62
### `get()` method
45
63
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:
47
69
48
70
```js
49
71
env.NAMESPACE.get(key, type?);
50
72
// OR
51
73
env.NAMESPACE.get(key, options?);
52
74
```
53
75
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
57
77
58
78
- `key`: `string`
59
79
- 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
65
85
}`
66
86
- 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.
- 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
76
96
77
97
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.
78
98
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.
- 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
+
79
133
### `getWithMetadata()` method
80
134
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
+
81
139
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:
Metadata is a serializable value you append to each KV entry.
90
148
91
-
#### Parameters
149
+
##### Parameters
92
150
93
151
- `key`: `string`
94
152
- The key of the KV pair.
@@ -100,7 +158,7 @@ Metadata is a serializable value you append to each KV entry.
100
158
}`
101
159
- 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.
@@ -117,7 +175,7 @@ If there is no metadata associated with the requested key-value pair, `null` wil
117
175
118
176
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.
119
177
120
-
#### Example
178
+
##### Example
121
179
122
180
An example of reading a key with metadata from within a Worker:
123
181
@@ -139,6 +197,63 @@ export default {
139
197
};
140
198
```
141
199
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:
0 commit comments