Skip to content
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "rushdb"
version = "3.2.0"
version = "3.2.1"
description = "RushDB SDK for Python: the client for RushDB, the database for AI (RushDB Cloud or self-hosted)"
authors = [
{name = "RushDB Team", email = "hi@rushdb.com"}
Expand Down
4 changes: 2 additions & 2 deletions src/rushdb/api/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def update(
data (Dict[str, Any]): The data to merge with the existing record data.
transaction (Optional[Transaction], optional): Transaction context for the operation.
If provided, the operation will be part of the transaction. Defaults to None.
label (Optional[str]): Optional label to re-assign to the record.

Returns:
Dict[str, str]: Response from the server containing operation status.
Expand All @@ -154,8 +155,7 @@ def update(
"""
record_id = pick_record_id(target)
headers = Transaction._build_transaction_header(transaction)

payload = data.copy()
payload: Dict[str, Any] = {"data": data}
if label is not None:
payload["label"] = label
return self.client._make_request(
Expand Down
12 changes: 5 additions & 7 deletions src/rushdb/direct/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,18 @@ def _sdk_records(records: Optional[List[Body]]) -> List[Body]:
return [to_sdk_record(record) or {} for record in records or []]


_RECORD_KEYS = {"label", "options", "vectors", "data", "properties"}


def _record_properties(body: Body) -> Body:
"""A write's flat data: ``data``, property drafts as an object, or the flat body."""
"""A record write's data, as RushDB Cloud reads it: ``data`` (a flat object)
or ``properties`` (a list of property drafts). Anything else is refused."""
data = body.get("data")
if isinstance(data, dict):
return data
properties = body.get("properties")
if isinstance(properties, list):
return {item.get("name"): item.get("value") for item in properties}
if isinstance(properties, dict):
return properties
return {key: value for key, value in body.items() if key not in _RECORD_KEYS}
raise _http_error(
400, "A record write takes `data` (an object) or `properties` (a list)."
)


def _is_empty(value: Any) -> bool:
Expand Down
41 changes: 41 additions & 0 deletions tests/test_direct_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,47 @@ def test_updates_replaces_deletes_and_links_are_staged_by_id(engine_calls):
]


def put_records(calls):
return [
op["mutation"]
for op in operations(calls)
if op["kind"] == "mutate" and op["mutation"]["kind"] == "putRecord"
]


def test_update_merges_the_data_object_with_the_label(engine_calls):
db = direct_db()
data = {"title": "Emma", "label": "classic", "options": "a,b"}
db.records.update(RECORD["id"], data, label="Book")
(put,) = put_records(engine_calls)
assert put["mode"] == "merge" and put["id"] == RECORD["id"]
assert put["properties"] == data


def test_a_flat_record_write_is_refused_as_cloud_refuses_it(engine_calls):
db = direct_db()
for method in ("PATCH", "PUT"):
with pytest.raises(RushDBError) as raised:
db._make_request(
method, f"/records/{RECORD['id']}", {"label": "Book", "title": "x"}
)
assert raised.value.status == 400
with pytest.raises(RushDBError):
db._make_request("POST", "/records", {"label": "Book", "title": "x"})
assert not put_records(engine_calls)


def test_property_drafts_are_written_like_data(engine_calls):
db = direct_db()
db._make_request(
"PATCH",
f"/records/{RECORD['id']}",
{"label": "Book", "properties": [{"name": "title", "value": "Emma"}]},
)
(put,) = put_records(engine_calls)
assert put["properties"] == {"title": "Emma"}


def test_imports_are_staged_into_a_transaction(engine_calls):
db = direct_db()
db.records.import_json([{"title": "Dune"}], label="Book", transaction="tx-1")
Expand Down
4 changes: 4 additions & 0 deletions tests/test_direct_mode_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def test_records_round_trip(db):
assert db.records.find_by_id(created.id)["title"] == "Ubik"
db.records.update(created.id, {"pages": 210})
assert db.records.find_by_id(created.id)["pages"] == 210
db.records.update(created.id, {"pages": 211, "label": "worn"}, label=BOOK)
updated = db.records.find_by_id(created.id)
assert updated["pages"] == 211 and updated["label"] == "worn"
assert updated["title"] == "Ubik" and updated.label == BOOK
db.records.set(created.id, {"title": "Ubik!"}, label=BOOK)
replaced = db.records.find_by_id(created.id)
assert replaced["title"] == "Ubik!" and replaced.get("pages") is None
Expand Down
51 changes: 51 additions & 0 deletions tests/test_records_write_body.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""The request bodies record writes send to RushDB Cloud: the same
``{label, data, options}`` shape the JavaScript SDK sends."""

from unittest import mock

from rushdb import RushDB

RECORD_ID = "0190a7e2-0000-7000-8000-000000000001"


def sent(call):
db = RushDB("API-KEY", base_url="http://cloud.test/api/v1")
with mock.patch.object(db, "_make_request", return_value={"data": {}}) as request:
call(db)
(method, path, body, headers), _ = request.call_args
return method, path, body


def test_update_sends_the_fields_under_data_with_the_label():
method, path, body = sent(
lambda db: db.records.update(
RECORD_ID, {"expires_at": "2026-12-01T00:00:00Z"}, label="Memory"
)
)
assert (method, path) == ("PATCH", f"/records/{RECORD_ID}")
assert body == {"label": "Memory", "data": {"expires_at": "2026-12-01T00:00:00Z"}}


def test_update_without_a_label_still_sends_data():
_, _, body = sent(lambda db: db.records.update(RECORD_ID, {"status": "done"}))
assert body == {"data": {"status": "done"}}


def test_update_keeps_fields_named_like_body_keys_inside_data():
data = {"label": "urgent", "options": "a,b", "data": "raw"}
_, _, body = sent(lambda db: db.records.update(RECORD_ID, data, label="Task"))
assert body == {"label": "Task", "data": data}


def test_update_does_not_mutate_the_callers_dict():
data = {"status": "done"}
sent(lambda db: db.records.update(RECORD_ID, data, label="Task"))
assert data == {"status": "done"}


def test_set_sends_the_same_shape_over_put():
method, path, body = sent(
lambda db: db.records.set(RECORD_ID, {"status": "done"}, label="Task")
)
assert (method, path) == ("PUT", f"/records/{RECORD_ID}")
assert body == {"data": {"status": "done"}, "label": "Task"}
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading