Skip to content

Commit

Permalink
Node: JSON.MGET. (#2567)
Browse files Browse the repository at this point in the history
* Add `JSON.MGET` to the Node client

---------

Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
  • Loading branch information
Yury-Fridlyand authored and prateek-kumar-improving committed Nov 14, 2024
1 parent 112885e commit 4db12f4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* Java: Added `FT.EXPLAIN`, `FT.EXPLAINCLI` ([#2515](https://github.com/valkey-io/valkey-glide/pull/2515))
* Core: Update routing for commands from server modules ([#2461](https://github.com/valkey-io/valkey-glide/pull/2461))
* Node: Added `JSON.SET` and `JSON.GET` ([#2427](https://github.com/valkey-io/valkey-glide/pull/2427))
* Node: Added `JSON.MGET` ([#2567](https://github.com/valkey-io/valkey-glide/pull/2567))
* Java: Added `JSON.NUMINCRBY` and `JSON.NUMMULTBY` ([#2511](https://github.com/valkey-io/valkey-glide/pull/2511))
* Java: Added `JSON.ARRAPPEND` ([#2489](https://github.com/valkey-io/valkey-glide/pull/2489))
* Java: Added `JSON.ARRTRIM` ([#2518](https://github.com/valkey-io/valkey-glide/pull/2518))
Expand Down
43 changes: 42 additions & 1 deletion node/src/server-modules/GlideJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,47 @@ export class GlideJson {
return _executeCommand(client, args, options);
}

/**
* Retrieves the JSON values at the specified `path` stored at multiple `keys`.
*
* @remarks When in cluster mode, if keys in `keyValueMap` map to different hash slots, the command
* will be split across these slots and executed separately for each. This means the command
* is atomic only at the slot level. If one or more slot-specific requests fail, the entire
* call will return the first encountered error, even though some requests may have succeeded
* while others did not. If this behavior impacts your application logic, consider splitting
* the request into sub-requests per slot to ensure atomicity.
*
* @param client - The client to execute the command.
* @param keys - The keys of the JSON documents.
* @param path - The path within the JSON documents.
* @param options - (Optional) See {@link DecoderOption}.
* @returns
* - For JSONPath (path starts with `$`):
* Returns a stringified JSON list replies for every possible path, or a string representation
* of an empty array, if path doesn't exist.
* - For legacy path (path doesn't start with `$`):
* Returns a string representation of the value in `path`. If `path` doesn't exist,
* the corresponding array element will be `null`.
* - If a `key` doesn't exist, the corresponding array element will be `null`.
*
* @example
* ```typescript
* await GlideJson.set(client, "doc1", "$", '{"a": 1, "b": ["one", "two"]}');
* await GlideJson.set(client, "doc2", "$", '{"a": 1, "c": false}');
* const res = await GlideJson.mget(client, [ "doc1", "doc2", "doc3" ], "$.c");
* console.log(res); // Output: ["[]", "[false]", null]
* ```
*/
static async mget(
client: BaseClient,
keys: GlideString[],
path: GlideString,
options?: DecoderOption,
): Promise<GlideString[]> {
const args = ["JSON.MGET", ...keys, path];
return _executeCommand(client, args, options);
}

/**
* Inserts one or more values into the array at the specified `path` within the JSON
* document stored at `key`, before the given `index`.
Expand Down Expand Up @@ -742,7 +783,7 @@ export class GlideJson {
* @returns
* - For JSONPath (path starts with `$`):
* - Returns a list of integer replies for every possible path, indicating the length of
* the JSON string value, or <code>null</code> for JSON values matching the path that
* the JSON string value, or `null` for JSON values matching the path that
* are not string.
* - For legacy path (path doesn't start with `$`):
* - Returns the length of the JSON value at `path` or `null` if `key` doesn't exist.
Expand Down
35 changes: 35 additions & 0 deletions node/tests/ServerModules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,41 @@ describe("Server Module Tests", () => {
expect(result3).toEqual(expectedResult2);
});

it("json.mget", async () => {
client = await GlideClusterClient.createClient(
getClientConfigurationOption(
cluster.getAddresses(),
protocol,
),
);
const key1 = uuidv4();
const key2 = uuidv4();
const data = {
[key1]: '{"a": 1, "b": ["one", "two"]}',
[key2]: '{"a": 1, "c": false}',
};

for (const key of Object.keys(data)) {
await GlideJson.set(client, key, ".", data[key]);
}

expect(
await GlideJson.mget(
client,
[key1, key2, uuidv4()],
Buffer.from("$.c"),
),
).toEqual(["[]", "[false]", null]);
expect(
await GlideJson.mget(
client,
[Buffer.from(key1), key2],
".b[*]",
{ decoder: Decoder.Bytes },
),
).toEqual([Buffer.from('"one"'), null]);
});

it("json.arrinsert", async () => {
client = await GlideClusterClient.createClient(
getClientConfigurationOption(
Expand Down

0 comments on commit 4db12f4

Please sign in to comment.