Skip to content

Commit

Permalink
added exists command in node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Oct 24, 2023
1 parent a0aa119 commit 5a57c1a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
createDecr,
createDecrBy,
createDel,
createExists,
createGet,
createHDel,
createHExists,
Expand Down Expand Up @@ -659,6 +660,17 @@ export class BaseClient {
return this.createWritePromise(createSCard(key));
}

/** Returns if the keys in `keys` exist.
* See https://redis.io/commands/exists/ for details.
*
* @param keys - The keys list to check.
* @returns the number of keys that exist. If the same existing key is mentioned in `keys` multiple times,
* it will be counted multiple times.
*/
public exists(keys: string[]): Promise<number> {
return this.createWritePromise(createExists(keys));
}

private readonly MAP_READ_FROM_REPLICA_STRATEGY: Record<
ReadFromReplicaStrategy,
connection_request.ReadFromReplicaStrategy
Expand Down
4 changes: 4 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,7 @@ export function createHIncrByFloat(
amount.toString(),
]);
}

export function createExists(keys: string[]): redis_request.Command {
return createCommand(RequestType.Exists, keys);
}
13 changes: 13 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createDecr,
createDecrBy,
createDel,
createExists,
createGet,
createHDel,
createHExists,
Expand Down Expand Up @@ -522,6 +523,18 @@ export class BaseTransaction {
this.commands.push(createSCard(key));
}

/** Returns if the keys in `keys` exist.
* See https://redis.io/commands/exists/ for details.
*
* @param keys - The keys list to check.
*
* Command Response - the number of keys that exist. If the same existing key is mentioned in `keys` multiple times,
* it will be counted multiple times.
*/
public exists(keys: string[]) {
this.commands.push(createExists(keys));
}

/** 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
20 changes: 20 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type BaseClient = {
srem: (key: string, members: string[]) => Promise<number>;
smembers: (key: string) => Promise<string[]>;
scard: (key: string) => Promise<number>;
exists: (keys: string[]) => Promise<number>;
customCommand: (commandName: string, args: string[]) => Promise<ReturnType>;
};

Expand Down Expand Up @@ -881,6 +882,25 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);

it(
"exists with existing keys, an non existing key",
async () => {
await runTest(async (client: BaseClient) => {
const key1 = uuidv4();
const key2 = uuidv4();
const value = uuidv4();
expect(await client.set(key1, value)).toEqual("OK");
expect(await client.exists([key1])).toEqual(1);
expect(await client.set(key2, value)).toEqual("OK");
expect(
await client.exists([key1, "nonExistingKey", key2])
).toEqual(2);
expect(await client.exists([key1, key1])).toEqual(2);
});
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down

0 comments on commit 5a57c1a

Please sign in to comment.