Skip to content

Commit

Permalink
Node: added sismember command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan Wattad authored Mar 7, 2024
1 parent ad02e64 commit 0f7db1a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* Python Node: Allow routing Cluster requests by address. ([#1021](https://github.com/aws/glide-for-redis/pull/1021))
* Python: Added HSETNX command. ([#954](https://github.com/aws/glide-for-redis/pull/954))
* Python: Added SISMEMBER command ([#971](https://github.com/aws/glide-for-redis/pull/971))
* Python, Node: Added SISMEMBER command ([#972](https://github.com/aws/glide-for-redis/pull/972), [#1083](https://github.com/aws/glide-for-redis/pull/1083))
* Python, Node: Added TYPE command ([#945](https://github.com/aws/glide-for-redis/pull/945), [#980](https://github.com/aws/glide-for-redis/pull/980))
* Python, Node: Added HLEN command ([#944](https://github.com/aws/glide-for-redis/pull/944), [#981](https://github.com/aws/glide-for-redis/pull/981))
* Python, Node: Added ZCOUNT command ([#878](https://github.com/aws/glide-for-redis/pull/878)) ([#909](https://github.com/aws/glide-for-redis/pull/909))
Expand Down
13 changes: 13 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
createSMembers,
createSRem,
createSet,
createSismember,
createStrlen,
createTTL,
createType,
Expand Down Expand Up @@ -827,6 +828,18 @@ export class BaseClient {
return this.createWritePromise(createSCard(key));
}

/** Returns if `member` is a member of the set stored at `key`.
* See https://redis.io/commands/sismember/ for more details.
*
* @param key - The key of the set.
* @param member - The member to check for existence in the set.
* @returns `true` if the member exists in the set, `false` otherwise.
* If `key` doesn't exist, it is treated as an empty set and the command returns `false`.
*/
public sismember(key: string, member: string): Promise<boolean> {
return this.createWritePromise(createSismember(key, member));
}

/** Returns the number of keys in `keys` that exist in the database.
* See https://redis.io/commands/exists/ for 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 @@ -545,6 +545,16 @@ export function createSCard(key: string): redis_request.Command {
return createCommand(RequestType.SCard, [key]);
}

/**
* @internal
*/
export function createSismember(
key: string,
member: string,
): redis_request.Command {
return createCommand(RequestType.SIsMember, [key, member]);
}

/**
* @internal
*/
Expand Down
14 changes: 14 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
createSRem,
createSelect,
createSet,
createSismember,
createStrlen,
createTTL,
createType,
Expand Down Expand Up @@ -636,6 +637,19 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createSCard(key));
}

/** Returns if `member` is a member of the set stored at `key`.
* See https://redis.io/commands/sismember/ for more details.
*
* @param key - The key of the set.
* @param member - The member to check for existence in the set.
*
* Command Response - `true` if the member exists in the set, `false` otherwise.
* If `key` doesn't exist, it is treated as an empty set and the command returns `false`.
*/
public sismember(key: string, member: string): T {
return this.addAndReturn(createSismember(key, member));
}

/** Returns the number of keys in `keys` that exist in the database.
* See https://redis.io/commands/exists/ for details.
*
Expand Down
24 changes: 24 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,30 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`sismember test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = uuidv4();
const key2 = uuidv4();
expect(await client.sadd(key1, ["member1"])).toEqual(1);
expect(await client.sismember(key1, "member1")).toEqual(true);
expect(
await client.sismember(key1, "nonExistingMember"),
).toEqual(false);
expect(
await client.sismember("nonExistingKey", "member1"),
).toEqual(false);

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

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`exists with existing keys, an non existing key_%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 @@ -135,6 +135,8 @@ export function transactionTest(
args.push(1);
baseTransaction.scard(key7);
args.push(1);
baseTransaction.sismember(key7, "bar");
args.push(true);
baseTransaction.smembers(key7);
args.push(["bar"]);
baseTransaction.zadd(key8, {
Expand Down

0 comments on commit 0f7db1a

Please sign in to comment.