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

Support JSON.MSET Command #2766

Merged
merged 18 commits into from
Jun 25, 2023
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
1 change: 1 addition & 0 deletions redis/commands/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(
"JSON.GET": self._decode,
"JSON.MGET": bulk_of_jsons(self._decode),
"JSON.SET": lambda r: r and nativestr(r) == "OK",
"JSON.MSET": lambda r: r and nativestr(r) == "OK",
"JSON.MERGE": lambda r: r and nativestr(r) == "OK",
"JSON.NUMINCRBY": self._decode,
"JSON.NUMMULTBY": self._decode,
Expand Down
19 changes: 18 additions & 1 deletion redis/commands/json/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from json import JSONDecodeError, loads
from typing import Dict, List, Optional, Union
from typing import Dict, List, Optional, Tuple, Union

from redis.exceptions import DataError
from redis.utils import deprecated_function
Expand Down Expand Up @@ -253,6 +253,23 @@ def set(
pieces.append("XX")
return self.execute_command("JSON.SET", *pieces)

def mset(self, triplets: List[Tuple[str, str, JsonType]]) -> Optional[str]:
"""
Set the JSON value at key ``name`` under the ``path`` to ``obj``
for one or more keys.

``triplets`` is a list of one or more triplets of key, path, value.

For the purpose of using this within a pipeline, this command is also
aliased to JSON.MSET.

For more information see `JSON.MSET <https://redis.io/commands/json.mset>`_.
"""
pieces = []
for triplet in triplets:
pieces.extend([triplet[0], str(triplet[1]), self._encode(triplet[2])])
return self.execute_command("JSON.MSET", *pieces)

def merge(
self,
name: str,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_asyncio/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ async def test_mgetshouldsucceed(modclient: redis.Redis):
assert await modclient.json().mget([1, 2], Path.root_path()) == [1, 2]


@pytest.mark.redismod
@skip_ifmodversion_lt("2.6.0", "ReJSON") # todo: update after the release
async def test_mset(modclient: redis.Redis):
await modclient.json().mset(
[("1", Path.root_path(), 1), ("2", Path.root_path(), 2)]
)

assert await modclient.json().mget(["1"], Path.root_path()) == [1]
assert await modclient.json().mget(["1", "2"], Path.root_path()) == [1, 2]


@pytest.mark.redismod
@skip_ifmodversion_lt("99.99.99", "ReJSON") # todo: update after the release
async def test_clear(modclient: redis.Redis):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ def test_mgetshouldsucceed(client):
assert client.json().mget([1, 2], Path.root_path()) == [1, 2]


@pytest.mark.redismod
@skip_ifmodversion_lt("2.6.0", "ReJSON") # todo: update after the release
def test_mset(client):
client.json().mset([("1", Path.root_path(), 1), ("2", Path.root_path(), 2)])

assert client.json().mget(["1"], Path.root_path()) == [1]
assert client.json().mget(["1", "2"], Path.root_path()) == [1, 2]


@pytest.mark.redismod
@skip_ifmodversion_lt("99.99.99", "ReJSON") # todo: update after the release
def test_clear(client):
Expand Down