diff --git a/CHANGELOG.md b/CHANGELOG.md index a93b96f61f..0f17a6d306 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ * Python, Node: Added HLEN command ([#944](https://github.com/aws/glide-for-redis/pull/944), [#981](https://github.com/aws/glide-for-redis/pull/981)) * Node: Added ZCOUNT command ([#909](https://github.com/aws/glide-for-redis/pull/909)) * Python: Added ECHO command ([#953](https://github.com/aws/glide-for-redis/pull/953)) -* Python: Added ZPOPMIN command ([#975](https://github.com/aws/glide-for-redis/pull/975)) +* Python, Node: Added ZPOPMIN command ([#975](https://github.com/aws/glide-for-redis/pull/975), [#1008](https://github.com/aws/glide-for-redis/pull/1008)) * Node: Added STRLEN command ([#993](https://github.com/aws/glide-for-redis/pull/993)) * Node: Added LINDEX command ([#999](https://github.com/aws/glide-for-redis/pull/999)) * Python: Added ZPOPMAX command ([#996](https://github.com/aws/glide-for-redis/pull/996)) diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index 161bec9122..ef5b1f2835 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -59,6 +59,7 @@ import { createZadd, createZcard, createZcount, + createZpopmin, createZrem, createZscore, } from "./Commands"; @@ -1100,6 +1101,24 @@ export class BaseClient { return this.createWritePromise(createType(key)); } + /** Removes and returns the members with the lowest scores from the sorted set stored at `key`. + * If `count` is provided, up to `count` members with the lowest scores are removed and returned. + * Otherwise, only one member with the lowest score is removed and returned. + * See https://redis.io/commands/zpopmin for more details. + * + * @param key - The key of the sorted set. + * @param count - Specifies the quantity of members to pop. If not specified, pops one member. + * If `count` is higher than the sorted set's cardinality, returns all members and their scores. + * @returns A map of the removed members and their scores, ordered from the one with the lowest score to the one with the highest. + * If `key` doesn't exist, it will be treated as an empty sorted set and the command returns an empty map. + */ + public zpopmin( + key: string, + count?: number + ): Promise> { + return this.createWritePromise(createZpopmin(key, count)); + } + private readonly MAP_READ_FROM_STRATEGY: Record< ReadFrom, connection_request.ReadFrom diff --git a/node/src/Commands.ts b/node/src/Commands.ts index a1adda9549..b5f772e600 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -847,3 +847,11 @@ export function createLindex( ): redis_request.Command { return createCommand(RequestType.Lindex, [key, index.toString()]); } + +/** + * @internal + */ +export function createZpopmin(key: string, count?: number): redis_request.Command { + const args: string[] = count == undefined ? [key] : [key, count.toString()]; + return createCommand(RequestType.ZPopMin, args); +} diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index ff2ce50895..a7e70e9c73 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -41,6 +41,7 @@ import { createLRange, createLRem, createLTrim, + createLindex, createMGet, createMSet, createPExpire, @@ -61,9 +62,9 @@ import { createZadd, createZcard, createZcount, + createZpopmin, createZrem, createZscore, - createLindex, } from "./Commands"; import { redis_request } from "./ProtobufMessage"; @@ -858,6 +859,7 @@ export class BaseTransaction> { * See https://redis.io/commands/strlen/ for more details. * * @param key - The `key` to check its length. + * * 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. */ @@ -865,6 +867,22 @@ export class BaseTransaction> { return this.addAndReturn(createStrlen(key)); } + /** Removes and returns the members with the lowest scores from the sorted set stored at `key`. + * If `count` is provided, up to `count` members with the lowest scores are removed and returned. + * Otherwise, only one member with the lowest score is removed and returned. + * See https://redis.io/commands/zpopmin for more details. + * + * @param key - The key of the sorted set. + * @param count - Specifies the quantity of members to pop. If not specified, pops one member. + * If `count` is higher than the sorted set's cardinality, returns all members and their scores. + * + * Command Response - A map of the removed members and their scores, ordered from the one with the lowest score to the one with the highest. + * If `key` doesn't exist, it will be treated as an empty sorted set and the command returns an empty map. + */ + public zpopmin(key: string, count?: number): T { + return this.addAndReturn(createZpopmin(key, count)); + } + /** Executes a single command, without checking inputs. Every part of the command, including subcommands, * should be added as a separate value in args. * diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index 18e898c32f..022eca1fba 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -1557,6 +1557,7 @@ export function runBaseTests(config: { }, config.timeout ); + it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `lindex test_%p`, async (protocol) => { @@ -1575,6 +1576,27 @@ export function runBaseTests(config: { }, config.timeout ); + + it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( + `zpopmin test_%p`, + async (protocol) => { + await runTest(async (client: BaseClient) => { + const key = uuidv4(); + const membersScores = { a: 1, b: 2, c: 3 }; + expect(await client.zadd(key, membersScores)).toEqual(3); + expect(await client.zpopmin(key)).toEqual({ a: 1.0 }); + expect(await client.zpopmin(key, 3)).toEqual({ + b: 2.0, + c: 3.0, + }); + expect(await client.zpopmin(key)).toEqual({}); + expect(await client.set(key, "value")).toEqual("OK"); + await expect(client.zpopmin(key)).rejects.toThrow(); + expect(await client.zpopmin("notExsitingKey")).toEqual({}); + }, protocol); + }, + config.timeout + ); } export function runCommonTests(config: { diff --git a/node/tests/TestUtilities.ts b/node/tests/TestUtilities.ts index af4c971d51..3e5b6377f7 100644 --- a/node/tests/TestUtilities.ts +++ b/node/tests/TestUtilities.ts @@ -143,6 +143,8 @@ export function transactionTest( args.push(3.0); baseTransaction.zcount(key8, { bound: 2 }, "positiveInfinity"); args.push(1); + baseTransaction.zpopmin(key8) + args.push({"member2": 3.0}) return args; }