Skip to content

Commit

Permalink
added hmget command in node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan Wattad authored Oct 5, 2023
1 parent fb2c86b commit 873636d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
13 changes: 13 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
createGet,
createHDel,
createHGet,
createHMGet,
createHSet,
createIncr,
createIncrBy,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
14 changes: 14 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createGet,
createHDel,
createHGet,
createHMGet,
createHSet,
createIncr,
createIncrBy,
Expand Down Expand Up @@ -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.
*
Expand Down
27 changes: 27 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type BaseClient = {
) => Promise<number>;
hget: (key: string, field: string) => Promise<string | null>;
hdel: (key: string, fields: string[]) => Promise<number>;
hmget: (key: string, fields: string[]) => Promise<(string | null)[]>;
customCommand: (commandName: string, args: string[]) => Promise<ReturnType>;
};

Expand Down Expand Up @@ -528,6 +529,32 @@ export function runBaseTests<Context>(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<Context>(config: {
Expand Down
14 changes: 13 additions & 1 deletion node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}

0 comments on commit 873636d

Please sign in to comment.