Skip to content

Commit

Permalink
Added zcard command in node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Feb 1, 2024
1 parent 2ea5d6c commit 90fde6f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
13 changes: 13 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 @@ -1057,6 +1058,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]);
}
14 changes: 14 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 @@ -770,6 +771,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 90fde6f

Please sign in to comment.