Skip to content

Commit

Permalink
Node: added ZMSCORE command (valkey-io#1987)
Browse files Browse the repository at this point in the history
* Node: added ZMSCORE command

Signed-off-by: aaron-congo <[email protected]>
Signed-off-by: Chloe Yip <[email protected]>
  • Loading branch information
aaron-congo authored and cyip10 committed Jul 23, 2024
1 parent 15b6f0f commit 8cdb26f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Node: Added SUNION command ([#1919](https:/valkey-io/valkey-glide/pull/1919))
* Node: Added SMISMEMBER command ([#1955](https:/valkey-io/valkey-glide/pull/1955))
* Node: Added SDIFF command ([#1924](https:/valkey-io/valkey-glide/pull/1924))
* Node: Added ZMSCORE command ([#1987](https:/valkey-io/valkey-glide/pull/1987))
* Node: Added LOLWUT command ([#1934](https:/valkey-io/valkey-glide/pull/1934))
* Node: Added LPOS command ([#1927](https:/valkey-io/valkey-glide/pull/1927))
* Node: Added FUNCTION LOAD command ([#1969](https:/valkey-io/valkey-glide/pull/1969))
Expand Down
23 changes: 23 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import {
createZDiffWithScores,
createZInterCard,
createZInterstore,
createZMScore,
createZPopMax,
createZPopMin,
createZRange,
Expand Down Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> =
/**
* Positive infinity bound for sorted set.
Expand Down
18 changes: 18 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import {
createZDiffWithScores,
createZInterCard,
createZInterstore,
createZMScore,
createZPopMax,
createZPopMin,
createZRange,
Expand Down Expand Up @@ -1291,6 +1292,23 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
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.
*
Expand Down
49 changes: 49 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,55 @@ export function runBaseTests<Context>(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) => {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 8cdb26f

Please sign in to comment.