From 873636d613397b5e1cb01c8cf73bbe7302044fa9 Mon Sep 17 00:00:00 2001 From: Adan Wattad Date: Thu, 5 Oct 2023 17:55:34 +0300 Subject: [PATCH] added hmget command in node. --- node/src/BaseClient.ts | 13 +++++++++++++ node/src/Commands.ts | 7 +++++++ node/src/Transaction.ts | 14 ++++++++++++++ node/tests/SharedTests.ts | 27 +++++++++++++++++++++++++++ node/tests/TestUtilities.ts | 14 +++++++++++++- 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index fa25e9ca00..808840d57a 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -13,6 +13,7 @@ import { createGet, createHDel, createHGet, + createHMGet, createHSet, createIncr, createIncrBy, @@ -434,6 +435,18 @@ export class BaseClient { return this.createWritePromise(createHDel(key, fields)); } + /** Returns the values associated with the specified fields in the hash stored at key. + * + * @param key - The key of the hash. + * @param fields - The fields in the hash stored at key to retrieve from the database. + * @returns a list of values associated with the given fields, in the same order as they are requested. + * For every field that does not exist in the hash, a null value is returned. + * If key does not exist, it is treated as an empty hash and it returns a list of null values. + */ + public hmget(key: string, fields: string[]): Promise<(string | null)[]> { + return this.createWritePromise(createHMGet(key, fields)); + } + private readonly MAP_READ_FROM_REPLICA_STRATEGY: Record< ReadFromReplicaStrategy, connection_request.ReadFromReplicaStrategy diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 36e570fb89..335208bb89 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -319,6 +319,13 @@ export function createHDel( return createCommand(RequestType.HashDel, [key].concat(fields)); } +export function createHMGet( + key: string, + fields: string[] +): redis_request.Command { + return createCommand(RequestType.HashMGet, [key].concat(fields)); +} + export function createCustomCommand(commandName: string, args: string[]) { return createCommand(RequestType.CustomCommand, [commandName, ...args]); } diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index c48c9d89df..20ec77b4dd 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -14,6 +14,7 @@ import { createGet, createHDel, createHGet, + createHMGet, createHSet, createIncr, createIncrBy, @@ -298,6 +299,19 @@ export class BaseTransaction { this.commands.push(createHDel(key, fields)); } + /** Returns the values associated with the specified fields in the hash stored at key. + * + * @param key - The key of the hash. + * @param fields - The fields in the hash stored at key to retrieve from the database. + * + * Command Response - a list of values associated with the given fields, in the same order as they are requested. + * For every field that does not exist in the hash, a null value is returned. + * If key does not exist, it is treated as an empty hash and it returns a list of null values. + */ + public hmget(key: string, fields: string[]) { + this.commands.push(createHMGet(key, fields)); + } + /** 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 f84d8d7273..6429fbd95f 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -34,6 +34,7 @@ type BaseClient = { ) => Promise; hget: (key: string, field: string) => Promise; hdel: (key: string, fields: string[]) => Promise; + hmget: (key: string, fields: string[]) => Promise<(string | null)[]>; customCommand: (commandName: string, args: string[]) => Promise; }; @@ -528,6 +529,32 @@ export function runBaseTests(config: { }, config.timeout ); + + it( + "testing hmget with multiple existing fields, an non existing field and an non existing key", + async () => { + await runTest(async (client: BaseClient) => { + const key = uuidv4(); + const nonExistingKey = uuidv4(); + const field1 = uuidv4(); + const field2 = uuidv4(); + const nonExistingField = uuidv4(); + const value = uuidv4(); + const fieldValueMap = { + [field1]: value, + [field2]: value, + }; + expect(await client.hset(key, fieldValueMap)).toEqual(2); + expect( + await client.hmget(key, [field1, nonExistingField, field2]) + ).toEqual([value, null, value]); + expect( + await client.hmget(nonExistingKey, [field1, field2]) + ).toEqual([null, null]); + }); + }, + config.timeout + ); } export function runCommonTests(config: { diff --git a/node/tests/TestUtilities.ts b/node/tests/TestUtilities.ts index 26e65127e9..97b4b26173 100644 --- a/node/tests/TestUtilities.ts +++ b/node/tests/TestUtilities.ts @@ -67,5 +67,17 @@ export function transactionTest( baseTransaction.hset(key4, { [field]: value }); baseTransaction.hget(key4, field); baseTransaction.hdel(key4, [field]); - return ["OK", null, ["bar", "baz"], "OK", ["bar", "baz"], 1, 1, value, 1]; + baseTransaction.hmget(key4, [field]); + return [ + "OK", + null, + ["bar", "baz"], + "OK", + ["bar", "baz"], + 1, + 1, + value, + 1, + [null], + ]; }