Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: adds HKEYS command #1499

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#### Changes
* Node: Added HKEYS command ([#1499](https://github.com/aws/glide-for-redis/pull/1499))
* Node: Added SMOVE command ([#1476](https://github.com/aws/glide-for-redis/pull/1476))
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
* Node: Added ZINTERSTORE command ([#1513](https://github.com/valkey-io/valkey-glide/pull/1513))
* Python: Added OBJECT ENCODING command ([#1471](https://github.com/valkey-io/valkey-glide/pull/1471))
* Python: Added OBJECT FREQ command ([#1472](https://github.com/valkey-io/valkey-glide/pull/1472))
Expand Down
1 change: 0 additions & 1 deletion node/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# ignore that dir, because there are a lot of files which we don't manage, e.g. json files in cargo crates
rust-client/*
*.md
# unignore specific files
!rust-client/package.json
!rust-client/tsconfig.json
19 changes: 19 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
createHGetAll,
createHIncrBy,
createHIncrByFloat,
createHKeys,
createHLen,
createHMGet,
createHSet,
Expand Down Expand Up @@ -722,6 +723,24 @@ export class BaseClient {
return this.createWritePromise(createHSet(key, fieldValueMap));
}

/**
* Returns all field names in the hash stored at `key`.
* See https://valkey.io/commands/hkeys/ for more details.
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved
*
* @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
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved
* 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 @@ -406,6 +406,13 @@ export function createHSet(
);
}

/**
* @internal
*/
export function createHKeys(key: string): command_request.Command {
return createCommand(RequestType.HKeys, [key]);
}

/**
* @internal
*/
Expand Down
13 changes: 13 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
createHGetAll,
createHIncrBy,
createHIncrByFloat,
createHKeys,
createHLen,
createHMGet,
createHSet,
Expand Down Expand Up @@ -390,6 +391,18 @@ 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://valkey.io/commands/hkeys/ for more details.
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved
*
* @param key - The key of the hash.
*
* Command Response - 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
29 changes: 29 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,35 @@ 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,
};

acarbonetto marked this conversation as resolved.
Show resolved Hide resolved
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");

await expect(client.hkeys(key2)).rejects.toThrow();
}, 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 @@ -340,6 +340,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
Loading