Skip to content

Commit

Permalink
Added zscore command in node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Feb 14, 2024
1 parent 7b4c2f0 commit b8da24c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
15 changes: 15 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
createZadd,
createZcard,
createZrem,
createZscore,
} from "./Commands";
import {
ClosingError,
Expand Down Expand Up @@ -1062,6 +1063,20 @@ export class BaseClient {
return this.createWritePromise(createZcard(key));
}

/** Returns the score of `member` in the sorted set stored at `key`.
* See https://redis.io/commands/zscore/ for more details.
*
* @param key - The key of the sorted set.
* @param member - The member whose score is to be retrieved.
* @returns The score of the member.
* If `member` does not exist in the sorted set, null is returned.
* If `key` does not exist, null is returned.
* If `key` holds a value that is not a sorted set, an error is returned.
*/
public zscore(key: string, member: string): Promise<number | null> {
return this.createWritePromise(createZscore(key, member));
}

private readonly MAP_READ_FROM_STRATEGY: Record<
ReadFrom,
connection_request.ReadFrom
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,10 @@ export function createZrem(
export function createZcard(key: string): redis_request.Command {
return createCommand(RequestType.Zcard, [key]);
}

/**
* @internal
*/
export function createZscore(key: string, member: string): redis_request.Command {
return createCommand(RequestType.ZScore, [key, member]);
}
16 changes: 16 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
createZadd,
createZcard,
createZrem,
createZscore,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -821,6 +822,21 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createZcard(key));
}

/** Returns the score of `member` in the sorted set stored at `key`.
* See https://redis.io/commands/zscore/ for more details.
*
* @param key - The key of the sorted set.
* @param member - The member whose score is to be retrieved.
*
* Command Response - The score of the member.
* If `member` does not exist in the sorted set, null is returned.
* If `key` does not exist, null is returned.
* If `key` holds a value that is not a sorted set, an error is returned.
*/
public zscore(key: string, member: string) {
this.commands.push(createZscore(key, member));
}

/** Executes a single command, without checking inputs. Every part of the command, including subcommands,
* should be added as a separate value in args.
*
Expand Down
24 changes: 24 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,30 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);


it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`zscore test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = uuidv4();
const key2 = uuidv4();
const membersScores = { one: 1, two: 2, three: 3 };
expect(await client.zadd(key1, membersScores)).toEqual(3);
expect(await client.zscore(key1, "one")).toEqual(1.0);
expect(await client.zscore(key1, "nonExistingMember")).toEqual(
null
);
expect(
await client.zscore("nonExistingKey", "nonExistingMember")
).toEqual(null);

expect(await client.set(key2, "foo")).toEqual("OK");
await expect(client.zscore(key2, "foo")).rejects.toThrow();
}, protocol);
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down
4 changes: 3 additions & 1 deletion node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export function transactionTest(
.zadd(key8, { member1: 1, member2: 2 })
.zaddIncr(key8, "member2", 1)
.zrem(key8, ["member1"])
.zcard(key8);
.zcard(key8)
.zscore(key8, "member2");
return [
"OK",
null,
Expand Down Expand Up @@ -127,6 +128,7 @@ export function transactionTest(
3,
1,
1,
3.0,
];
}

Expand Down

0 comments on commit b8da24c

Please sign in to comment.