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: fix ZADD bug #1995

Merged
merged 5 commits into from
Jul 23, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
* Node: Added FUNCTION LOAD command ([#1969](https://github.com/valkey-io/valkey-glide/pull/1969))
* Node: Added FUNCTION FLUSH command ([#1984](https://github.com/valkey-io/valkey-glide/pull/1984))

#### Fixes
* Node: Fix ZADD bug where command could not be called with only the `changed` optional parameter ([#1995](https://github.com/valkey-io/valkey-glide/pull/1995))

## 1.0.0 (2024-07-09)

#### Changes
Expand Down
15 changes: 4 additions & 11 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2186,37 +2186,30 @@ export class BaseClient {
* @param key - The key of the sorted set.
* @param membersScoresMap - A mapping of members to their corresponding scores.
* @param options - The ZAdd options.
* @param changed - Modify the return value from the number of new elements added, to the total number of elements changed.
* @returns The number of elements added to the sorted set.
* If `changed` is set, returns the number of elements updated in the sorted set.
*
* @example
* ```typescript
* // Example usage of the zadd method to add elements to a sorted set
* const result = await client.zadd("my_sorted_set", \{ "member1": 10.5, "member2": 8.2 \});
* const result = await client.zadd("my_sorted_set", { "member1": 10.5, "member2": 8.2 });
* console.log(result); // Output: 2 - Indicates that two elements have been added to the sorted set "my_sorted_set."
* ```
*
* @example
* ```typescript
* // Example usage of the zadd method to update scores in an existing sorted set
* const result = await client.zadd("existing_sorted_set", { member1: 15.0, member2: 5.5 }, options={ conditionalChange: "onlyIfExists" } , changed=true);
* const result = await client.zadd("existing_sorted_set", { member1: 15.0, member2: 5.5 }, { conditionalChange: "onlyIfExists", changed: true });
* console.log(result); // Output: 2 - Updates the scores of two existing members in the sorted set "existing_sorted_set."
* ```
*/
public zadd(
key: string,
membersScoresMap: Record<string, number>,
options?: ZAddOptions,
changed?: boolean,
): Promise<number> {
return this.createWritePromise(
createZAdd(
key,
membersScoresMap,
options,
changed ? "CH" : undefined,
),
createZAdd(key, membersScoresMap, options),
);
}

Expand Down Expand Up @@ -2253,7 +2246,7 @@ export class BaseClient {
options?: ZAddOptions,
): Promise<number | null> {
return this.createWritePromise(
createZAdd(key, { [member]: increment }, options, "INCR"),
createZAdd(key, { [member]: increment }, options, true),
);
}

Expand Down
14 changes: 11 additions & 3 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,10 @@ export type ZAddOptions = {
* is greater than the current score. Equivalent to `GT` in the Redis API.
*/
updateOptions?: "scoreLessThanCurrent" | "scoreGreaterThanCurrent";
/**
* Modify the return value from the number of new elements added, to the total number of elements changed.
*/
changed?: boolean;
};

/**
Expand All @@ -937,7 +941,7 @@ export function createZAdd(
key: string,
membersScoresMap: Record<string, number>,
options?: ZAddOptions,
changedOrIncr?: "CH" | "INCR",
incr: boolean = false,
): command_request.Command {
let args = [key];

Expand All @@ -959,10 +963,14 @@ export function createZAdd(
} else if (options.updateOptions === "scoreGreaterThanCurrent") {
args.push("GT");
}

if (options.changed) {
args.push("CH");
}
}

if (changedOrIncr) {
args.push(changedOrIncr);
if (incr) {
args.push("INCR");
}

args = args.concat(
Expand Down
13 changes: 2 additions & 11 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,6 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* @param key - The key of the sorted set.
* @param membersScoresMap - A mapping of members to their corresponding scores.
* @param options - The ZAdd options.
* @param changed - Modify the return value from the number of new elements added, to the total number of elements changed.
*
* Command Response - The number of elements added to the sorted set.
* If `changed` is set, returns the number of elements updated in the sorted set.
Expand All @@ -1118,16 +1117,8 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
key: string,
membersScoresMap: Record<string, number>,
options?: ZAddOptions,
changed?: boolean,
): T {
return this.addAndReturn(
createZAdd(
key,
membersScoresMap,
options,
changed ? "CH" : undefined,
),
);
return this.addAndReturn(createZAdd(key, membersScoresMap, options));
}

/** Increments the score of member in the sorted set stored at `key` by `increment`.
Expand All @@ -1150,7 +1141,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
options?: ZAddOptions,
): T {
return this.addAndReturn(
createZAdd(key, { [member]: increment }, options, "INCR"),
createZAdd(key, { [member]: increment }, options, true),
);
}

Expand Down
28 changes: 12 additions & 16 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2061,9 +2061,13 @@ export function runBaseTests<Context>(config: {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
const membersScores = { one: 1, two: 2, three: 3 };
const newMembersScores = { one: 2, two: 3 };

expect(await client.zadd(key, membersScores)).toEqual(3);
expect(await client.zaddIncr(key, "one", 2)).toEqual(3.0);
expect(
await client.zadd(key, newMembersScores, { changed: true }),
).toEqual(2);
}, protocol);
},
config.timeout,
Expand Down Expand Up @@ -2114,25 +2118,17 @@ export function runBaseTests<Context>(config: {
membersScores["one"] = 10;

expect(
await client.zadd(
key,
membersScores,
{
updateOptions: "scoreGreaterThanCurrent",
},
true,
),
await client.zadd(key, membersScores, {
updateOptions: "scoreGreaterThanCurrent",
changed: true,
}),
).toEqual(1);

expect(
await client.zadd(
key,
membersScores,
{
updateOptions: "scoreLessThanCurrent",
},
true,
),
await client.zadd(key, membersScores, {
updateOptions: "scoreLessThanCurrent",
changed: true,
}),
).toEqual(0);

expect(
Expand Down
Loading