From 657674297f1e577acda3166e1b315d4103ee6604 Mon Sep 17 00:00:00 2001 From: Adan Date: Thu, 1 Feb 2024 12:03:05 +0000 Subject: [PATCH] Added zcard command in node. --- node/src/BaseClient.ts | 11 +++++++++++ node/src/Commands.ts | 7 +++++++ node/src/Transaction.ts | 12 ++++++++++++ node/tests/SharedTests.ts | 15 +++++++++++++++ node/tests/TestUtilities.ts | 2 ++ 5 files changed, 47 insertions(+) diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index a5e909f12f..908373fd67 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -52,6 +52,7 @@ import { createTTL, createUnlink, createZadd, + createZcard, createZrem, } from "./Commands"; import { @@ -1057,6 +1058,16 @@ export class BaseClient { return this.createWritePromise(createZrem(key, members)); } + /** Returns the cardinality (number of elements) of the sorted set stored at `key`. + * See https://redis.io/commands/zcard/ for more details. + * + * @param key - The key of the sorted set. + * @returns The number of elements in the sorted set. + */ + public zcard(key: string): Promise { + return this.createWritePromise(createZcard(key)); + } + private readonly MAP_READ_FROM_STRATEGY: Record< ReadFrom, connection_request.ReadFrom diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 64b4d6305b..ff2a0b8ab5 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -753,3 +753,10 @@ export function createZrem( ): redis_request.Command { return createCommand(RequestType.Zrem, [key].concat(members)); } + +/** + * @internal + */ +export function createZcard(key: string): redis_request.Command { + return createCommand(RequestType.Zcard, [key]); +} diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index 474a8550c9..996432908d 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -55,6 +55,7 @@ import { createTTL, createUnlink, createZadd, + createZcard, createZrem, } from "./Commands"; import { redis_request } from "./ProtobufMessage"; @@ -770,6 +771,17 @@ export class BaseTransaction { this.commands.push(createZrem(key, members)); } + /** Returns the cardinality (number of elements) of the sorted set stored at `key`. + * See https://redis.io/commands/zcard/ for more details. + * + * @param key - The key of the sorted set. + * + * Command Response - The number of elements in the sorted set. + */ + public zcard(key: string) { + this.commands.push(createZcard(key)); + } + /** Executes a single command, without checking inputs. Every part of the command, including subcommands, * should be added as a separate value in args. * diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index abeee908ca..942b24fbe8 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -1370,6 +1370,21 @@ export function runBaseTests(config: { }, config.timeout ); + + it( + "zcard test", + async () => { + await runTest(async (client: BaseClient) => { + const key = uuidv4(); + const membersScores = { one: 1, two: 2, three: 3 }; + expect(await client.zadd(key, membersScores)).toEqual(3); + expect(await client.zcard(key)).toEqual(3); + expect(await client.zrem(key, ["one"])).toEqual(1); + expect(await client.zcard(key)).toEqual(2); + }); + }, + config.timeout + ); } export function runCommonTests(config: { diff --git a/node/tests/TestUtilities.ts b/node/tests/TestUtilities.ts index 92dd7705cc..652509e79b 100644 --- a/node/tests/TestUtilities.ts +++ b/node/tests/TestUtilities.ts @@ -92,6 +92,7 @@ export function transactionTest( baseTransaction.zadd(key8, { member1: 1, member2: 2 }); baseTransaction.zaddIncr(key8, "member2", 1); baseTransaction.zrem(key8, ["member1"]); + baseTransaction.zcard(key8); return [ "OK", null, @@ -119,6 +120,7 @@ export function transactionTest( 2, 3, 1, + 1, ]; }