Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: Add XRANGE command #2069

Merged
merged 21 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* Node: Added ZMPOP command ([#1994](https://github.com/valkey-io/valkey-glide/pull/1994))
* Node: Added ZINCRBY command ([#2009](https://github.com/valkey-io/valkey-glide/pull/2009))
* Node: Added BZMPOP command ([#2018](https://github.com/valkey-io/valkey-glide/pull/2018))
* Node: Added XRANGE command ([#2069](https://github.com/valkey-io/valkey-glide/pull/2069))
* Node: Added PFMERGE command ([#2053](https://github.com/valkey-io/valkey-glide/pull/2053))
* Node: Added WATCH and UNWATCH commands ([#2076](https://github.com/valkey-io/valkey-glide/pull/2076))
* Node: Added ZLEXCOUNT command ([#2022](https://github.com/valkey-io/valkey-glide/pull/2022))
Expand Down
8 changes: 4 additions & 4 deletions node/npm/glide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ function initialize() {
InsertPosition,
SetOptions,
ZaddOptions,
InfScoreBoundary,
ScoreBoundary,
InfBoundary,
Boundary,
UpdateOptions,
ProtocolVersion,
RangeByIndex,
Expand Down Expand Up @@ -218,8 +218,8 @@ function initialize() {
InsertPosition,
SetOptions,
ZaddOptions,
InfScoreBoundary,
ScoreBoundary,
InfBoundary,
Boundary,
UpdateOptions,
ProtocolVersion,
RangeByIndex,
Expand Down
74 changes: 57 additions & 17 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
BitOffsetOptions,
BitmapIndexType,
BitwiseOperation,
Boundary,
CoordOrigin, // eslint-disable-line @typescript-eslint/no-unused-vars
ExpireOptions,
GeoAddOptions,
Expand All @@ -41,7 +42,6 @@ import {
RangeByLex,
RangeByScore,
ReturnTypeXinfoStream,
ScoreBoundary,
ScoreFilter,
SearchOrigin,
SetOptions,
Expand Down Expand Up @@ -172,6 +172,7 @@ import {
createXGroupDelConsumer,
createXLen,
createXPending,
createXRange,
createXRead,
createXTrim,
createZAdd,
Expand Down Expand Up @@ -2818,6 +2819,45 @@ export class BaseClient {
return this.createWritePromise(scriptInvocation);
}

/**
* Returns stream entries matching a given range of entry IDs.
*
* See https://valkey.io/commands/xrange for more details.
*
* @param key - The key of the stream.
* @param start - The starting stream entry ID bound for the range.
* - Use `value` to specify a stream entry ID.
* - Use `isInclusive: false` to specify an exclusive bounded stream entry ID. This is only available starting with Valkey version 6.2.0.
* - Use `InfBoundary.NegativeInfinity` to start with the minimum available ID.
* @param end - The ending stream entry ID bound for the range.
* - Use `value` to specify a stream entry ID.
* - Use `isInclusive: false` to specify an exclusive bounded stream entry ID. This is only available starting with Valkey version 6.2.0.
* - Use `InfBoundary.PositiveInfinity` to end with the maximum available ID.
* @param count - An optional argument specifying the maximum count of stream entries to return.
* If `count` is not provided, all stream entries in the range will be returned.
* @returns A map of stream entry ids, to an array of entries, or `null` if `count` is negative.
*
* @example
* ```typescript
* await client.xadd("mystream", [["field1", "value1"]], {id: "0-1"});
* await client.xadd("mystream", [["field2", "value2"], ["field2", "value3"]], {id: "0-2"});
* console.log(await client.xrange("mystream", InfBoundary.NegativeInfinity, InfBoundary.PositiveInfinity));
* // Output:
* // {
* // "0-1": [["field1", "value1"]],
* // "0-2": [["field2", "value2"], ["field2", "value3"]],
* // } // Indicates the stream entry IDs and their associated field-value pairs for all stream entries in "mystream".
* ```
*/
public async xrange(
key: string,
start: Boundary<string>,
end: Boundary<string>,
count?: number,
): Promise<Record<string, [string, string][]> | null> {
return this.createWritePromise(createXRange(key, start, end, count));
}

/** Adds members with their scores to the sorted set stored at `key`.
* If a member is already a part of the sorted set, its score is updated.
* See https://valkey.io/commands/zadd/ for more details.
Expand Down Expand Up @@ -3115,7 +3155,7 @@ export class BaseClient {
* @example
* ```typescript
* // Example usage of the zcount method to count members in a sorted set within a score range
* const result = await client.zcount("my_sorted_set", { value: 5.0, isInclusive: true }, InfScoreBoundary.PositiveInfinity);
* const result = await client.zcount("my_sorted_set", { value: 5.0, isInclusive: true }, InfBoundary.PositiveInfinity);
* console.log(result); // Output: 2 - Indicates that there are 2 members with scores between 5.0 (inclusive) and +inf in the sorted set "my_sorted_set".
* ```
*
Expand All @@ -3128,8 +3168,8 @@ export class BaseClient {
*/
public zcount(
key: string,
minScore: ScoreBoundary<number>,
maxScore: ScoreBoundary<number>,
minScore: Boundary<number>,
maxScore: Boundary<number>,
): Promise<number> {
return this.createWritePromise(createZCount(key, minScore, maxScore));
}
Expand Down Expand Up @@ -3159,7 +3199,7 @@ export class BaseClient {
* ```typescript
* // Example usage of zrange method to retrieve members within a score range in ascending order
* const result = await client.zrange("my_sorted_set", {
* start: InfScoreBoundary.NegativeInfinity,
* start: InfBoundary.NegativeInfinity,
* stop: { value: 3, isInclusive: false },
* type: "byScore",
* });
Expand Down Expand Up @@ -3201,7 +3241,7 @@ export class BaseClient {
* ```typescript
* // Example usage of zrangeWithScores method to retrieve members within a score range with their scores
* const result = await client.zrangeWithScores("my_sorted_set", {
* start: InfScoreBoundary.NegativeInfinity,
* start: InfBoundary.NegativeInfinity,
* stop: { value: 3, isInclusive: false },
* type: "byScore",
* });
Expand Down Expand Up @@ -3247,7 +3287,7 @@ export class BaseClient {
* ```typescript
* // Example usage of zrangeStore method to retrieve members within a score range in ascending order and store in "destination_key"
* const result = await client.zrangeStore("destination_key", "my_sorted_set", {
* start: InfScoreBoundary.NegativeInfinity,
* start: InfBoundary.NegativeInfinity,
* stop: { value: 3, isInclusive: false },
* type: "byScore",
* });
Expand Down Expand Up @@ -3643,14 +3683,14 @@ export class BaseClient {
* @example
* ```typescript
* // Example usage of zremRangeByLex method when the sorted set does not exist
* const result = await client.zremRangeByLex("non_existing_sorted_set", InfScoreBoundary.NegativeInfinity, { value: "e" });
* const result = await client.zremRangeByLex("non_existing_sorted_set", InfBoundary.NegativeInfinity, { value: "e" });
* console.log(result); // Output: 0 - Indicates that no elements were removed.
* ```
*/
public zremRangeByLex(
key: string,
minLex: ScoreBoundary<string>,
maxLex: ScoreBoundary<string>,
minLex: Boundary<string>,
maxLex: Boundary<string>,
): Promise<number> {
return this.createWritePromise(
createZRemRangeByLex(key, minLex, maxLex),
Expand All @@ -3670,7 +3710,7 @@ export class BaseClient {
* @example
* ```typescript
* // Example usage of zremRangeByScore method to remove members from a sorted set based on score range
* const result = await client.zremRangeByScore("my_sorted_set", { value: 5.0, isInclusive: true }, InfScoreBoundary.PositiveInfinity);
* const result = await client.zremRangeByScore("my_sorted_set", { value: 5.0, isInclusive: true }, InfBoundary.PositiveInfinity);
* console.log(result); // Output: 2 - Indicates that 2 members with scores between 5.0 (inclusive) and +inf have been removed from the sorted set "my_sorted_set".
* ```
*
Expand All @@ -3683,8 +3723,8 @@ export class BaseClient {
*/
public zremRangeByScore(
key: string,
minScore: ScoreBoundary<number>,
maxScore: ScoreBoundary<number>,
minScore: Boundary<number>,
maxScore: Boundary<number>,
): Promise<number> {
return this.createWritePromise(
createZRemRangeByScore(key, minScore, maxScore),
Expand All @@ -3705,7 +3745,7 @@ export class BaseClient {
*
* @example
* ```typescript
* const result = await client.zlexcount("my_sorted_set", {value: "c"}, InfScoreBoundary.PositiveInfinity);
* const result = await client.zlexcount("my_sorted_set", {value: "c"}, InfBoundary.PositiveInfinity);
* console.log(result); // Output: 2 - Indicates that there are 2 members with lex scores between "c" (inclusive) and positive infinity in the sorted set "my_sorted_set".
* ```
*
Expand All @@ -3717,8 +3757,8 @@ export class BaseClient {
*/
public async zlexcount(
key: string,
minLex: ScoreBoundary<string>,
maxLex: ScoreBoundary<string>,
minLex: Boundary<string>,
maxLex: Boundary<string>,
): Promise<number> {
return this.createWritePromise(createZLexCount(key, minLex, maxLex));
}
Expand Down Expand Up @@ -3970,7 +4010,7 @@ export class BaseClient {
* ```typescript
* console.log(await client.xpending("my_stream", "my_group"), {
* start: { value: "0-1", isInclusive: true },
* end: InfScoreBoundary.PositiveInfinity,
* end: InfBoundary.PositiveInfinity,
* count: 2,
* consumer: "consumer1"
* }); // Output:
Expand Down
Loading
Loading