From 8cdb26fb222478fc447324a3f8ded533cab37b63 Mon Sep 17 00:00:00 2001 From: Aaron <69273634+aaron-congo@users.noreply.github.com> Date: Fri, 19 Jul 2024 18:39:48 -0700 Subject: [PATCH] Node: added ZMSCORE command (#1987) * Node: added ZMSCORE command Signed-off-by: aaron-congo Signed-off-by: Chloe Yip --- CHANGELOG.md | 1 + node/src/BaseClient.ts | 23 +++++++++++++++++ node/src/Commands.ts | 10 ++++++++ node/src/Transaction.ts | 18 ++++++++++++++ node/tests/SharedTests.ts | 49 +++++++++++++++++++++++++++++++++++++ node/tests/TestUtilities.ts | 2 ++ 6 files changed, 103 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dd54d8989..10456b9734 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * Node: Added SUNION command ([#1919](https://github.com/valkey-io/valkey-glide/pull/1919)) * Node: Added SMISMEMBER command ([#1955](https://github.com/valkey-io/valkey-glide/pull/1955)) * Node: Added SDIFF command ([#1924](https://github.com/valkey-io/valkey-glide/pull/1924)) +* Node: Added ZMSCORE command ([#1987](https://github.com/valkey-io/valkey-glide/pull/1987)) * Node: Added LOLWUT command ([#1934](https://github.com/valkey-io/valkey-glide/pull/1934)) * Node: Added LPOS command ([#1927](https://github.com/valkey-io/valkey-glide/pull/1927)) * Node: Added FUNCTION LOAD command ([#1969](https://github.com/valkey-io/valkey-glide/pull/1969)) diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index 2f567f951a..717525bc55 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -114,6 +114,7 @@ import { createZDiffWithScores, createZInterCard, createZInterstore, + createZMScore, createZPopMax, createZPopMin, createZRange, @@ -2488,6 +2489,28 @@ export class BaseClient { return this.createWritePromise(createZScore(key, member)); } + /** + * Returns the scores associated with the specified `members` in the sorted set stored at `key`. + * + * See https://valkey.io/commands/zmscore/ for more details. + * + * @param key - The key of the sorted set. + * @param members - A list of members in the sorted set. + * @returns An `array` of scores corresponding to `members`. + * If a member does not exist in the sorted set, the corresponding value in the list will be `null`. + * + * since Valkey version 6.2.0. + * + * @example + * ```typescript + * const result = await client.zmscore("zset1", ["member1", "non_existent_member", "member2"]); + * console.log(result); // Output: [1.0, null, 2.0] - "member1" has a score of 1.0, "non_existent_member" does not exist in the sorted set, and "member2" has a score of 2.0. + * ``` + */ + public zmscore(key: string, members: string[]): Promise<(number | null)[]> { + return this.createWritePromise(createZMScore(key, members)); + } + /** Returns the number of members in the sorted set stored at `key` with scores between `minScore` and `maxScore`. * See https://valkey.io/commands/zcount/ for more details. * diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 4381041177..871a58fc63 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -1109,6 +1109,16 @@ export function createZScore( return createCommand(RequestType.ZScore, [key, member]); } +/** + * @internal + */ +export function createZMScore( + key: string, + members: string[], +): command_request.Command { + return createCommand(RequestType.ZMScore, [key, ...members]); +} + export type ScoreBoundary = /** * Positive infinity bound for sorted set. diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index 4ff5ba927c..e1f68b9d38 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -126,6 +126,7 @@ import { createZDiffWithScores, createZInterCard, createZInterstore, + createZMScore, createZPopMax, createZPopMin, createZRange, @@ -1291,6 +1292,23 @@ export class BaseTransaction> { return this.addAndReturn(createZScore(key, member)); } + /** + * Returns the scores associated with the specified `members` in the sorted set stored at `key`. + * + * See https://valkey.io/commands/zmscore/ for more details. + * + * @param key - The key of the sorted set. + * @param members - A list of members in the sorted set. + * + * Command Response - An `array` of scores corresponding to `members`. + * If a member does not exist in the sorted set, the corresponding value in the list will be `null`. + * + * since Valkey version 6.2.0. + */ + public zmscore(key: string, members: string[]): T { + return this.addAndReturn(createZMScore(key, members)); + } + /** Returns the number of members in the sorted set stored at `key` with scores between `minScore` and `maxScore`. * See https://valkey.io/commands/zcount/ for more details. * diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index 27370137ff..76b429e853 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -2429,6 +2429,55 @@ export function runBaseTests(config: { config.timeout, ); + it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( + `zmscore test_%p`, + async (protocol) => { + await runTest(async (client: BaseClient) => { + if (await checkIfServerVersionLessThan("6.2.0")) { + return; + } + + const key1 = `{key}-${uuidv4()}`; + const nonExistingKey = `{key}-${uuidv4()}`; + const stringKey = `{key}-${uuidv4()}`; + + const entries = { + one: 1.0, + two: 2.0, + three: 3.0, + }; + expect(await client.zadd(key1, entries)).toEqual(3); + + expect( + await client.zmscore(key1, ["one", "three", "two"]), + ).toEqual([1.0, 3.0, 2.0]); + expect( + await client.zmscore(key1, [ + "one", + "nonExistingMember", + "two", + "nonExistingMember", + ]), + ).toEqual([1.0, null, 2.0, null]); + expect(await client.zmscore(nonExistingKey, ["one"])).toEqual([ + null, + ]); + + // invalid arg - member list must not be empty + await expect(client.zmscore(key1, [])).rejects.toThrow( + RequestError, + ); + + // key exists, but it is not a sorted set + checkSimple(await client.set(stringKey, "foo")).toEqual("OK"); + await expect( + client.zmscore(stringKey, ["one"]), + ).rejects.toThrow(RequestError); + }, protocol); + }, + config.timeout, + ); + it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `zcount test_%p`, async (protocol) => { diff --git a/node/tests/TestUtilities.ts b/node/tests/TestUtilities.ts index 70b4b853a1..d5be0ac2db 100644 --- a/node/tests/TestUtilities.ts +++ b/node/tests/TestUtilities.ts @@ -565,6 +565,8 @@ export async function transactionTest( args.push({ three: 3.5 }); baseTransaction.zdiffstore(key13, [key13, key13]); args.push(0); + baseTransaction.zmscore(key12, ["two", "one"]); + args.push([2.0, 1.0]); } baseTransaction.zinterstore(key12, [key12, key13]);