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

feat: add json mset command #1197

Merged
merged 2 commits into from
Jul 22, 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: 0 additions & 3 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
"performance": {
"noDelete": "off"
},
"nursery": {
"useAwait": "error"
},
"complexity": {
"noBannedTypes": "off"
},
Expand Down
58 changes: 58 additions & 0 deletions pkg/commands/json_mset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { afterAll, expect, test } from "bun:test";
import { keygen, newHttpClient } from "../test-utils";

import { JsonGetCommand } from "./json_get";
import { JsonMSetCommand } from "./json_mset";

const client = newHttpClient();

const { newKey, cleanup } = keygen();
afterAll(cleanup);

test("add a new value", async () => {
const key1 = newKey();
const key2 = newKey();
const key3 = newKey();
const res1 = await new JsonMSetCommand([
{ key: key1, path: "$", value: { key: "value" } },
{ key: key2, path: "$", value: { key: "value" } },
{ key: key3, path: "$", value: { key: "value" } },
]).exec(client);
expect(res1).toEqual("OK");
});

test("replace an existing value", async () => {
const key = newKey();
const res1 = await new JsonMSetCommand([
{ key, path: "$", value: { a: 2 } },
]).exec(client);
expect(res1).toEqual("OK");
const res2 = await new JsonMSetCommand([{ key, path: "$.a", value: 3 }]).exec(
client
);
expect(res2).toEqual("OK");
const res3 = await new JsonGetCommand([key, "$"]).exec(client);
expect(res3).toEqual([{ a: 3 }]);
});

test("update multi-paths", async () => {
const key = newKey();
const data = {
f1: { a: 1 },
f2: { a: 2 },
};
const res1 = await new JsonMSetCommand([
{ key, path: "$", value: data },
]).exec(client);
expect(res1).toEqual("OK");
const res2 = await new JsonMSetCommand([
{ key, path: "$..a", value: 3 },
]).exec(client);
expect(res2).toEqual("OK");
const res3 = await new JsonGetCommand<any[]>([key, "$"]).exec(client);

expect(res3).not.toBeNull();
expect(res3!.length).toEqual(1);
expect(res3![0]?.f1?.a).toEqual(3);
expect(res3![0]?.f2?.a).toEqual(3);
});
25 changes: 25 additions & 0 deletions pkg/commands/json_mset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Command, CommandOptions } from "./command";

/**
* @see https://redis.io/commands/json.mset
*/
export class JsonMSetCommand<
TData extends
| number
| string
| boolean
| Record<string, unknown>
| (number | string | boolean | Record<string, unknown>)[]
> extends Command<"OK" | null, "OK" | null> {
constructor(
cmd: { key: string; path: string; value: TData }[],
opts?: CommandOptions<"OK" | null, "OK" | null>
) {
const command: unknown[] = ["JSON.MSET"];

for (const c of cmd) {
command.push(c.key, c.path, c.value);
}
super(command, opts);
}
}
1 change: 1 addition & 0 deletions pkg/commands/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export * from "./json_del";
export * from "./json_forget";
export * from "./json_get";
export * from "./json_mget";
export * from "./json_mset";
export * from "./json_numincrby";
export * from "./json_nummultby";
export * from "./json_objkeys";
Expand Down
Loading
Loading