From ec8a787bb71675db6d68f0cb24fb53f85d2a1321 Mon Sep 17 00:00:00 2001 From: TJ Zhang Date: Thu, 24 Oct 2024 15:26:20 -0700 Subject: [PATCH] Node: Add command JSON.RESP Signed-off-by: TJ Zhang --- node/src/server-modules/GlideJson.ts | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/node/src/server-modules/GlideJson.ts b/node/src/server-modules/GlideJson.ts index 0e2f10228a..53c229e221 100644 --- a/node/src/server-modules/GlideJson.ts +++ b/node/src/server-modules/GlideJson.ts @@ -308,4 +308,45 @@ export class GlideJson { return _executeCommand(client, args); } + + /** + * Retrieve the JSON value at the specified `path` within the JSON document stored at + * `key`. The returning result is in the Valkey or Redis OSS Serialization Protocol (RESP). + * + * @param client - The client to execute the command. + * @param key - The key of the JSON document. + * @param options - (Optional) Additional parameters: + * - (Optional) path - The path within the JSON document, Defaults to root if not provided. + * @returns ReturnTypeJson: + * - For JSONPath (path starts with `$`): + * - Returns an array of replies for every possible path, indicating the RESP form of the JSON value. + * If `path` doesn't exist, returns an empty array. + * - For legacy path (path doesn't start with `$`): + * - Returns a single reply for the JSON value at the specified `path`, in its RESP form. + * If multiple paths match, the value of the first JSON value match is returned. If `path` doesn't exist, an error is raised. + * - If `key` doesn't exist, `null` is returned. + * + * @example + * ```typescript + * console.log(await GlideJson.set(client, "doc", ".", "{a: [1, 2, 3], b: {a: [1, 2], c: {a: 42}}}")); + * // Output: 'OK' - Indicates successful setting of the value at path '.' in the key stored at `doc`. + * const result = await GlideJson.resp(client, "doc", "$..a"); + * console.log(result); + * // Output: [ ["[", 1L, 2L, 3L], ["[", 1L, 2L], [42L]]; + * console.log(await GlideJson.type(client, "doc", "..a")); // Output: ["[", 1L, 2L, 3L] + * ``` + */ + static async resp( + client: BaseClient, + key: GlideString, + options?: { path: GlideString }, + ): Promise> { + const args = ["JSON.RESP", key]; + + if (options) { + args.push(options.path); + } + + return _executeCommand>(client, args); + } }