Skip to content

Commit 9b7ba33

Browse files
committed
Add NodeJS
Signed-off-by: Shoham Elias <shohame@amazon.com>
1 parent 88d871c commit 9b7ba33

File tree

8 files changed

+439
-582
lines changed

8 files changed

+439
-582
lines changed

node/src/BaseClient.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
ScoreFilter,
4747
SearchOrigin,
4848
SetOptions,
49+
SortOptions,
4950
StreamAddOptions,
5051
StreamClaimOptions,
5152
StreamGroupOptions,
@@ -168,6 +169,8 @@ import {
168169
createSet,
169170
createSetBit,
170171
createSetRange,
172+
createSort,
173+
createSortReadOnly,
171174
createStrlen,
172175
createTTL,
173176
createTouch,
@@ -7305,6 +7308,112 @@ export class BaseClient {
73057308
);
73067309
}
73077310

7311+
/**
7312+
* Sorts the elements in the list, set, or sorted set at `key` and returns the result.
7313+
*
7314+
* The `sort` command can be used to sort elements based on different criteria and
7315+
* apply transformations on sorted elements.
7316+
*
7317+
* To store the result into a new key, see {@link sortStore}.
7318+
*
7319+
* @see {@link https://valkey.io/commands/sort/|valkey.io} for more details.
7320+
* @remarks When in cluster mode, both `key` and the patterns specified in {@link SortOptions.byPattern}
7321+
* and {@link SortOptions.getPatterns} must map to the same hash slot. The use of {@link SortOptions.byPattern}
7322+
* and {@link SortOptions.getPatterns} in cluster mode is supported since Valkey version 8.0.
7323+
* @param key - The key of the list, set, or sorted set to be sorted.
7324+
* @param options - (Optional) The {@link SortOptions} and {@link DecoderOption}.
7325+
*
7326+
* @returns An `Array` of sorted elements.
7327+
*
7328+
* @example
7329+
* ```typescript
7330+
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
7331+
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
7332+
* await client.lpush("user_ids", ["2", "1"]);
7333+
* const result = await client.sort("user_ids", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
7334+
* console.log(result); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age
7335+
* ```
7336+
*/
7337+
public async sort(
7338+
key: GlideString,
7339+
options?: SortOptions & DecoderOption,
7340+
): Promise<(GlideString | null)[]> {
7341+
return this.createWritePromise(createSort(key, options), options);
7342+
}
7343+
7344+
/**
7345+
* Sorts the elements in the list, set, or sorted set at `key` and returns the result.
7346+
*
7347+
* The `sortReadOnly` command can be used to sort elements based on different criteria and
7348+
* apply transformations on sorted elements.
7349+
*
7350+
* This command is routed depending on the client's {@link ReadFrom} strategy.
7351+
*
7352+
* @see {@link https://valkey.io/commands/sort/|valkey.io} for more details.
7353+
* @remarks Since Valkey version 7.0.0.
7354+
* @remarks When in cluster mode, both `key` and the patterns specified in {@link SortOptions.byPattern}
7355+
* and {@link SortOptions.getPatterns} must map to the same hash slot. The use of {@link SortOptions.byPattern}
7356+
* and {@link SortOptions.getPatterns} in cluster mode is supported since Valkey version 8.0.
7357+
* @param key - The key of the list, set, or sorted set to be sorted.
7358+
* @param options - (Optional) The {@link SortOptions} and {@link DecoderOption}.
7359+
* @returns An `Array` of sorted elements
7360+
*
7361+
* @example
7362+
* ```typescript
7363+
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
7364+
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
7365+
* await client.lpush("user_ids", ["2", "1"]);
7366+
* const result = await client.sortReadOnly("user_ids", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
7367+
* console.log(result); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age
7368+
* ```
7369+
*/
7370+
public async sortReadOnly(
7371+
key: GlideString,
7372+
options?: SortOptions & DecoderOption,
7373+
): Promise<(GlideString | null)[]> {
7374+
return this.createWritePromise(
7375+
createSortReadOnly(key, options),
7376+
options,
7377+
);
7378+
}
7379+
7380+
/**
7381+
* Sorts the elements in the list, set, or sorted set at `key` and stores the result in
7382+
* `destination`.
7383+
*
7384+
* The `sort` command can be used to sort elements based on different criteria and
7385+
* apply transformations on sorted elements, and store the result in a new key.
7386+
*
7387+
* To get the sort result without storing it into a key, see {@link sort} or {@link sortReadOnly}.
7388+
*
7389+
* @see {@link https://valkey.io/commands/sort|valkey.io} for more details.
7390+
* @remarks When in cluster mode, `key`, `destination` and the patterns specified in {@link SortOptions.byPattern}
7391+
* and {@link SortOptions.getPatterns} must map to the same hash slot. The use of {@link SortOptions.byPattern}
7392+
* and {@link SortOptions.getPatterns} in cluster mode is supported since Valkey version 8.0.
7393+
*
7394+
* @param key - The key of the list, set, or sorted set to be sorted.
7395+
* @param destination - The key where the sorted result will be stored.
7396+
* @param options - (Optional) The {@link SortOptions}.
7397+
* @returns The number of elements in the sorted key stored at `destination`.
7398+
*
7399+
* @example
7400+
* ```typescript
7401+
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
7402+
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
7403+
* await client.lpush("user_ids", ["2", "1"]);
7404+
* const sortedElements = await client.sortStore("user_ids", "sortedList", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
7405+
* console.log(sortedElements); // Output: 2 - number of elements sorted and stored
7406+
* console.log(await client.lrange("sortedList", 0, -1)); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age stored in `sortedList`
7407+
* ```
7408+
*/
7409+
public async sortStore(
7410+
key: GlideString,
7411+
destination: GlideString,
7412+
options?: SortOptions,
7413+
): Promise<number> {
7414+
return this.createWritePromise(createSort(key, options, destination));
7415+
}
7416+
73087417
/**
73097418
* @internal
73107419
*/

