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: add binary support to string commands #2183

Merged
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
* Node: Added ZINTER and ZUNION commands ([#2146](https://github.com/aws/glide-for-redis/pull/2146))
* Node: Added XACK commands ([#2112](https://github.com/valkey-io/valkey-glide/pull/2112))
* Node: Added XGROUP SETID command ([#2135]((https://github.com/valkey-io/valkey-glide/pull/2135))
* Node: Added binary variant to string commands ([#2183](https://github.com/valkey-io/valkey-glide/pull/2183))

#### Breaking Changes
* Node: (Refactor) Convert classes to types ([#2005](https://github.com/valkey-io/valkey-glide/pull/2005))
Expand Down
85 changes: 57 additions & 28 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,11 @@ export class BaseClient {
* @remarks Since Valkey version 6.2.0.
*
* @param key - The key to retrieve from the database.
* @param options - (Optional) Set expiriation to the given key.
* "persist" will retain the time to live associated with the key. Equivalent to `PERSIST` in the VALKEY API.
* Otherwise, a {@link TimeUnit} and duration of the expire time should be specified.
* @param options - (Optional) Additional Parameters:
* - (Optional) `expiry`: expiriation to the given key:
* `"persist"` will retain the time to live associated with the key. Equivalent to `PERSIST` in the VALKEY API.
* Otherwise, a {@link TimeUnit} and duration of the expire time should be specified.
* - (Optional) `decoder`: see {@link DecoderOption}.
* @returns If `key` exists, returns the value of `key` as a `string`. Otherwise, return `null`.
*
* @example
Expand All @@ -999,10 +1001,14 @@ export class BaseClient {
* ```
*/
public async getex(
key: string,
options?: "persist" | { type: TimeUnit; duration: number },
): Promise<string | null> {
return this.createWritePromise(createGetEx(key, options));
key: GlideString,
options?: {
tjzhang-BQ marked this conversation as resolved.
Show resolved Hide resolved
expiry: "persist" | { type: TimeUnit; duration: number };
} & DecoderOption,
): Promise<GlideString | null> {
return this.createWritePromise(createGetEx(key, options?.expiry), {
decoder: options?.decoder,
});
}

/**
Expand Down Expand Up @@ -1076,7 +1082,7 @@ export class BaseClient {
*
* @param key - The key to store.
* @param value - The value to store with the given key.
* @param options - The set options.
* @param options - (Optional) See {@link SetOptions} and {@link DecoderOption}.
* @returns - If the value is successfully set, return OK.
* If value isn't set because of `onlyIfExists` or `onlyIfDoesNotExist` conditions, return null.
* If `returnOldValue` is set, return the old value as a string.
Expand All @@ -1103,9 +1109,11 @@ export class BaseClient {
public async set(
key: GlideString,
value: GlideString,
options?: SetOptions,
): Promise<"OK" | string | null> {
return this.createWritePromise(createSet(key, value, options));
options?: SetOptions & DecoderOption,
): Promise<"OK" | GlideString | null> {
return this.createWritePromise(createSet(key, value, options), {
decoder: options?.decoder,
});
}

/**
Expand Down Expand Up @@ -1294,7 +1302,7 @@ export class BaseClient {
* console.log(result); // Output: 11
* ```
*/
public async incr(key: string): Promise<number> {
public async incr(key: GlideString): Promise<number> {
return this.createWritePromise(createIncr(key));
}

Expand All @@ -1314,7 +1322,7 @@ export class BaseClient {
* console.log(result); // Output: 15
* ```
*/
public async incrBy(key: string, amount: number): Promise<number> {
public async incrBy(key: GlideString, amount: number): Promise<number> {
return this.createWritePromise(createIncrBy(key, amount));
}

Expand All @@ -1336,7 +1344,10 @@ export class BaseClient {
* console.log(result); // Output: 13.0
* ```
*/
public async incrByFloat(key: string, amount: number): Promise<number> {
public async incrByFloat(
key: GlideString,
amount: number,
): Promise<number> {
return this.createWritePromise(createIncrByFloat(key, amount));
}

Expand All @@ -1355,7 +1366,7 @@ export class BaseClient {
* console.log(result); // Output: 9
* ```
*/
public async decr(key: string): Promise<number> {
public async decr(key: GlideString): Promise<number> {
return this.createWritePromise(createDecr(key));
}

Expand All @@ -1375,7 +1386,7 @@ export class BaseClient {
* console.log(result); // Output: 5
* ```
*/
public async decrBy(key: string, amount: number): Promise<number> {
public async decrBy(key: GlideString, amount: number): Promise<number> {
return this.createWritePromise(createDecrBy(key, amount));
}

Expand Down Expand Up @@ -4163,7 +4174,7 @@ export class BaseClient {
* console.log(len2); // Output: 0
* ```
*/
public async strlen(key: string): Promise<number> {
public async strlen(key: GlideString): Promise<number> {
return this.createWritePromise(createStrlen(key));
}

Expand Down Expand Up @@ -6231,6 +6242,7 @@ export class BaseClient {
*
* @param key1 - The key that stores the first string.
* @param key2 - The key that stores the second string.
* @param options - (Optional) See {@link DecoderOption}.
* @returns A `String` containing all the longest common subsequence combined between the 2 strings.
* An empty `String` is returned if the keys do not exist or have no common subsequences.
*
Expand All @@ -6241,8 +6253,12 @@ export class BaseClient {
* console.log(result); // Output: 'acd'
* ```
*/
public async lcs(key1: string, key2: string): Promise<string> {
return this.createWritePromise(createLCS(key1, key2));
public async lcs(
key1: GlideString,
key2: GlideString,
options?: DecoderOption,
): Promise<string> {
return this.createWritePromise(createLCS(key1, key2), options);
}

/**
Expand All @@ -6254,6 +6270,7 @@ export class BaseClient {
*
* @param key1 - The key that stores the first string.
* @param key2 - The key that stores the second string.
* @param options - (Optional) See {@link DecoderOption}.
* @returns The total length of all the longest common subsequences between the 2 strings.
*
* @example
Expand All @@ -6263,8 +6280,15 @@ export class BaseClient {
* console.log(result); // Output: 3
* ```
*/
public async lcsLen(key1: string, key2: string): Promise<number> {
return this.createWritePromise(createLCS(key1, key2, { len: true }));
public async lcsLen(
key1: GlideString,
key2: GlideString,
options?: DecoderOption,
): Promise<number> {
return this.createWritePromise(
createLCS(key1, key2, { len: true }),
options,
);
}

/**
Expand All @@ -6277,8 +6301,9 @@ export class BaseClient {
*
* @param key1 - The key that stores the first string.
* @param key2 - The key that stores the second string.
* @param withMatchLen - (Optional) If `true`, include the length of the substring matched for the each match.
* @param minMatchLen - (Optional) The minimum length of matches to include in the result.
* @param options - (Optional) Additional parameters:
* - (Optional) `withMatchLen`: if `true`, include the length of the substring matched for the each match.
* - (Optional) `minMatchLen`: the minimum length of matches to include in the result.
* @returns A `Record` containing the indices of the longest common subsequences between the
* 2 strings and the lengths of the longest common subsequences. The resulting map contains two
* keys, "matches" and "len":
Expand Down Expand Up @@ -6314,12 +6339,16 @@ export class BaseClient {
* ```
*/
public async lcsIdx(
key1: string,
key2: string,
options?: { withMatchLen?: boolean; minMatchLen?: number },
key1: GlideString,
key2: GlideString,
tjzhang-BQ marked this conversation as resolved.
Show resolved Hide resolved
options?: {
withMatchLen?: boolean;
minMatchLen?: number;
},
): Promise<Record<string, (number | [number, number])[][] | number>> {
tjzhang-BQ marked this conversation as resolved.
Show resolved Hide resolved
return this.createWritePromise(
createLCS(key1, key2, { idx: options ?? {} }),
{ decoder: Decoder.String },
);
}

Expand Down Expand Up @@ -6421,9 +6450,9 @@ export class BaseClient {
* ```
*/
public async setrange(
key: string,
key: GlideString,
offset: number,
value: string,
value: GlideString,
): Promise<number> {
return this.createWritePromise(createSetRange(key, offset, value));
}
Expand Down
22 changes: 11 additions & 11 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,15 @@ export function createMSetNX(
/**
* @internal
*/
export function createIncr(key: string): command_request.Command {
export function createIncr(key: GlideString): command_request.Command {
return createCommand(RequestType.Incr, [key]);
}

/**
* @internal
*/
export function createIncrBy(
key: string,
key: GlideString,
amount: number,
): command_request.Command {
return createCommand(RequestType.IncrBy, [key, amount.toString()]);
Expand All @@ -366,7 +366,7 @@ export function createIncrBy(
* @internal
*/
export function createIncrByFloat(
key: string,
key: GlideString,
amount: number,
): command_request.Command {
return createCommand(RequestType.IncrByFloat, [key, amount.toString()]);
Expand Down Expand Up @@ -442,15 +442,15 @@ export function createHSetNX(
/**
* @internal
*/
export function createDecr(key: string): command_request.Command {
export function createDecr(key: GlideString): command_request.Command {
return createCommand(RequestType.Decr, [key]);
}

/**
* @internal
*/
export function createDecrBy(
key: string,
key: GlideString,
amount: number,
): command_request.Command {
return createCommand(RequestType.DecrBy, [key, amount.toString()]);
Expand Down Expand Up @@ -1850,7 +1850,7 @@ export function createType(key: GlideString): command_request.Command {
/**
* @internal
*/
export function createStrlen(key: string): command_request.Command {
export function createStrlen(key: GlideString): command_request.Command {
return createCommand(RequestType.Strlen, [key]);
}

Expand Down Expand Up @@ -3679,8 +3679,8 @@ export function createLastSave(): command_request.Command {

/** @internal */
export function createLCS(
key1: string,
key2: string,
key1: GlideString,
key2: GlideString,
options?: {
len?: boolean;
idx?: { withMatchLen?: boolean; minMatchLen?: number };
Expand Down Expand Up @@ -3792,9 +3792,9 @@ export function createZScan(

/** @internal */
export function createSetRange(
key: string,
key: GlideString,
offset: number,
value: string,
value: GlideString,
): command_request.Command {
return createCommand(RequestType.SetRange, [key, offset.toString(), value]);
}
Expand Down Expand Up @@ -3942,7 +3942,7 @@ export enum TimeUnit {
* @internal
*/
export function createGetEx(
key: string,
key: GlideString,
options?: "persist" | { type: TimeUnit; duration: number },
): command_request.Command {
const args = [key];
Expand Down
Loading
Loading