From f27bcca494e203f1b3c5b527d058f56e86ff47d3 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 5 Jun 2024 14:19:49 +0000 Subject: [PATCH] Node: add ZINTERSTORE command --- node/src/BaseClient.ts | 12 +++++++++++- node/src/Commands.ts | 14 ++++++++++---- node/src/Transaction.ts | 2 +- node/tests/RedisClusterClient.test.ts | 1 + 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index 751b7e900d..2f4b564578 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -1896,10 +1896,20 @@ export class BaseClient { * KeyWeight[] - for weighted keys with score multipliers. * @param aggregationType - Specifies the aggregation strategy to apply when combining the scores of elements. * @returns The number of elements in the resulting sorted set stored at `destination`. + * + * @example + * ```typescript + * // Example usage of zinterstore command with an existing key + * await client.zadd("key1", {"member1": 10.5, "member2": 8.2}) + * await client.zadd("key2", {"member1": 9.5}) + * await client.zinterstore("my_sorted_set", ["key1", "key2"]) // Output: 1 - Indicates that the sorted set "my_sorted_set" contains one element. + * await client.zrange_withscores("my_sorted_set", RangeByIndex(0, -1)) // Output: {'member1': 20} - "member1" is now stored in "my_sorted_set" with score of 20. + * await client.zinterstore("my_sorted_set", ["key1", "key2"] , AggregationType.MAX ) // Output: 1 - Indicates that the sorted set "my_sorted_set" contains one element, and it's score is the maximum score between the sets. + * await client.zrange_withscores("my_sorted_set", RangeByIndex(0, -1)) // Output: {'member1': 10.5} - "member1" is now stored in "my_sorted_set" with score of 10.5. */ public zinterstore( destination: string, - keys: (string | KeyWeight)[], + keys: string[] | KeyWeight[], aggregationType?: AggregationType, ): Promise { return this.createWritePromise( diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 76f2b84a53..2516c6b35e 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -827,7 +827,13 @@ export function createZAdd( return createCommand(RequestType.ZAdd, args); } +/** + * `KeyWeight` - pair of variables represents a weighted key for the `ZINTERSTORE` and `ZUNIONSTORE` sorted sets commands. + */ export type KeyWeight = [string, number]; +/** + * `AggregationType` - representing aggregation types for `ZINTERSTORE` and `ZUNIONSTORE` sorted set commands. + */ export type AggregationType = "SUM" | "MIN" | "MAX"; /** @@ -835,16 +841,16 @@ export type AggregationType = "SUM" | "MIN" | "MAX"; */ export function createZInterstore( destination: string, - keys: (string | KeyWeight)[], + keys: string[] | KeyWeight[], aggregationType?: AggregationType, ): redis_request.Command { - const args = createZInterstoreArgs(destination, keys, aggregationType); + const args = createZCmdStoreArgs(destination, keys, aggregationType); return createCommand(RequestType.ZInterStore, args); } -function createZInterstoreArgs( +function createZCmdStoreArgs( destination: string, - keys: (string | KeyWeight)[], + keys: string[] | KeyWeight[], aggregationType?: AggregationType, ): string[] { const args: string[] = [destination, keys.length.toString()]; diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index ece2890e7d..73f643738b 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -1053,7 +1053,7 @@ export class BaseTransaction> { */ public zinterstore( destination: string, - keys: (string | KeyWeight)[], + keys: string[] | KeyWeight[], aggregationType?: AggregationType, ): T { return this.addAndReturn( diff --git a/node/tests/RedisClusterClient.test.ts b/node/tests/RedisClusterClient.test.ts index 4ab7c49e1c..cbe5254b44 100644 --- a/node/tests/RedisClusterClient.test.ts +++ b/node/tests/RedisClusterClient.test.ts @@ -288,6 +288,7 @@ describe("RedisClusterClient", () => { client.smove("abc", "zxy", "value"), client.renamenx("abc", "zxy"), client.sinter(["abc", "zxy", "lkn"]), + client.zinterstore("abc", ["zxy", "lkn"]), // TODO all rest multi-key commands except ones tested below ];