Is it possible to get key and value in one request, or, filter the value?

I’m building a huge namespace list of key values (KV). The list contain something like

[
   { key: '0.0.0.0', value: { views: 100, type: 'extreme' } }
   { key: '0.0.0.1', value: { views: 9, type: 'low' } }
]

So… to get the keys that the value it’s > 0 and type == ‘extreme’, I need to do the following requests

  1. storage/kv/namespaces/{namespaceId}/keys/?cursor=1 // 1000 items

  2. storage/kv/namespaces/{namespaceId}/values/1

  3. storage/kv/namespaces/{namespaceId}/values/2…

  4. storage/kv/namespaces/{namespaceId}/values/1000

  5. storage/kv/namespaces/{namespaceId}/keys/?cursor=2 // +1000 items

  6. storage/kv/namespaces/{namespaceId}/values/1

  7. storage/kv/namespaces/{namespaceId}/values/2…

  8. storage/kv/namespaces/{namespaceId}/values/100

So, this will tame some time and requests.

My questions is; Is there a better way to bring only the keys + values that I want?

1 Like

Not from what I can gather from the documentation, Storage options guide · Cloudflare Workers docs

Your best bet might be trying to incorporate the type as prefix into the name (i.e. extreme-key or low-key) and then use the filter of list() to filter for these. Though I am not sure that filter is also available on the web interface and not just via the JavaScript functions.

1 Like

Just to chime in here, “bulk get” is a thing that’s on my radar as a feature; it’s true we don’t have anything that makes this simple right now, and this advice (prefixing) is the best thing you can do at the moment.

1 Like

“Bulk get” using the API is just what we’ve been missing from KV! Any idea when something like that may be available?

I cannot give you an estimate, but I appreciate you mentioning the need! These comments help me prioritize what would be most useful for folks.

2 Likes

My most needed requests:

  • Worker scheduling (Like AWS Lambdas Tasks)
  • Atomic Counter
  • KV Bulk Read
  • KV Backup or History
4 Likes

The last one especially, along with the same for normal Workers would allow a revert for Sites as well, in case something goes wrong instead of having to re-deploy if the fix is quickly possible.

4 Likes