Skip to content

Commit

Permalink
adds HKEYS command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed May 30, 2024
1 parent fbbf797 commit c5dfbee
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
19 changes: 19 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
createHGetAll,
createHIncrBy,
createHIncrByFloat,
createHKeys,
createHLen,
createHMGet,
createHSet,
Expand Down Expand Up @@ -702,6 +703,24 @@ export class BaseClient {
return this.createWritePromise(createHSet(key, fieldValueMap));
}

/**
* Returns all field names in the hash stored at `key`.
* See https://redis.io/commands/hkeys/ for more details.
*
* @param key - The key of the hash.
* @returns A list of field names for the hash, or an empty list when the key does not exist.
*
* @example
* ```typescript
* // Example usage of the hkeys method
* const result = await client.hkeys("my_hash")
* console.log(result); // Output: ["field1", "field2", "field3"] - Returns all the field names stored in the hash "my_hash".
* ```
*/
public hkeys(key: string): Promise<string[]> {
return this.createWritePromise(createHKeys(key));
}

/** Sets `field` in the hash stored at `key` to `value`, only if `field` does not yet exist.
* If `key` does not exist, a new key holding a hash is created.
* If `field` already exists, this operation has no effect.
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ export function createHSet(
);
}

/**
* @internal
*/
export function createHKeys(key: string): redis_request.Command {
return createCommand(RequestType.HKeys, [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 @@ -37,6 +37,7 @@ import {
createHGetAll,
createHIncrBy,
createHIncrByFloat,
createHKeys,
createHLen,
createHMGet,
createHSet,
Expand Down Expand Up @@ -374,6 +375,17 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createHSet(key, fieldValueMap));
}

/**
* Returns all field names in the hash stored at `key`.
* See https://redis.io/commands/hkeys/ for more details.
*
* @param key - The key of the hash.
* @returns A list of field names for the hash, or an empty list when the key does not exist.
*/
public hkeys(key: string): T {
return this.addAndReturn(createHKeys(key));
}

/** Sets `field` in the hash stored at `key` to `value`, only if `field` does not yet exist.
* If `key` does not exist, a new key holding a hash is created.
* If `field` already exists, this operation has no effect.
Expand Down
34 changes: 34 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,40 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`testing hkeys with exiting, an non exising key and error request key_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
const key2 = uuidv4();
const field1 = uuidv4();
const field2 = uuidv4();
const value = uuidv4();
const value2 = uuidv4();
const fieldValueMap = {
[field1]: value,
[field2]: value2,
};

expect(await client.hset(key, fieldValueMap)).toEqual(2);
expect(await client.hkeys(key)).toEqual([field1, field2]);
expect(await client.hdel(key, [field1])).toEqual(1);
expect(await client.hkeys(key)).toEqual([field2]);
expect(await client.hkeys("nonExistingKey")).toEqual([]);

expect(await client.set(key2, value)).toEqual("OK");
try {
expect(await client.hkeys(key2)).toThrow();
} catch (e) {
expect((e as Error).message).toMatch(
"WRONGTYPE: Operation against a key holding the wrong kind of value",
);
}
}, protocol);
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`hdel multiple existing fields, an non existing field and 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 @@ -249,6 +249,8 @@ export async function transactionTest(
args.push(false);
baseTransaction.hvals(key4);
args.push([value]);
baseTransaction.hkeys(key4);
args.push([field]);
baseTransaction.hget(key4, field);
args.push(value);
baseTransaction.hgetall(key4);
Expand Down

0 comments on commit c5dfbee

Please sign in to comment.