Skip to content

Commit f22615f

Browse files
committed
feat: add delex command and tests
1 parent 90ab01f commit f22615f

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { strict as assert } from "node:assert";
2+
import DELEX, { DelexCondition } from "./DELEX";
3+
import { parseArgs } from "./generic-transformers";
4+
import testUtils, { GLOBAL } from "../test-utils";
5+
6+
describe("DELEX", () => {
7+
describe("transformArguments", () => {
8+
it("no condition", () => {
9+
assert.deepEqual(parseArgs(DELEX, "key"), ["DELEX", "key"]);
10+
});
11+
12+
it("with condition", () => {
13+
assert.deepEqual(
14+
parseArgs(DELEX, "key", {
15+
condition: DelexCondition.IFEQ,
16+
matchValue: "some-value",
17+
}),
18+
["DELEX", "key", "IFEQ", "some-value"]
19+
);
20+
});
21+
});
22+
23+
testUtils.testAll(
24+
"non-existing key",
25+
async (client) => {
26+
assert.equal(await client.delEx("key{tag}"), 0);
27+
},
28+
{
29+
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 4] },
30+
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 4] },
31+
}
32+
);
33+
34+
testUtils.testAll(
35+
"non-existing key with condition",
36+
async (client) => {
37+
assert.equal(
38+
await client.delEx("key{tag}", {
39+
condition: DelexCondition.IFDEQ,
40+
matchValue: "digest",
41+
}),
42+
0
43+
);
44+
},
45+
{
46+
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 4] },
47+
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 4] },
48+
}
49+
);
50+
51+
testUtils.testAll(
52+
"existing key no condition",
53+
async (client) => {
54+
await client.set("key{tag}", "value");
55+
assert.equal(await client.delEx("key{tag}"), 1);
56+
},
57+
{
58+
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 4] },
59+
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 4] },
60+
}
61+
);
62+
63+
testUtils.testAll(
64+
"existing key and condition",
65+
async (client) => {
66+
await client.set("key{tag}", "some-value");
67+
68+
assert.equal(
69+
await client.delEx("key{tag}", {
70+
condition: DelexCondition.IFEQ,
71+
matchValue: "some-value",
72+
}),
73+
1
74+
);
75+
},
76+
{
77+
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 4] },
78+
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 4] },
79+
}
80+
);
81+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { CommandParser } from "../client/parser";
2+
import { NumberReply, Command, RedisArgument } from "../RESP/types";
3+
4+
export const DelexCondition = {
5+
/**
6+
* Delete if value equals match-value.
7+
*/
8+
IFEQ: "IFEQ",
9+
/**
10+
* Delete if value does not equal match-value.
11+
*/
12+
IFNE: "IFNE",
13+
/**
14+
* Delete if value digest equals match-digest.
15+
*/
16+
IFDEQ: "IFDEQ",
17+
/**
18+
* Delete if value digest does not equal match-digest.
19+
*/
20+
IFDNE: "IFDNE",
21+
} as const;
22+
23+
type DelexCondition = (typeof DelexCondition)[keyof typeof DelexCondition];
24+
25+
export default {
26+
IS_READ_ONLY: false,
27+
/**
28+
* Conditionally removes the specified key based on value or digest comparison.
29+
*
30+
* @param parser - The Redis command parser
31+
* @param key - Key to delete
32+
*/
33+
parseCommand(
34+
parser: CommandParser,
35+
key: RedisArgument,
36+
options?: {
37+
/**
38+
* The condition to apply when deleting the key.
39+
* - `IFEQ` - Delete if value equals match-value
40+
* - `IFNE` - Delete if value does not equal match-value
41+
* - `IFDEQ` - Delete if value digest equals match-digest
42+
* - `IFDNE` - Delete if value digest does not equal match-digest
43+
*/
44+
condition: DelexCondition;
45+
/**
46+
* The value or digest to compare against
47+
*/
48+
matchValue: RedisArgument;
49+
}
50+
) {
51+
parser.push("DELEX");
52+
parser.pushKey(key);
53+
54+
if (options) {
55+
parser.push(options.condition);
56+
parser.push(options.matchValue);
57+
}
58+
},
59+
transformReply: undefined as unknown as () => NumberReply<1 | 0>,
60+
} as const satisfies Command;

packages/client/lib/commands/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import DBSIZE from './DBSIZE';
8484
import DECR from './DECR';
8585
import DECRBY from './DECRBY';
8686
import DEL from './DEL';
87+
import DELEX from './DELEX';
8788
import DIGEST from './DIGEST';
8889
import DUMP from './DUMP';
8990
import ECHO from './ECHO';
@@ -544,6 +545,8 @@ export default {
544545
decrBy: DECRBY,
545546
DEL,
546547
del: DEL,
548+
DELEX,
549+
delEx: DELEX,
547550
DIGEST,
548551
digest: DIGEST,
549552
DUMP,

0 commit comments

Comments
 (0)