Skip to content

Commit

Permalink
adding decoder
Browse files Browse the repository at this point in the history
Signed-off-by: TJ Zhang <tj.zhang@improving.com>
  • Loading branch information
TJ Zhang committed Aug 22, 2024
1 parent de66bde commit 912e93c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 277 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,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 ([#TBD](https://github.com/valkey-io/valkey-glide/pull/TBD))
* * 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
100 changes: 74 additions & 26 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -965,9 +965,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:
* - 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.
* - {@link Decoder} type which defines how to handle the response. If not set, the default decoder from the client config will be used.
* @returns If `key` exists, returns the value of `key` as a `string`. Otherwise, return `null`.
*
* @example
Expand All @@ -978,9 +980,14 @@ export class BaseClient {
*/
public async getex(
key: GlideString,
options?: "persist" | { type: TimeUnit; duration: number },
options?: {
cmdOptions: "persist" | { type: TimeUnit; duration: number };
decoder?: Decoder;
},
): Promise<string | null> {
return this.createWritePromise(createGetEx(key, options));
return this.createWritePromise(createGetEx(key, options?.cmdOptions), {
decoder: options?.decoder,
});
}

/**
Expand Down Expand Up @@ -1079,8 +1086,8 @@ export class BaseClient {
* ```
*/
public async set(
key: string | Uint8Array,
value: string | Uint8Array,
key: GlideString | Uint8Array,
value: GlideString | Uint8Array,
options?: SetOptions,
): Promise<"OK" | string | null> {
return this.createWritePromise(createSet(key, value, options));
Expand Down Expand Up @@ -1261,6 +1268,7 @@ export class BaseClient {
* @see {@link https://valkey.io/commands/incr/|valkey.io} for details.
*
* @param key - The key to increment its value.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response. If not set, the default decoder from the client config will be used.
* @returns the value of `key` after the increment.
*
* @example
Expand All @@ -1271,15 +1279,16 @@ export class BaseClient {
* console.log(result); // Output: 11
* ```
*/
public async incr(key: GlideString): Promise<number> {
return this.createWritePromise(createIncr(key));
public async incr(key: GlideString, decoder?: Decoder): Promise<number> {
return this.createWritePromise(createIncr(key), { decoder: decoder });
}

/** Increments the number stored at `key` by `amount`. If `key` does not exist, it is set to 0 before performing the operation.
*
* @see {@link https://valkey.io/commands/incrby/|valkey.io} for details.
*
* @param key - The key to increment its value.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response. If not set, the default decoder from the client config will be used.
* @param amount - The amount to increment.
* @returns the value of `key` after the increment.
*
Expand All @@ -1291,8 +1300,14 @@ export class BaseClient {
* console.log(result); // Output: 15
* ```
*/
public async incrBy(key: GlideString, amount: number): Promise<number> {
return this.createWritePromise(createIncrBy(key, amount));
public async incrBy(
key: GlideString,
amount: number,
decoder?: Decoder,
): Promise<number> {
return this.createWritePromise(createIncrBy(key, amount), {
decoder: decoder,
});
}

/** Increment the string representing a floating point number stored at `key` by `amount`.
Expand All @@ -1303,6 +1318,7 @@ export class BaseClient {
*
* @param key - The key to increment its value.
* @param amount - The amount to increment.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response. If not set, the default decoder from the client config will be used.
* @returns the value of `key` after the increment.
*
* @example
Expand All @@ -1316,15 +1332,19 @@ export class BaseClient {
public async incrByFloat(
key: GlideString,
amount: number,
decoder?: Decoder,
): Promise<number> {
return this.createWritePromise(createIncrByFloat(key, amount));
return this.createWritePromise(createIncrByFloat(key, amount), {
decoder: decoder,
});
}

/** Decrements the number stored at `key` by one. If `key` does not exist, it is set to 0 before performing the operation.
*
* @see {@link https://valkey.io/commands/decr/|valkey.io} for details.
*
* @param key - The key to decrement its value.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response. If not set, the default decoder from the client config will be used.
* @returns the value of `key` after the decrement.
*
* @example
Expand All @@ -1335,8 +1355,8 @@ export class BaseClient {
* console.log(result); // Output: 9
* ```
*/
public async decr(key: GlideString): Promise<number> {
return this.createWritePromise(createDecr(key));
public async decr(key: GlideString, decoder?: Decoder): Promise<number> {
return this.createWritePromise(createDecr(key), { decoder: decoder });
}

/** Decrements the number stored at `key` by `amount`. If `key` does not exist, it is set to 0 before performing the operation.
Expand All @@ -1345,6 +1365,7 @@ export class BaseClient {
*
* @param key - The key to decrement its value.
* @param amount - The amount to decrement.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response. If not set, the default decoder from the client config will be used.
* @returns the value of `key` after the decrement.
*
* @example
Expand All @@ -1355,8 +1376,14 @@ export class BaseClient {
* console.log(result); // Output: 5
* ```
*/
public async decrBy(key: GlideString, amount: number): Promise<number> {
return this.createWritePromise(createDecrBy(key, amount));
public async decrBy(
key: GlideString,
amount: number,
decoder?: Decoder,
): Promise<number> {
return this.createWritePromise(createDecrBy(key, amount), {
decoder: decoder,
});
}

/**
Expand Down Expand Up @@ -4131,7 +4158,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 @@ -6162,6 +6189,7 @@ export class BaseClient {
*
* @param key1 - The key that stores the first string.
* @param key2 - The key that stores the second string.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response. If not set, the default decoder from the client config will be used.
* @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 @@ -6172,8 +6200,14 @@ export class BaseClient {
* console.log(result); // Output: 'acd'
* ```
*/
public async lcs(key1: GlideString, key2: GlideString): Promise<string> {
return this.createWritePromise(createLCS(key1, key2));
public async lcs(
key1: GlideString,
key2: GlideString,
decoder?: Decoder,
): Promise<string> {
return this.createWritePromise(createLCS(key1, key2), {
decoder: decoder,
});
}

/**
Expand All @@ -6185,6 +6219,7 @@ export class BaseClient {
*
* @param key1 - The key that stores the first string.
* @param key2 - The key that stores the second string.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response. If not set, the default decoder from the client config will be used.
* @returns The total length of all the longest common subsequences between the 2 strings.
*
* @example
Expand All @@ -6194,8 +6229,14 @@ export class BaseClient {
* console.log(result); // Output: 3
* ```
*/
public async lcsLen(key1: GlideString, key2: GlideString): Promise<number> {
return this.createWritePromise(createLCS(key1, key2, { len: true }));
public async lcsLen(
key1: GlideString,
key2: GlideString,
decoder?: Decoder,
): Promise<number> {
return this.createWritePromise(createLCS(key1, key2, { len: true }), {
decoder: decoder,
});
}

/**
Expand All @@ -6208,8 +6249,10 @@ 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- Additional parameters:
* - withMatchLen - (Optional) If `true`, include the length of the substring matched for the each match.
* - minMatchLen - (Optional) The minimum length of matches to include in the result.
* - decoder - (Optional) {@link Decoder} type which defines how to handle the response. If not set, the default decoder from the client config will be used.
* @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 @@ -6247,10 +6290,15 @@ export class BaseClient {
public async lcsIdx(
key1: GlideString,
key2: GlideString,
options?: { withMatchLen?: boolean; minMatchLen?: number },
options?: {
withMatchLen?: boolean;
minMatchLen?: number;
decoder?: Decoder;
},
): Promise<Record<string, (number | [number, number])[][] | number>> {
return this.createWritePromise(
createLCS(key1, key2, { idx: options ?? {} }),
{ decoder: options?.decoder },
);
}

Expand Down Expand Up @@ -6350,9 +6398,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
6 changes: 3 additions & 3 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ export function createType(key: string): 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 @@ -3770,9 +3770,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
11 changes: 6 additions & 5 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* If `value` isn't set because of `onlyIfExists` or `onlyIfDoesNotExist` conditions, return null.
* If `returnOldValue` is set, return the old value as a string.
*/
public set(key: string, value: string, options?: SetOptions): T {
public set(key: GlideString, value: GlideString, options?: SetOptions): T {
return this.addAndReturn(createSet(key, value, options));
}

Expand Down Expand Up @@ -2129,7 +2129,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* Command Response - The length of the string value stored at `key`
* If `key` does not exist, it is treated as an empty string, and the command returns 0.
*/
public strlen(key: string): T {
public strlen(key: GlideString): T {
return this.addAndReturn(createStrlen(key));
}

Expand Down Expand Up @@ -3636,8 +3636,9 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* @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 - Additional parameters:
* - withMatchLen - (Optional) If `true`, include the length of the substring matched for the each match.
* - minMatchLen - (Optional) The minimum length of matches to include in the result.
*
* Command Response - 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
Expand Down Expand Up @@ -3695,7 +3696,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* Command Response - The length of the string stored at `key` after it was modified.
*/
public setrange(key: string, offset: number, value: string): T {
public setrange(key: GlideString, offset: number, value: GlideString): T {
return this.addAndReturn(createSetRange(key, offset, value));
}

Expand Down
Loading

0 comments on commit 912e93c

Please sign in to comment.