Skip to content

Commit 2591fcd

Browse files
committed
feat: add more conditional options to SET update tests
1 parent f22615f commit 2591fcd

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/client/lib/commands/SET.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
import { strict as assert } from 'node:assert';
23
import testUtils, { GLOBAL } from '../test-utils';
34
import SET from './SET';
@@ -127,6 +128,16 @@ describe('SET', () => {
127128
['SET', 'key', 'value', 'XX']
128129
);
129130
});
131+
132+
it('with IFDEQ condition', () => {
133+
assert.deepEqual(
134+
parseArgs(SET, 'key', 'value', {
135+
condition: 'IFDEQ',
136+
matchValue: 'some-value'
137+
}),
138+
['SET', 'key', 'value', 'IFDEQ', 'some-value']
139+
);
140+
});
130141
});
131142

132143
it('with GET', () => {
@@ -162,4 +173,19 @@ describe('SET', () => {
162173
client: GLOBAL.SERVERS.OPEN,
163174
cluster: GLOBAL.CLUSTERS.OPEN
164175
});
176+
177+
testUtils.testAll('set with IFEQ', async client => {
178+
await client.set('key{tag}', 'some-value');
179+
180+
assert.equal(
181+
await client.set('key{tag}', 'some-value', {
182+
condition: 'IFEQ',
183+
matchValue: 'some-value'
184+
}),
185+
'OK'
186+
);
187+
}, {
188+
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 4] },
189+
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 4] },
190+
});
165191
});

packages/client/lib/commands/SET.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,22 @@ export interface SetOptions {
2929
*/
3030
KEEPTTL?: boolean;
3131

32-
condition?: 'NX' | 'XX';
32+
/**
33+
* Condition for setting the key:
34+
* - `NX` - Set if key does not exist
35+
* - `XX` - Set if key already exists
36+
* - `IFEQ` - Set if current value equals match-value (since 8.4, requires `matchValue`)
37+
* - `IFNE` - Set if current value does not equal match-value (since 8.4, requires `matchValue`)
38+
* - `IFDEQ` - Set if current value digest equals match-digest (since 8.4, requires `matchValue`)
39+
* - `IFDNE` - Set if current value digest does not equal match-digest (since 8.4, requires `matchValue`)
40+
*/
41+
condition?: 'NX' | 'XX' | 'IFEQ' | 'IFNE' | 'IFDEQ' | 'IFDNE';
42+
43+
/**
44+
* Value or digest to compare against. Required when using `IFEQ`, `IFNE`, `IFDEQ`, or `IFDNE` conditions.
45+
*/
46+
matchValue?: RedisArgument;
47+
3348
/**
3449
* @deprecated Use `{ condition: 'NX' }` instead.
3550
*/
@@ -82,6 +97,9 @@ export default {
8297

8398
if (options?.condition) {
8499
parser.push(options.condition);
100+
if (options?.matchValue !== undefined) {
101+
parser.push(options.matchValue);
102+
}
85103
} else if (options?.NX) {
86104
parser.push('NX');
87105
} else if (options?.XX) {

0 commit comments

Comments
 (0)