kitsonk / kv-toolbox

Utilities for working with Deno KV 🦕🗝️

Home Page:https://kview.deno.dev/kv-toolbox

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Unique Prefix set

nhrones opened this issue · comments

commented

I'm using this method to populate a treeView of keys -> leafs -> values, in a utility UI.

/** 
 * returns a list of unique key-tuple prefixes 
 * an empty tuple (array) returns all `root` prefixes 
 */
export async function prefixes(prefix?: any): Promise<Set<any>> {
   const pre = (prefix) ? [prefix] : []
   let keys: Set<any> = new Set()
   const entries = connection.list({prefix:pre})
   for await (const entry of entries) {
      keys.add(entry.key[0]) 
   }
   return keys
}

I discovered an empty tuple returns all root prefixes from KV table.

The challenge is that not all Deno.KvKeyParts are primitive values within JavaScript, so your code would work as long as you don't use Uint8Array which is a valid KvKeyPart, so you would end up with an entry for every key, so if the key part is Uint8Array uniques would only be available by comparing any byte arrays in the set by iterating over it.

But I think I can add something that works in every situation.