Sorted Set accessors
To access Sorted Set values in a KV Store. First create a KV Instance using KvStore.open()
This instance provides zrangeByScore / zrangeByScoreEntries to
retrieve values within a score range, and zscan / zscanEntries to
match values by prefix. The *Entries variants return KvStoreEntry
wrappers with text() / json() / arrayBuffer() decode helpers; the
non-entry variants return raw ArrayBuffer values.
zrangeByScore
Section titled “zrangeByScore”import { KvStore } from 'fastedge::kv';
async function eventHandler(event) { try { const myStore = KvStore.open('kv-store-name-as-defined-on-app'); const results = myStore.zrangeByScore('key', 0, 10); return new Response(`The KV Store responded with: ${JSON.stringify(results)}`); } catch (error) { return Response.json({ error: error.message }, { status: 500 }); }}
addEventListener('fetch', (event) => { event.respondWith(eventHandler(event));});storeInstance.zrangeByScore(key, min, max);Parameters
Section titled “Parameters”-
key(required)A string containing the key you want to retrieve the values from.
-
min(required)A number representing the min-score for which to return values for.
-
max(required)A number representing the max-score for which to return values for.
Return Value
Section titled “Return Value”An Array<[ArrayBuffer, number]>. It returns a list of tuples, containing the value in an
ArrayBuffer and the score as a number.
zrangeByScoreEntries
Section titled “zrangeByScoreEntries”import { KvStore } from 'fastedge::kv';
async function eventHandler(event) { try { const myStore = KvStore.open('kv-store-name-as-defined-on-app'); const results = await myStore.zrangeByScoreEntries('key', 0, 10); const decoded = await Promise.all( results.map(async ([entry, score]) => [await entry.text(), score]), ); return new Response(`The KV Store responded with: ${JSON.stringify(decoded)}`); } catch (error) { return Response.json({ error: error.message }, { status: 500 }); }}
addEventListener('fetch', (event) => { event.respondWith(eventHandler(event));});storeInstance.zrangeByScoreEntries(key, min, max);Parameters
Section titled “Parameters”Same as zrangeByScore.
Return Value
Section titled “Return Value”A Promise<Array<[KvStoreEntry, number]>>. Each tuple contains a
KvStoreEntry (with text() / json() / arrayBuffer() accessors)
and the score as a number. Resolves to an empty array if no values
match.
import { KvStore } from 'fastedge::kv';
async function eventHandler(event) { try { const myStore = KvStore.open('kv-store-name-as-defined-on-app'); const results = myStore.zscan('pre*'); return new Response(`The KV Store responded with: ${JSON.stringify(results)}`); } catch (error) { return Response.json({ error: error.message }, { status: 500 }); }}
addEventListener('fetch', (event) => { event.respondWith(eventHandler(event));});storeInstance.zscan(key, pattern);Parameters
Section titled “Parameters”-
key(required)A string containing the key you want to retrieve the values from.
-
pattern(required)A string containing the prefix pattern match.
Note: This is a prefix match, it must contain the wildcard *. In the given example it matches
all values that start with pre
Return Value
Section titled “Return Value”An Array<[ArrayBuffer, number]>. It returns a list of tuples, containing the value in an
ArrayBuffer and the score as a number.
zscanEntries
Section titled “zscanEntries”import { KvStore } from 'fastedge::kv';
async function eventHandler(event) { try { const myStore = KvStore.open('kv-store-name-as-defined-on-app'); const results = await myStore.zscanEntries('key', 'pre*'); const decoded = await Promise.all( results.map(async ([entry, score]) => [await entry.text(), score]), ); return new Response(`The KV Store responded with: ${JSON.stringify(decoded)}`); } catch (error) { return Response.json({ error: error.message }, { status: 500 }); }}
addEventListener('fetch', (event) => { event.respondWith(eventHandler(event));});storeInstance.zscanEntries(key, pattern);Parameters
Section titled “Parameters”Same as zscan.
Return Value
Section titled “Return Value”A Promise<Array<[KvStoreEntry, number]>>. Each tuple contains a
KvStoreEntry (with text() / json() / arrayBuffer() accessors)
and the score as a number. Resolves to an empty array if no values
match.