Skip to content

Commit

Permalink
Node: Add command JSON.RESP
Browse files Browse the repository at this point in the history
Signed-off-by: TJ Zhang <tj.zhang@improving.com>
  • Loading branch information
TJ Zhang committed Oct 24, 2024
1 parent 50786a3 commit ec8a787
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions node/src/server-modules/GlideJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,45 @@ export class GlideJson {

return _executeCommand<number>(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<ReturnTypeJson<GlideString>> {
const args = ["JSON.RESP", key];

if (options) {
args.push(options.path);
}

return _executeCommand<ReturnTypeJson<GlideString>>(client, args);
}
}

0 comments on commit ec8a787

Please sign in to comment.