-
Notifications
You must be signed in to change notification settings - Fork 89
[Feature Request] Add KV list method #130
Comments
I've personally being using this shim: // namespace.test.ts
/* eslint-disable @typescript-eslint/camelcase */
import Cloudworker from '@dollarshaveclub/cloudworker'
const LIST_MAX_LIMIT = 1000
const namespace = new (Cloudworker as any).KeyValueStore()
namespace.list = async function(
this,
{
limit = 1000,
prefix = ``,
cursor = `-1`,
}: {
prefix?: string
limit?: number
cursor?: string
}
): Promise<{
keys: {
name: string
expiration?: number
}[]
list_complete: boolean
cursor: string
}> {
if (limit > LIST_MAX_LIMIT) limit = LIST_MAX_LIMIT
if (limit < 0) limit = 0
const { store } = this
let keys = Array.from(store.keys() as string[]).filter((key: string) =>
key.startsWith(prefix)
)
keys = keys.slice(+cursor + 1)
const list_complete = keys.length <= limit
keys = keys.slice(0, limit)
const nextCursor = (
+cursor + (list_complete ? keys.length : limit)
).toString()
return Promise.resolve({
keys: keys.map(key => ({
name: key,
})),
list_complete,
cursor: nextCursor,
})
}
export { namespace } If there is interest, I'm more than happy to write up a PR. The only concern I have is with the I also don't know how Cloudflare are generating the cursors they use (e.g. |
This looks solid!
The main thing to verify here would be the behavior of the cursors when there's lots of insertions in between list operations. I slightly suspect that Cloudflare's cursors use the last key of the previous "page" as a starting point for the next page rather than its global order, but I haven't verified this experimentally. |
KV namespaces now support the
list()
method which returns a paginated list of keys: https://developers.cloudflare.com/workers/reference/storage/listing-keys/.This method is missing from the
KeyValueStore
class.The text was updated successfully, but these errors were encountered: