From 940f2d2fdda896a15c8b7f65ed2cfec46f94fcd5 Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Thu, 20 Jun 2024 10:46:06 -0500 Subject: [PATCH] [FEAT] added support for listing keys with multiple filters (client already supported a single filter, this enables the possibility of specifying additional keys) --- jetstream/kv.ts | 5 +++-- jetstream/tests/kv_test.ts | 35 +++++++++++++++++++++++++++++++++++ jetstream/types.ts | 4 ++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/jetstream/kv.ts b/jetstream/kv.ts index 65e9cecd..1199d488 100644 --- a/jetstream/kv.ts +++ b/jetstream/kv.ts @@ -858,12 +858,13 @@ export class Bucket implements KV, KvRemove { return qi; } - async keys(k = ">"): Promise> { + async keys(k: string | string[] = ">"): Promise> { const keys = new QueuedIteratorImpl(); const cc = this._buildCC(k, KvWatchInclude.LastValue, { headers_only: true, }); - const subj = cc.filter_subject!; + + const subj = Array.isArray(k) ? ">" : cc.filter_subject!; const copts = consumerOpts(cc); copts.bindStream(this.stream); copts.orderedConsumer(); diff --git a/jetstream/tests/kv_test.ts b/jetstream/tests/kv_test.ts index 795357f2..c3d6b94b 100644 --- a/jetstream/tests/kv_test.ts +++ b/jetstream/tests/kv_test.ts @@ -2175,3 +2175,38 @@ Deno.test("kv - maxBucketSize doesn't override max_bytes", async () => { assertEquals(info.max_bytes, 100); await cleanup(ns, nc); }); + +Deno.test("kv - keys filter", async () => { + const { ns, nc } = await setup( + jetstreamServerConf({}), + ); + if (await notCompatible(ns, nc, "2.6.3")) { + return; + } + const js = nc.jetstream(); + const b = await js.views.kv(nuid.next()); + await Promise.all([b.put("A", "a"), b.put("B", "b"), b.put("C", "c")]); + + const buf = []; + for await (const e of await b.keys()) { + buf.push(e); + } + assertEquals(buf.length, 3); + assertArrayIncludes(buf, ["A", "B", "C"]); + + buf.length = 0; + for await (const e of await b.keys("A")) { + buf.push(e); + } + assertEquals(buf.length, 1); + assertArrayIncludes(buf, ["A"]); + + buf.length = 0; + for await (const e of await b.keys(["A", "C"])) { + buf.push(e); + } + assertEquals(buf.length, 2); + assertArrayIncludes(buf, ["A", "C"]); + + await cleanup(ns, nc); +}); diff --git a/jetstream/types.ts b/jetstream/types.ts index 4bfc1761..2acc0e89 100644 --- a/jetstream/types.ts +++ b/jetstream/types.ts @@ -1201,9 +1201,9 @@ export interface RoKV { /** * Returns an iterator of all the keys optionally matching * the specified filter. - * @param filter + * @param filter default to all keys */ - keys(filter?: string): Promise>; + keys(filter?: string | string[]): Promise>; } export interface KV extends RoKV {