Skip to content

Commit

Permalink
Node: Add binary support for hash commands (valkey-io#2194)
Browse files Browse the repository at this point in the history
* Update CHANGELOG

Signed-off-by: Jonathan Louie <[email protected]>

* Add binary variants for hash commands

Signed-off-by: Jonathan Louie <[email protected]>

* Apply Prettier

Signed-off-by: Jonathan Louie <[email protected]>

* Change args for hscan to be GlideString[]

Signed-off-by: Jonathan Louie <[email protected]>

* Use GlideString for BaseScanOptions

Signed-off-by: Jonathan Louie <[email protected]>

* Run Prettier

Signed-off-by: Jonathan Louie <[email protected]>

* Exclude HSCAN command from changes for now

Signed-off-by: Jonathan Louie <[email protected]>

* Revert change to return type of convertBaseScanOptionsToArgsArray

Signed-off-by: Jonathan Louie <[email protected]>

* Revert accidental change of count to string for BaseScanOptions

Signed-off-by: Jonathan Louie <[email protected]>

* Apply Prettier

Signed-off-by: Jonathan Louie <[email protected]>

* Start changing hash commands to use DecoderOption

Signed-off-by: Jonathan Louie <[email protected]>

* Add DecoderOption for hash commands

Signed-off-by: Jonathan Louie <[email protected]>

* Update SharedTests

Signed-off-by: Jonathan Louie <[email protected]>

* Shorten DecoderOption parameter descriptions

Signed-off-by: Jonathan Louie <[email protected]>

* Apply Prettier

Signed-off-by: Jonathan Louie <[email protected]>

* Try to fix test failures

Signed-off-by: Jonathan Louie <[email protected]>

* Switch to toContainEqual for hrandfield binary test

Signed-off-by: Jonathan Louie <[email protected]>

---------

Signed-off-by: Jonathan Louie <[email protected]>
Signed-off-by: jonathanl-bq <[email protected]>
  • Loading branch information
jonathanl-bq authored Aug 28, 2024
1 parent 70b0ae8 commit 8abbfb3
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#### Changes
* Node: Added binary variant to HASH commands ([#2194](https://github.com/valkey-io/valkey-glide/pull/2194))
* Node: Added binary variant to server management commands ([#2179](https://github.com/valkey-io/valkey-glide/pull/2179))
* Node: Added/updated binary variant to connection management commands and WATCH/UNWATCH ([#2160](https://github.com/valkey-io/valkey-glide/pull/2160))
* Java: Fix docs for stream commands ([#2086](https://github.com/valkey-io/valkey-glide/pull/2086))
Expand Down
83 changes: 55 additions & 28 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@ export class BaseClient {
* ```
*/
public async hset(
key: string,
key: GlideString,
fieldValueMap: Record<string, string>,
): Promise<number> {
return this.createWritePromise(createHSet(key, fieldValueMap));
Expand All @@ -1663,6 +1663,7 @@ export class BaseClient {
* @see {@link https://valkey.io/commands/hkeys/|valkey.io} for details.
*
* @param key - The key of the hash.
* @param options - (Optional) See {@link DecoderOption}.
* @returns A list of field names for the hash, or an empty list when the key does not exist.
*
* @example
Expand All @@ -1673,8 +1674,12 @@ export class BaseClient {
* console.log(result); // Output: ["field1", "field2", "field3"] - Returns all the field names stored in the hash "my_hash".
* ```
*/
public async hkeys(key: string): Promise<string[]> {
return this.createWritePromise(createHKeys(key));

public async hkeys(
key: GlideString,
options?: DecoderOption,
): Promise<GlideString[]> {
return this.createWritePromise(createHKeys(key), options);
}

/** Sets `field` in the hash stored at `key` to `value`, only if `field` does not yet exist.
Expand Down Expand Up @@ -1703,9 +1708,9 @@ export class BaseClient {
* ```
*/
public async hsetnx(
key: string,
field: string,
value: string,
key: GlideString,
field: GlideString,
value: GlideString,
): Promise<boolean> {
return this.createWritePromise(createHSetNX(key, field, value));
}
Expand All @@ -1727,7 +1732,10 @@ export class BaseClient {
* console.log(result); // Output: 2 - Indicates that two fields were successfully removed from the hash.
* ```
*/
public async hdel(key: string, fields: string[]): Promise<number> {
public async hdel(
key: GlideString,
fields: GlideString[],
): Promise<number> {
return this.createWritePromise(createHDel(key, fields));
}

Expand All @@ -1737,6 +1745,7 @@ export class BaseClient {
*
* @param key - The key of the hash.
* @param fields - The fields in the hash stored at `key` to retrieve from the database.
* @param options - (Optional) See {@link DecoderOption}.
* @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.
Expand All @@ -1749,10 +1758,11 @@ export class BaseClient {
* ```
*/
public async hmget(
key: string,
fields: string[],
): Promise<(string | null)[]> {
return this.createWritePromise(createHMGet(key, fields));
key: GlideString,
fields: GlideString[],
options?: DecoderOption,
): Promise<(GlideString | null)[]> {
return this.createWritePromise(createHMGet(key, fields), options);
}

/** Returns if `field` is an existing field in the hash stored at `key`.
Expand All @@ -1777,7 +1787,10 @@ export class BaseClient {
* console.log(result); // Output: false
* ```
*/
public async hexists(key: string, field: string): Promise<boolean> {
public async hexists(
key: GlideString,
field: GlideString,
): Promise<boolean> {
return this.createWritePromise(createHExists(key, field));
}

Expand All @@ -1796,7 +1809,7 @@ export class BaseClient {
* console.log(result); // Output: {"field1": "value1", "field2": "value2"}
* ```
*/
public async hgetall(key: string): Promise<Record<string, string>> {
public async hgetall(key: GlideString): Promise<Record<string, string>> {
return this.createWritePromise(createHGetAll(key));
}

Expand All @@ -1819,8 +1832,8 @@ export class BaseClient {
* ```
*/
public async hincrBy(
key: string,
field: string,
key: GlideString,
field: GlideString,
amount: number,
): Promise<number> {
return this.createWritePromise(createHIncrBy(key, field, amount));
Expand All @@ -1841,12 +1854,12 @@ export class BaseClient {
* ```typescript
* // Example usage of the hincrbyfloat method to increment the value of a floating point in a hash by a specified amount
* const result = await client.hincrbyfloat("my_hash", "field1", 2.5);
* console.log(result); // Output: '2.5'
* console.log(result); // Output: 2.5
* ```
*/
public async hincrByFloat(
key: string,
field: string,
key: GlideString,
field: GlideString,
amount: number,
): Promise<number> {
return this.createWritePromise(createHIncrByFloat(key, field, amount));
Expand All @@ -1873,7 +1886,7 @@ export class BaseClient {
* console.log(result); // Output: 0
* ```
*/
public async hlen(key: string): Promise<number> {
public async hlen(key: GlideString): Promise<number> {
return this.createWritePromise(createHLen(key));
}

Expand Down Expand Up @@ -1916,7 +1929,10 @@ export class BaseClient {
* console.log(result); // Output: 5
* ```
*/
public async hstrlen(key: string, field: string): Promise<number> {
public async hstrlen(
key: GlideString,
field: GlideString,
): Promise<number> {
return this.createWritePromise(createHStrlen(key, field));
}

Expand All @@ -1927,6 +1943,7 @@ export class BaseClient {
* @remarks Since Valkey version 6.2.0.
*
* @param key - The key of the hash.
* @param options - (Optional) See {@link DecoderOption}.
* @returns A random field name from the hash stored at `key`, or `null` when
* the key does not exist.
*
Expand All @@ -1935,8 +1952,11 @@ export class BaseClient {
* console.log(await client.hrandfield("myHash")); // Output: 'field'
* ```
*/
public async hrandfield(key: string): Promise<string | null> {
return this.createWritePromise(createHRandField(key));
public async hrandfield(
key: GlideString,
options?: DecoderOption,
): Promise<GlideString | null> {
return this.createWritePromise(createHRandField(key), options);
}

/**
Expand Down Expand Up @@ -1992,6 +2012,7 @@ export class BaseClient {
*
* @param key - The key of the hash.
* @param count - The number of field names to return.
* @param options - (Optional) See {@link DecoderOption}.
*
* If `count` is positive, returns unique elements. If negative, allows for duplicates.
* @returns An `array` of random field names from the hash stored at `key`,
Expand All @@ -2003,10 +2024,11 @@ export class BaseClient {
* ```
*/
public async hrandfieldCount(
key: string,
key: GlideString,
count: number,
): Promise<string[]> {
return this.createWritePromise(createHRandField(key, count));
options?: DecoderOption,
): Promise<GlideString[]> {
return this.createWritePromise(createHRandField(key, count), options);
}

/**
Expand All @@ -2018,6 +2040,7 @@ export class BaseClient {
*
* @param key - The key of the hash.
* @param count - The number of field names to return.
* @param options - (Optional) See {@link DecoderOption}.
*
* If `count` is positive, returns unique elements. If negative, allows for duplicates.
* @returns A 2D `array` of `[fieldName, value]` `arrays`, where `fieldName` is a random
Expand All @@ -2031,10 +2054,14 @@ export class BaseClient {
* ```
*/
public async hrandfieldWithValues(
key: string,
key: GlideString,
count: number,
): Promise<[string, string][]> {
return this.createWritePromise(createHRandField(key, count, true));
options?: DecoderOption,
): Promise<[GlideString, GlideString][]> {
return this.createWritePromise(
createHRandField(key, count, true),
options,
);
}

/** Inserts all the specified values at the head of the list stored at `key`.
Expand Down
40 changes: 20 additions & 20 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export function createHGet(
* @internal
*/
export function createHSet(
key: string,
key: GlideString,
fieldValueMap: Record<string, string>,
): command_request.Command {
return createCommand(
Expand All @@ -424,17 +424,17 @@ export function createHSet(
/**
* @internal
*/
export function createHKeys(key: string): command_request.Command {
export function createHKeys(key: GlideString): command_request.Command {
return createCommand(RequestType.HKeys, [key]);
}

/**
* @internal
*/
export function createHSetNX(
key: string,
field: string,
value: string,
key: GlideString,
field: GlideString,
value: GlideString,
): command_request.Command {
return createCommand(RequestType.HSetNX, [key, field, value]);
}
Expand Down Expand Up @@ -801,8 +801,8 @@ export function createBitField(
* @internal
*/
export function createHDel(
key: string,
fields: string[],
key: GlideString,
fields: GlideString[],
): command_request.Command {
return createCommand(RequestType.HDel, [key].concat(fields));
}
Expand All @@ -811,8 +811,8 @@ export function createHDel(
* @internal
*/
export function createHMGet(
key: string,
fields: string[],
key: GlideString,
fields: GlideString[],
): command_request.Command {
return createCommand(RequestType.HMGet, [key].concat(fields));
}
Expand All @@ -821,16 +821,16 @@ export function createHMGet(
* @internal
*/
export function createHExists(
key: string,
field: string,
key: GlideString,
field: GlideString,
): command_request.Command {
return createCommand(RequestType.HExists, [key, field]);
}

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

Expand Down Expand Up @@ -1193,8 +1193,8 @@ export function createCustomCommand(args: GlideString[]) {
* @internal
*/
export function createHIncrBy(
key: string,
field: string,
key: GlideString,
field: GlideString,
amount: number,
): command_request.Command {
return createCommand(RequestType.HIncrBy, [key, field, amount.toString()]);
Expand All @@ -1204,8 +1204,8 @@ export function createHIncrBy(
* @internal
*/
export function createHIncrByFloat(
key: string,
field: string,
key: GlideString,
field: GlideString,
amount: number,
): command_request.Command {
return createCommand(RequestType.HIncrByFloat, [
Expand All @@ -1218,7 +1218,7 @@ export function createHIncrByFloat(
/**
* @internal
*/
export function createHLen(key: string): command_request.Command {
export function createHLen(key: GlideString): command_request.Command {
return createCommand(RequestType.HLen, [key]);
}

Expand Down Expand Up @@ -3618,15 +3618,15 @@ function createSortImpl(
* @internal
*/
export function createHStrlen(
key: string,
field: string,
key: GlideString,
field: GlideString,
): command_request.Command {
return createCommand(RequestType.HStrlen, [key, field]);
}

/** @internal */
export function createHRandField(
key: string,
key: GlideString,
count?: number,
withValues?: boolean,
): command_request.Command {
Expand Down
Loading

0 comments on commit 8abbfb3

Please sign in to comment.