node/src/Commands.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,20 +3544,30 @@ export function createZIncrBy(
35443544
}
35453545

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

3565+
/**
3566+
* Limiting the range of the query by setting offset and result count. See {@link Limit} class for
3567+
* more information.
3568+
*/
3569+
limit?: Limit;
3570+
35613571
/**
35623572
* A pattern used to retrieve external keys' values, instead of the elements at `key`.
35633573
* The pattern should contain an asterisk (`*`) as a placeholder for the element values, where the
@@ -3570,16 +3580,9 @@ export type SortOptions = SortBaseOptions & {
35703580
* arguments can be provided to retrieve multiple attributes. The special value `#` can
35713581
* be used to include the actual element from `key` being sorted. If not provided, only
35723582
* the sorted elements themselves are returned.
3583+
* Supported in cluster mode since Valkey version 8.0 and above.
35733584
*/
35743585
getPatterns?: GlideString[];
3575-
};
3576-
3577-
interface SortBaseOptions {
3578-
/**
3579-
* Limiting the range of the query by setting offset and result count. See {@link Limit} class for
3580-
* more information.
3581-
*/
3582-
limit?: Limit;
35833586

35843587
/** Options for sorting order of elements. */
35853588
orderBy?: SortOrder;
@@ -3592,13 +3595,6 @@ interface SortBaseOptions {
35923595
isAlpha?: boolean;
35933596
}
35943597

3595-
/**
3596-
* Optional arguments to {@link GlideClusterClient.sort|sort}, {@link GlideClusterClient.sortStore|sortStore} and {@link GlideClusterClient.sortReadOnly|sortReadOnly} commands.
3597-
*
3598-
* See https://valkey.io/commands/sort/ for more details.
3599-
*/
3600-
export type SortClusterOptions = SortBaseOptions;
3601-
36023598
/**
36033599
* The `LIMIT` argument is commonly used to specify a subset of results from the
36043600
* matching elements, similar to the `LIMIT` clause in SQL (e.g., `SELECT LIMIT offset, count`).

node/src/GlideClient.ts

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
GlideReturnType,
1414
GlideString,
1515
PubSubMsg,
16-
ReadFrom, // eslint-disable-line @typescript-eslint/no-unused-vars
1716
} from "./BaseClient";
1817
import {
1918
createClientGetName,
@@ -44,8 +43,6 @@ import {
4443
createPublish,
4544
createRandomKey,
4645
createSelect,
47-
createSort,
48-
createSortReadOnly,
4946
createTime,
5047
createUnWatch,
5148
FlushMode,
@@ -55,7 +52,6 @@ import {
5552
FunctionStatsFullResponse,
5653
InfoOptions,
5754
LolwutOptions,
58-
SortOptions,
5955
} from "./Commands";
6056
import { connection_request } from "./ProtobufMessage";
6157
import { Transaction } from "./Transaction";
@@ -862,106 +858,6 @@ export class GlideClient extends BaseClient {
862858
return this.createWritePromise(createPublish(message, channel));
863859
}
864860

865-
/**
866-
* Sorts the elements in the list, set, or sorted set at `key` and returns the result.
867-
*
868-
* The `sort` command can be used to sort elements based on different criteria and
869-
* apply transformations on sorted elements.
870-
*
871-
* To store the result into a new key, see {@link sortStore}.
872-
*
873-
* @see {@link https://valkey.io/commands/sort/|valkey.io} for more details.
874-
*
875-
* @param key - The key of the list, set, or sorted set to be sorted.
876-
* @param options - (Optional) The {@link SortOptions} and {@link DecoderOption}.
877-
*
878-
* @returns An `Array` of sorted elements.
879-
*
880-
* @example
881-
* ```typescript
882-
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
883-
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
884-
* await client.lpush("user_ids", ["2", "1"]);
885-
* const result = await client.sort("user_ids", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
886-
* console.log(result); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age
887-
* ```
888-
*/
889-
public async sort(
890-
key: GlideString,
891-
options?: SortOptions & DecoderOption,
892-
): Promise<(GlideString | null)[]> {
893-
return this.createWritePromise(createSort(key, options), options);
894-
}
895-
896-
/**
897-
* Sorts the elements in the list, set, or sorted set at `key` and returns the result.
898-
*
899-
* The `sortReadOnly` command can be used to sort elements based on different criteria and
900-
* apply transformations on sorted elements.
901-
*
902-
* This command is routed depending on the client's {@link ReadFrom} strategy.
903-
*
904-
* @see {@link https://valkey.io/commands/sort/|valkey.io} for more details.
905-
* @remarks Since Valkey version 7.0.0.
906-
*
907-
* @param key - The key of the list, set, or sorted set to be sorted.
908-
* @param options - (Optional) The {@link SortOptions} and {@link DecoderOption}.
909-
* @returns An `Array` of sorted elements
910-
*
911-
* @example
912-
* ```typescript
913-
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
914-
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
915-
* await client.lpush("user_ids", ["2", "1"]);
916-
* const result = await client.sortReadOnly("user_ids", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
917-
* console.log(result); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age
918-
* ```
919-
*/
920-
public async sortReadOnly(
921-
key: GlideString,
922-
options?: SortOptions & DecoderOption,
923-
): Promise<(GlideString | null)[]> {
924-
return this.createWritePromise(
925-
createSortReadOnly(key, options),
926-
options,
927-
);
928-
}
929-
930-
/**
931-
* Sorts the elements in the list, set, or sorted set at `key` and stores the result in
932-
* `destination`.
933-
*
934-
* The `sort` command can be used to sort elements based on different criteria and
935-
* apply transformations on sorted elements, and store the result in a new key.
936-
*
937-
* To get the sort result without storing it into a key, see {@link sort} or {@link sortReadOnly}.
938-
*
939-
* @see {@link https://valkey.io/commands/sort|valkey.io} for more details.
940-
* @remarks When in cluster mode, `destination` and `key` must map to the same hash slot.
941-
*
942-
* @param key - The key of the list, set, or sorted set to be sorted.
943-
* @param destination - The key where the sorted result will be stored.
944-
* @param options - (Optional) The {@link SortOptions}.
945-
* @returns The number of elements in the sorted key stored at `destination`.
946-
*
947-
* @example
948-
* ```typescript
949-
* await client.hset("user:1", new Map([["name", "Alice"], ["age", "30"]]));
950-
* await client.hset("user:2", new Map([["name", "Bob"], ["age", "25"]]));
951-
* await client.lpush("user_ids", ["2", "1"]);
952-
* const sortedElements = await client.sortStore("user_ids", "sortedList", { byPattern: "user:*->age", getPattern: ["user:*->name"] });
953-
* console.log(sortedElements); // Output: 2 - number of elements sorted and stored
954-
* console.log(await client.lrange("sortedList", 0, -1)); // Output: [ 'Bob', 'Alice' ] - Returns a list of the names sorted by age stored in `sortedList`
955-
* ```
956-
*/
957-
public async sortStore(
958-
key: GlideString,
959-
destination: GlideString,
960-
options?: SortOptions,
961-
): Promise<number> {
962-
return this.createWritePromise(createSort(key, options, destination));
963-
}
964-
965861
/**
966862
* Returns `UNIX TIME` of the last DB save timestamp or startup timestamp if no save
967863
* was made since then.

0 commit comments

Comments
 (0)