Skip to content

Commit

Permalink
docstring + add test
Browse files Browse the repository at this point in the history
Signed-off-by: Maayan Shani <[email protected]>
  • Loading branch information
Maayanshani25 committed Jan 14, 2025
1 parent 3acc0d9 commit c9016bf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
7 changes: 5 additions & 2 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1496,8 +1496,11 @@ export class BaseClient {
* console.log(result4); // Output: 'new_value' - Value wasn't modified back to being "value" because of "NX" flag.
*
* // Example usage of set method with conditional option IFEQ
* const result5 = await client.set("key", "ifeq_value", {conditionalSet: "onlyIfEqual", comparisonValue: "new_value");
* console.log(result5); // Output: 'OK' - Set "ifeq_value" to "key" only if comparisonValue is equal to the value of "key".
* await client.set("key", "value we will compare to");
* const result5 = await client.set("key", "new_value", {conditionalSet: "onlyIfEqual", comparisonValue: "value we will compare to"});
* console.log(result5); // Output: 'OK' - Set "new_value" to "key" only if comparisonValue is equal to the current value of "key".
* const result6 = await client.set("key", "another_new_value", {conditionalSet: "onlyIfEqual", comparisonValue: "value we will compare to"});
* console.log(result6); // Output: `null` - Value wasn't set because the comparisonValue is not equal to the current value of "key". Value of "key" remains "new_value".
* ```
*/
public async set(
Expand Down
8 changes: 4 additions & 4 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ export type SetOptions = (
| {
/**
* `onlyIfDoesNotExist` - Only set the key if it does not already exist.
* Equivalent to `NX` in the Valkey API.
* `NX` in the Valkey API.
*
* `onlyIfExists` - Only set the key if it already exists.
* Equivalent to `EX` in the Valkey API.
* `EX` in the Valkey API.
*/
conditionalSet?: "onlyIfExists" | "onlyIfDoesNotExist";
}
| {
/**
* `onlyIfEqual` - Only set the key if the comparison value equals the key value.
* Equivalent to `IFEQ` in the Valkey API.
* `onlyIfEqual` - Only set the key if the comparison value equals the current value of key.
* `IFEQ` in the Valkey API.
*/
conditionalSet: "onlyIfEqual";
/**
Expand Down
16 changes: 16 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8284,6 +8284,22 @@ export function runBaseTests(config: {
expect(setResWithAllOptions).toEqual(initialValue);
// newValue should be set as the key value
expect(await client.get(key)).toEqual(newValue);

// fail command
const wrongValue = "wrong value";
const setResFailedWithAllOptions = await client.set(key, wrongValue, {
expiry: {
type: TimeUnit.UnixSeconds,
count: Math.floor(Date.now() / 1000) + 1,
},
conditionalSet: "onlyIfEqual",
comparisonValue: wrongValue,
returnOldValue: true,
});
// current value of key should be newValue
expect(setResFailedWithAllOptions).toEqual(newValue);
// key should not be set. it remains the same
expect(await client.get(key)).toEqual(newValue);
}

async function testSetWithAllCombination(
Expand Down

0 comments on commit c9016bf

Please sign in to comment.