Skip to content

Commit

Permalink
Node: Added hlen command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Feb 16, 2024
1 parent 10cdf60 commit 033389a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
createHGetAll,
createHIncrBy,
createHIncrByFloat,
createHLen,
createHMGet,
createHSet,
createIncr,
Expand Down Expand Up @@ -616,6 +617,16 @@ export class BaseClient {
return this.createWritePromise(createHIncrByFloat(key, field, amount));
}

/** Returns the number of fields contained in the hash stored at `key`.
* See https://redis.io/commands/hlen/ for more details.
*
* @param key - The key of the hash.
* @returns The number of fields in the hash, or 0 when the key does not exist.
*/
public hlen(key: string): Promise<number> {
return this.createWritePromise(createHLen(key));
}

/** Inserts all the specified values at the head of the list stored at `key`.
* `elements` are inserted one after the other to the head of the list, from the leftmost element to the rightmost element.
* If `key` does not exist, it is created as empty list before performing the push operations.
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,13 @@ export function createHIncrByFloat(
]);
}

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

/**
* @internal
*/
Expand Down
12 changes: 12 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
createHGetAll,
createHIncrBy,
createHIncrByFloat,
createHLen,
createHMGet,
createHSet,
createIncr,
Expand Down Expand Up @@ -409,6 +410,17 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createHIncrByFloat(key, field, amount));
}

/** Returns the number of fields contained in the hash stored at `key`.
* See https://redis.io/commands/hlen/ for more details.
*
* @param key - The key of the hash.
*
* Command Response - The number of fields in the hash, or 0 when the key does not exist.
*/
public hlen(key: string): T {
return this.addAndReturn(createHLen(key));
}

/** Inserts all the specified values at the head of the list stored at `key`.
* `elements` are inserted one after the other to the head of the list, from the leftmost element to the rightmost element.
* If `key` does not exist, it is created as empty list before performing the push operations.
Expand Down
22 changes: 22 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,28 @@ export function runBaseTests<Context>(config: {
config.timeout
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`hlen test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = uuidv4();
const field1 = uuidv4();
const field2 = uuidv4();
const fieldValueMap = {
[field1]: "value1",
[field2]: "value2",
};

expect(await client.hset(key1, fieldValueMap)).toEqual(2);
expect(await client.hlen(key1)).toEqual(2);
expect(await client.hdel(key1, [field1])).toEqual(1);
expect(await client.hlen(key1)).toEqual(1);
expect(await client.hlen("nonExistingHash")).toEqual(0);
}, protocol);
},
config.timeout
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`lpush, lpop and lrange with existing and 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 @@ -82,6 +82,8 @@ export function transactionTest(
args.push(1);
baseTransaction.hset(key4, { [field]: value });
args.push(1);
baseTransaction.hlen(key4);
args.push(1);
baseTransaction.hget(key4, field);
args.push(value);
baseTransaction.hgetall(key4);
Expand Down

0 comments on commit 033389a

Please sign in to comment.