Skip to content

Commit 1c92e21

Browse files
feat: add json mset command (#1197)
* feat: add json mset command * fix: remove unused import
1 parent ac6f459 commit 1c92e21

File tree

6 files changed

+245
-67
lines changed

6 files changed

+245
-67
lines changed

biome.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
"performance": {
1111
"noDelete": "off"
1212
},
13-
"nursery": {
14-
"useAwait": "error"
15-
},
1613
"complexity": {
1714
"noBannedTypes": "off"
1815
},

pkg/commands/json_mset.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { afterAll, expect, test } from "bun:test";
2+
import { keygen, newHttpClient } from "../test-utils";
3+
4+
import { JsonGetCommand } from "./json_get";
5+
import { JsonMSetCommand } from "./json_mset";
6+
7+
const client = newHttpClient();
8+
9+
const { newKey, cleanup } = keygen();
10+
afterAll(cleanup);
11+
12+
test("add a new value", async () => {
13+
const key1 = newKey();
14+
const key2 = newKey();
15+
const key3 = newKey();
16+
const res1 = await new JsonMSetCommand([
17+
{ key: key1, path: "$", value: { key: "value" } },
18+
{ key: key2, path: "$", value: { key: "value" } },
19+
{ key: key3, path: "$", value: { key: "value" } },
20+
]).exec(client);
21+
expect(res1).toEqual("OK");
22+
});
23+
24+
test("replace an existing value", async () => {
25+
const key = newKey();
26+
const res1 = await new JsonMSetCommand([
27+
{ key, path: "$", value: { a: 2 } },
28+
]).exec(client);
29+
expect(res1).toEqual("OK");
30+
const res2 = await new JsonMSetCommand([{ key, path: "$.a", value: 3 }]).exec(
31+
client
32+
);
33+
expect(res2).toEqual("OK");
34+
const res3 = await new JsonGetCommand([key, "$"]).exec(client);
35+
expect(res3).toEqual([{ a: 3 }]);
36+
});
37+
38+
test("update multi-paths", async () => {
39+
const key = newKey();
40+
const data = {
41+
f1: { a: 1 },
42+
f2: { a: 2 },
43+
};
44+
const res1 = await new JsonMSetCommand([
45+
{ key, path: "$", value: data },
46+
]).exec(client);
47+
expect(res1).toEqual("OK");
48+
const res2 = await new JsonMSetCommand([
49+
{ key, path: "$..a", value: 3 },
50+
]).exec(client);
51+
expect(res2).toEqual("OK");
52+
const res3 = await new JsonGetCommand<any[]>([key, "$"]).exec(client);
53+
54+
expect(res3).not.toBeNull();
55+
expect(res3!.length).toEqual(1);
56+
expect(res3![0]?.f1?.a).toEqual(3);
57+
expect(res3![0]?.f2?.a).toEqual(3);
58+
});

pkg/commands/json_mset.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Command, CommandOptions } from "./command";
2+
3+
/**
4+
* @see https://redis.io/commands/json.mset
5+
*/
6+
export class JsonMSetCommand<
7+
TData extends
8+
| number
9+
| string
10+
| boolean
11+
| Record<string, unknown>
12+
| (number | string | boolean | Record<string, unknown>)[]
13+
> extends Command<"OK" | null, "OK" | null> {
14+
constructor(
15+
cmd: { key: string; path: string; value: TData }[],
16+
opts?: CommandOptions<"OK" | null, "OK" | null>
17+
) {
18+
const command: unknown[] = ["JSON.MSET"];
19+
20+
for (const c of cmd) {
21+
command.push(c.key, c.path, c.value);
22+
}
23+
super(command, opts);
24+
}
25+
}

pkg/commands/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export * from "./json_del";
5858
export * from "./json_forget";
5959
export * from "./json_get";
6060
export * from "./json_mget";
61+
export * from "./json_mset";
6162
export * from "./json_numincrby";
6263
export * from "./json_nummultby";
6364
export * from "./json_objkeys";

0 commit comments

Comments
 (0)