From c7f126925f50df6caaddd879a5e8bc3c953ec939 Mon Sep 17 00:00:00 2001 From: TJ Zhang Date: Mon, 7 Oct 2024 22:40:53 -0700 Subject: [PATCH] Node: Add command JSON.GET and JSON.SET Signed-off-by: TJ Zhang --- node/src/Commands.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 811504301c..f61ceb2834 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -4141,3 +4141,54 @@ export function createScriptFlush(mode?: FlushMode): command_request.Command { export function createScriptKill(): command_request.Command { return createCommand(RequestType.ScriptKill, []); } + +/** Represents options for formatting JSON data, to be used in the [JSON.GET](https://redis.io/commands/json.get/) command. + Args: + indent (Optional[str]): Sets an indentation string for nested levels. Defaults to None. + newline (Optional[str]): Sets a string that's printed at the end of each line. Defaults to None. + space (Optional[str]): Sets a string that's put between a key and a value. Defaults to None. */ +export type JsonGetOptions = { + paths?: string | string[]; + indent?: string; + newline?: string; + space?: string; +}; + +function jsonGetOptionsToArgs(options: JsonGetOptions): string[] { + const result: string[] = []; + + if (options.paths !== undefined) { + result.push(...options.paths); + } + + if (options.indent !== undefined) { + result.push("INDENT", options.indent); + } + + if (options.newline !== undefined) { + result.push("NEWLINE", options.newline); + } + + if (options.space !== undefined) { + result.push("SPACE", options.space); + } + + return result; +} + +/** + * @internal + */ +export function createJsonGet( + key: string, + options?: JsonGetOptions, +): command_request.Command { + if (options) { + return createCommand( + RequestType.ScriptFlush, + jsonGetOptionsToArgs(options), + ); + } else { + return createCommand(RequestType.ScriptFlush, []); + } +}