Skip to content

Commit

Permalink
Merge branch 'main' into node/integ_cyip10_zunionstore
Browse files Browse the repository at this point in the history
Signed-off-by: Chloe Yip <[email protected]>
  • Loading branch information
cyip10 authored Aug 15, 2024
2 parents 48674ee + fc5a64b commit 8972411
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Java: Added binary support for custom command ([#2109](https://github.com/valkey-io/valkey-glide/pull/2109))
* Node: Added SSCAN command ([#2132](https://github.com/valkey-io/valkey-glide/pull/2132))
* Node: Added ZUNIONSTORE command ([#2145](https://github.com/valkey-io/valkey-glide/pull/2145))
* Node: Added HKEYS command ([#2136](https://github.com/aws/glide-for-redis/pull/2136))
* Node: Added FUNCTION KILL command ([#2114](https://github.com/valkey-io/valkey-glide/pull/2114))
* Node: Update all commands to use `async` ([#2110](https://github.com/valkey-io/valkey-glide/pull/2110))
* Node: Added XAUTOCLAIM command ([#2108](https://github.com/valkey-io/valkey-glide/pull/2108))
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
25 changes: 23 additions & 2 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import {
createHGetAll,
createHIncrBy,
createHIncrByFloat,
createHKeys,
createHLen,
createHMGet,
createHRandField,
Expand Down Expand Up @@ -1488,7 +1489,7 @@ export class BaseClient {
* @example
* ```typescript
* // Example usage of the hget method on an-existing field
* await client.hset("my_hash", "field");
* await client.hset("my_hash", {"field": "value"});
* const result = await client.hget("my_hash", "field");
* console.log(result); // Output: "value"
* ```
Expand Down Expand Up @@ -1516,7 +1517,7 @@ export class BaseClient {
* @example
* ```typescript
* // Example usage of the hset method
* const result = await client.hset("my_hash", \{"field": "value", "field2": "value2"\});
* const result = await client.hset("my_hash", {"field": "value", "field2": "value2"});
* console.log(result); // Output: 2 - Indicates that 2 fields were successfully set in the hash "my_hash".
* ```
*/
Expand All @@ -1527,6 +1528,26 @@ export class BaseClient {
return this.createWritePromise(createHSet(key, fieldValueMap));
}

/**
* Returns all field names in the hash stored at `key`.
*
* @see {@link https://valkey.io/commands/hkeys/|valkey.io} for 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:
* await client.hset("my_hash", {"field1": "value1", "field2": "value2", "field3": "value3"});
* 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 @@ -421,6 +421,13 @@ export function createHSet(
);
}

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

/**
* @internal
*/
Expand Down
14 changes: 14 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ import {
createHGetAll,
createHIncrBy,
createHIncrByFloat,
createHKeys,
createHLen,
createHMGet,
createHRandField,
Expand Down Expand Up @@ -743,6 +744,19 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createHSet(key, fieldValueMap));
}

/**
* Returns all field names in the hash stored at `key`.
*
* @see {@link https://valkey.io/commands/hkeys/|valkey.io} for details.
*
* @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
34 changes: 34 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,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,
};

// set up hash with two keys/values
expect(await client.hset(key, fieldValueMap)).toEqual(2);
expect(await client.hkeys(key)).toEqual([field1, field2]);

// remove one key
expect(await client.hdel(key, [field1])).toEqual(1);
expect(await client.hkeys(key)).toEqual([field2]);

// non-existing key returns an empty list
expect(await client.hkeys("nonExistingKey")).toEqual([]);

// Key exists, but it is not a hash
expect(await client.set(key2, value)).toEqual("OK");
await expect(client.hkeys(key2)).rejects.toThrow();
}, protocol);
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`hscan test_%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 @@ -744,6 +744,8 @@ export async function transactionTest(
responseData.push(["hsetnx(key4, field, value)", false]);
baseTransaction.hvals(key4);
responseData.push(["hvals(key4)", [value]]);
baseTransaction.hkeys(key4);
responseData.push(["hkeys(key4)", [field]]);
baseTransaction.hget(key4, field);
responseData.push(["hget(key4, field)", value]);
baseTransaction.hgetall(key4);
Expand Down

0 comments on commit 8972411

Please sign in to comment.