Skip to content

Commit

Permalink
add node
Browse files Browse the repository at this point in the history
Signed-off-by: Shoham Elias <shohame@amazon.com>
  • Loading branch information
shohamazon committed Sep 10, 2024
1 parent 686647c commit daeef61
Show file tree
Hide file tree
Showing 8 changed files with 488 additions and 602 deletions.
110 changes: 110 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
ScoreFilter,
SearchOrigin,
SetOptions,
SortOptions,
StreamAddOptions,
StreamClaimOptions,
StreamGroupOptions,
Expand Down Expand Up @@ -167,6 +168,8 @@ import {
createSet,
createSetBit,
createSetRange,
createSort,
createSortReadOnly,
createStrlen,
createTTL,
createTouch,
Expand Down Expand Up @@ -6978,6 +6981,113 @@ export class BaseClient {
return this.createWritePromise(createPubSubNumSub(channels));
}

/**
* Sorts the elements in the list, set, or sorted set at `key` and returns the result.
*
* The `sort` command can be used to sort elements based on different criteria and
* apply transformations on sorted elements.
*
* To store the result into a new key, see {@link sortStore}.
*
* @see {@link https://valkey.io/commands/sort/|valkey.io} for more details.
* @remarks When in cluster mode, both `key` and the patterns specified in {@link SortOptions.byPattern}
* and {@link SortOptions.getPatterns} must map to the same hash slot. The use of {@link SortOptions.byPattern}
* and {@link SortOptions.getPatterns} in cluster mode is supported since Valkey version 8.0.
* @param key - The key of the list, set, or sorted set to be sorted.
* @param options - (Optional) The {@link SortOptions} and {@link DecoderOption}.
*
* @returns An `Array` of sorted elements.
*
* @example
* ```typescript
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
* await client.lpush("user_ids", ["2", "1"]);
* const result = await client.sort("user_ids", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
* console.log(result); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age
* ```
*/
public async sort(
key: GlideString,
options?: SortOptions & DecoderOption,
): Promise<(GlideString | null)[]> {
return this.createWritePromise(createSort(key, options), {
decoder: options?.decoder,
});
}

/**
* Sorts the elements in the list, set, or sorted set at `key` and returns the result.
*
* The `sortReadOnly` command can be used to sort elements based on different criteria and
* apply transformations on sorted elements.
*
* This command is routed depending on the client's {@link ReadFrom} strategy.
*
* @see {@link https://valkey.io/commands/sort/|valkey.io} for more details.
* @remarks Since Valkey version 7.0.0.
* @remarks When in cluster mode, both `key` and the patterns specified in {@link SortOptions.byPattern}
* and {@link SortOptions.getPatterns} must map to the same hash slot. The use of {@link SortOptions.byPattern}
* and {@link SortOptions.getPatterns} in cluster mode is supported since Valkey version 8.0.
* @param key - The key of the list, set, or sorted set to be sorted.
* @param options - (Optional) The {@link SortOptions} and {@link DecoderOption}.
* @returns An `Array` of sorted elements
*
* @example
* ```typescript
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
* await client.lpush("user_ids", ["2", "1"]);
* const result = await client.sortReadOnly("user_ids", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
* console.log(result); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age
* ```
*/
public async sortReadOnly(
key: GlideString,
options?: SortOptions & DecoderOption,
): Promise<(GlideString | null)[]> {
return this.createWritePromise(createSortReadOnly(key, options), {
decoder: options?.decoder,
});
}

/**
* Sorts the elements in the list, set, or sorted set at `key` and stores the result in
* `destination`.
*
* The `sort` command can be used to sort elements based on different criteria and
* apply transformations on sorted elements, and store the result in a new key.
*
* To get the sort result without storing it into a key, see {@link sort} or {@link sortReadOnly}.
*
* @see {@link https://valkey.io/commands/sort|valkey.io} for more details.
* @remarks When in cluster mode, `key`, `destination` and the patterns specified in {@link SortOptions.byPattern}
* and {@link SortOptions.getPatterns} must map to the same hash slot. The use of {@link SortOptions.byPattern}
* and {@link SortOptions.getPatterns} in cluster mode is supported since Valkey version 8.0.
*
* @param key - The key of the list, set, or sorted set to be sorted.
* @param destination - The key where the sorted result will be stored.
* @param options - (Optional) The {@link SortOptions}.
* @returns The number of elements in the sorted key stored at `destination`.
*
* @example
* ```typescript
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
* await client.lpush("user_ids", ["2", "1"]);
* const sortedElements = await client.sortStore("user_ids", "sortedList", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
* console.log(sortedElements); // Output: 2 - number of elements sorted and stored
* console.log(await client.lrange("sortedList", 0, -1)); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age stored in `sortedList`
* ```
*/
public async sortStore(
key: GlideString,
destination: GlideString,
options?: SortOptions,
): Promise<number> {
return this.createWritePromise(createSort(key, options, destination));
}

/**
* @internal
*/
Expand Down
30 changes: 13 additions & 17 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3518,20 +3518,30 @@ export function createZIncrBy(
}

/**
* Optional arguments to {@link GlideClient.sort|sort}, {@link GlideClient.sortStore|sortStore} and {@link GlideClient.sortReadOnly|sortReadOnly} commands.
* Optional arguments to {@link BaseClient.sort|sort}, {@link BaseClient.sortStore|sortStore} and {@link BaseClient.sortReadOnly|sortReadOnly} commands.
*
* See https://valkey.io/commands/sort/ for more details.
*
* @remarks When in cluster mode, {@link SortOptions.byPattern|byPattern} and {@link SortOptions.getPatterns|getPattern} must map to the same hash
* slot as the key, and this is supported only since Valkey version 8.0.
*/
export type SortOptions = SortBaseOptions & {
export type SortOptions = {
/**
* A pattern to sort by external keys instead of by the elements stored at the key themselves. The
* pattern should contain an asterisk (*) as a placeholder for the element values, where the value
* from the key replaces the asterisk to create the key name. For example, if `key`
* contains IDs of objects, `byPattern` can be used to sort these IDs based on an
* attribute of the objects, like their weights or timestamps.
* Supported in cluster mode since Valkey version 8.0 and above.
*/
byPattern?: GlideString;

/**
* Limiting the range of the query by setting offset and result count. See {@link Limit} class for
* more information.
*/
limit?: Limit;

/**
* A pattern used to retrieve external keys' values, instead of the elements at `key`.
* The pattern should contain an asterisk (`*`) as a placeholder for the element values, where the
Expand All @@ -3544,16 +3554,9 @@ export type SortOptions = SortBaseOptions & {
* arguments can be provided to retrieve multiple attributes. The special value `#` can
* be used to include the actual element from `key` being sorted. If not provided, only
* the sorted elements themselves are returned.
* Supported in cluster mode since Valkey version 8.0 and above.
*/
getPatterns?: GlideString[];
};

type SortBaseOptions = {
/**
* Limiting the range of the query by setting offset and result count. See {@link Limit} class for
* more information.
*/
limit?: Limit;

/** Options for sorting order of elements. */
orderBy?: SortOrder;
Expand All @@ -3566,13 +3569,6 @@ type SortBaseOptions = {
isAlpha?: boolean;
};

/**
* Optional arguments to {@link GlideClusterClient.sort|sort}, {@link GlideClusterClient.sortStore|sortStore} and {@link GlideClusterClient.sortReadOnly|sortReadOnly} commands.
*
* See https://valkey.io/commands/sort/ for more details.
*/
export type SortClusterOptions = SortBaseOptions;

/**
* The `LIMIT` argument is commonly used to specify a subset of results from the
* matching elements, similar to the `LIMIT` clause in SQL (e.g., `SELECT LIMIT offset, count`).
Expand Down
107 changes: 1 addition & 106 deletions node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
Decoder,
DecoderOption,
GlideString,
PubSubMsg,
ReadFrom, // eslint-disable-line @typescript-eslint/no-unused-vars
PubSubMsg, // eslint-disable-line @typescript-eslint/no-unused-vars
ReturnType,
} from "./BaseClient";
import {
Expand All @@ -21,7 +20,6 @@ import {
FunctionStatsFullResponse,
InfoOptions,
LolwutOptions,
SortOptions,
createClientGetName,
createClientId,
createConfigGet,
Expand Down Expand Up @@ -50,8 +48,6 @@ import {
createPublish,
createRandomKey,
createSelect,
createSort,
createSortReadOnly,
createTime,
createUnWatch,
} from "./Commands";
Expand Down Expand Up @@ -858,107 +854,6 @@ export class GlideClient extends BaseClient {
return this.createWritePromise(createPublish(message, channel));
}

/**
* Sorts the elements in the list, set, or sorted set at `key` and returns the result.
*
* The `sort` command can be used to sort elements based on different criteria and
* apply transformations on sorted elements.
*
* To store the result into a new key, see {@link sortStore}.
*
* @see {@link https://valkey.io/commands/sort/|valkey.io} for more details.
*
* @param key - The key of the list, set, or sorted set to be sorted.
* @param options - (Optional) The {@link SortOptions} and {@link DecoderOption}.
*
* @returns An `Array` of sorted elements.
*
* @example
* ```typescript
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
* await client.lpush("user_ids", ["2", "1"]);
* const result = await client.sort("user_ids", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
* console.log(result); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age
* ```
*/
public async sort(
key: GlideString,
options?: SortOptions & DecoderOption,
): Promise<(GlideString | null)[]> {
return this.createWritePromise(createSort(key, options), {
decoder: options?.decoder,
});
}

/**
* Sorts the elements in the list, set, or sorted set at `key` and returns the result.
*
* The `sortReadOnly` command can be used to sort elements based on different criteria and
* apply transformations on sorted elements.
*
* This command is routed depending on the client's {@link ReadFrom} strategy.
*
* @see {@link https://valkey.io/commands/sort/|valkey.io} for more details.
* @remarks Since Valkey version 7.0.0.
*
* @param key - The key of the list, set, or sorted set to be sorted.
* @param options - (Optional) The {@link SortOptions} and {@link DecoderOption}.
* @returns An `Array` of sorted elements
*
* @example
* ```typescript
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
* await client.lpush("user_ids", ["2", "1"]);
* const result = await client.sortReadOnly("user_ids", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
* console.log(result); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age
* ```
*/
public async sortReadOnly(
key: GlideString,
options?: SortOptions & DecoderOption,
): Promise<(GlideString | null)[]> {
return this.createWritePromise(createSortReadOnly(key, options), {
decoder: options?.decoder,
});
}

/**
* Sorts the elements in the list, set, or sorted set at `key` and stores the result in
* `destination`.
*
* The `sort` command can be used to sort elements based on different criteria and
* apply transformations on sorted elements, and store the result in a new key.
*
* To get the sort result without storing it into a key, see {@link sort} or {@link sortReadOnly}.
*
* @see {@link https://valkey.io/commands/sort|valkey.io} for more details.
* @remarks When in cluster mode, `destination` and `key` must map to the same hash slot.
*
* @param key - The key of the list, set, or sorted set to be sorted.
* @param destination - The key where the sorted result will be stored.
* @param options - (Optional) The {@link SortOptions}.
* @returns The number of elements in the sorted key stored at `destination`.
*
* @example
* ```typescript
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
* await client.lpush("user_ids", ["2", "1"]);
* const sortedElements = await client.sortStore("user_ids", "sortedList", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
* console.log(sortedElements); // Output: 2 - number of elements sorted and stored
* console.log(await client.lrange("sortedList", 0, -1)); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age stored in `sortedList`
* ```
*/
public async sortStore(
key: GlideString,
destination: GlideString,
options?: SortOptions,
): Promise<number> {
return this.createWritePromise(createSort(key, options, destination));
}

/**
* Returns `UNIX TIME` of the last DB save timestamp or startup timestamp if no save
* was made since then.
Expand Down
Loading

0 comments on commit daeef61

Please sign in to comment.