KV API - Need either keys() or count() and key(index)

#1

It is possible to get the list of keys for a KV namespace using the REST API; however, it seems the in Worker API is incomplete without one of the following:

  1. a generator that returns a key at a time async function* keys() { … }
  2. a function that returns an array of keys function keys(). This does not seem like a great idea given the potential size of the key set.
  3. a count function and the ability to get a key by index
#2

They plan to add ability to get namespaces based on prefix, do listing and limited search.

#3

Thanks. Meanwhile I keep an Object in the KV store with the key _keys. On the object I store all the keys I have put into the store. Ugh :-).

#4

Just be wary of the 64kb max per-key limit.

#5

This is what I see in the docs. What is the 64kb limit? Is that while in beta?

  • Keys of up to 512 bytes
  • Values of up to 2 MB
#6

Ah, I’m sorry, I forgot that they changed the limits. It’s 2MB now :slightly_smiling_face: still, you would hit the limit after a few hundred thousand keys and it will take cpu time to parse a large index, especially if the key names are not small.

#7

Just testing for now, so OK. If we get further down the road and nothing built-in is available, we will call the REST API from within the worker to get the keys (which might be a big Array, but … Just want something simple for now.

1 Like
#8

The upcoming search and prefix is a must, else the kv isn’t usable for even medium size apps.