Skip to content

Commit

Permalink
Added zcard command in node. (#885)
Browse files Browse the repository at this point in the history
* Added zcard command in node.

* Update the zadd and zaddincr docs.
  • Loading branch information
Adan Wattad authored Feb 1, 2024
1 parent 7fdba0f commit c7e8527
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
15 changes: 15 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
createTTL,
createUnlink,
createZadd,
createZcard,
createZrem,
} from "./Commands";
import {
Expand Down Expand Up @@ -988,6 +989,7 @@ export class BaseClient {
* @param changed - Modify the return value from the number of new elements added, to the total number of elements changed.
* @returns The number of elements added to the sorted set.
* If `changed` is set, returns the number of elements updated in the sorted set.
* If `key` holds a value that is not a sorted set, an error is returned.
*
* @example
* await zadd("mySortedSet", \{ "member1": 10.5, "member2": 8.2 \})
Expand Down Expand Up @@ -1024,6 +1026,7 @@ export class BaseClient {
* @param options - The Zadd options.
* @returns The score of the member.
* If there was a conflict with the options, the operation aborts and null is returned.
* If `key` holds a value that is not a sorted set, an error is returned.
*
* @example
* await zaddIncr("mySortedSet", member , 5.0)
Expand Down Expand Up @@ -1057,6 +1060,18 @@ 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.
* If `key` does not exist, it is treated as an empty sorted set, and this command returns 0.
* If `key` holds a value that is not a sorted set, an error is returned.
*/
public zcard(key: string): Promise<number> {
return this.createWritePromise(createZcard(key));
}

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 @@ -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]);
}
16 changes: 16 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
createTTL,
createUnlink,
createZadd,
createZcard,
createZrem,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";
Expand Down Expand Up @@ -714,6 +715,7 @@ export class BaseTransaction {
*
* Command Response - The number of elements added to the sorted set.
* If `changed` is set, returns the number of elements updated in the sorted set.
* If `key` holds a value that is not a sorted set, an error is returned.
*/
public zadd(
key: string,
Expand Down Expand Up @@ -743,6 +745,7 @@ export class BaseTransaction {
*
* Command Response - The score of the member.
* If there was a conflict with the options, the operation aborts and null is returned.
* If `key` holds a value that is not a sorted set, an error is returned.
*/
public zaddIncr(
key: string,
Expand Down Expand Up @@ -770,6 +773,19 @@ 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.
* If `key` does not exist, it is treated as an empty sorted set, and this command returns 0.
* If `key` holds a value that is not a sorted set, an error is returned.
*/
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.
*
Expand Down
15 changes: 15 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,21 @@ export function runBaseTests<Context>(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<Context>(config: {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -119,6 +120,7 @@ export function transactionTest(
2,
3,
1,
1,
];
}

Expand Down

0 comments on commit c7e8527

Please sign in to comment.