Sorting of KV keys when using "limit" seems to get from the bottom

Hi Guys,

Maybe this is just a matter of better understanding how this works.
I want to retrieve the last stored key in KV. I have used ULID based keys; this to honor the lexicographically sorting explained here: https://developers.cloudflare.com/workers/runtime-apis/kv#listing-keys

However this seems to be doing the opposite of what I want.

I have writing a small service that put stuff into my namespace. When I use wrangler kv:list to retrieve data, it is working as expected.

# wrangler kv:key list
[
  {
    "name": "01FJPB76VT1KWXP2GSSK2R5SGB"
  },
  {
    "name": "01FJPATAAZ27XBXEWB519QG1AY"
  },
  {
    "name": "01FJPAJM1Y7MGFGQGH5G1TMYYE"
  },
  {
    "name": "01FJP90RQK632HCVAQ5MRSDN1Y"
  },
  {
    "name": "01FJP8XTTHDQ0VZ7GBAH94SFR5"
  },
  {
    "name": "01FJP85BKY4MXESWZ1742VW9A7"
  },
  {
    "name": "01FJP4F8Y29MSJJWWE7A89ZXZN"
  },
  {
    "name": "01FJP4F4QY8EAKN7WQWJ53928C"
  },
  {
    "name": "01FJP447JJ3QVA6TW2Z52JXY58"
  },
  {
    "name": "01FJP445ZHAYD3CGAVG4A1WDVG"
  }
]

The top most entry is the lastet key and the one I want to retrieve with my other service.
When using a worker like this: const lastHeartbeat = await HEARTBEAT.list( {limit: 1} ) then I get the "01FJP445ZHAYD3CGAVG4A1WDVG" key; i.e. the first entered key.

So how do I go about getting the last entered key out using a worker (without traversing ALL keys)?

/Tue

It is worth mentioning that I am running this locally using wrangler dev and at this point I only rely on the preview namespace.

made a workaround.

Added an epoch element in the front of the ID. Then I can use the --prefix option to filter out the correct keys.
There isn’t many entries per hour, so it easy enough to filter the result.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.