|
14 | 14 | DocumentInsertError,
|
15 | 15 | DocumentParseError,
|
16 | 16 | DocumentRevisionError,
|
| 17 | + DocumentUpdateError, |
17 | 18 | IndexCreateError,
|
18 | 19 | IndexDeleteError,
|
19 | 20 | IndexGetError,
|
@@ -474,15 +475,15 @@ async def insert(
|
474 | 475 | retained in the document. Otherwise, they are removed completely.
|
475 | 476 | Applies only when **overwrite_mode** is set to "update"
|
476 | 477 | (update-insert).
|
477 |
| - merge_objects (bool | None): If set to True, sub-dictionaries are merged |
| 478 | + merge_objects (bool | None): If set to `True`, sub-dictionaries are merged |
478 | 479 | instead of the new one overwriting the old one. Applies only when
|
479 | 480 | **overwrite_mode** is set to "update" (update-insert).
|
480 | 481 | refill_index_caches (bool | None): Whether to add new entries to
|
481 | 482 | in-memory index caches if document insertions affect the edge index
|
482 | 483 | or cache-enabled persistent indexes.
|
483 | 484 | version_attribute (str | None): Support for simple external versioning to
|
484 |
| - document operations. Only applicable if **overwrite** is set to true |
485 |
| - or **overwriteMode** is set to "update" or "replace". |
| 485 | + document operations. Only applicable if **overwrite** is set to `True` |
| 486 | + or **overwrite_mode** is set to "update" or "replace". |
486 | 487 |
|
487 | 488 | Returns:
|
488 | 489 | bool | dict: Document metadata (e.g. document id, key, revision) or `True`
|
@@ -543,3 +544,97 @@ def response_handler(resp: Response) -> bool | Json:
|
543 | 544 | raise DocumentInsertError(resp, request, msg)
|
544 | 545 |
|
545 | 546 | return await self._executor.execute(request, response_handler)
|
| 547 | + |
| 548 | + async def update( |
| 549 | + self, |
| 550 | + document: T, |
| 551 | + ignore_revs: Optional[bool] = None, |
| 552 | + wait_for_sync: Optional[bool] = None, |
| 553 | + return_new: Optional[bool] = None, |
| 554 | + return_old: Optional[bool] = None, |
| 555 | + silent: Optional[bool] = None, |
| 556 | + keep_null: Optional[bool] = None, |
| 557 | + merge_objects: Optional[bool] = None, |
| 558 | + refill_index_caches: Optional[bool] = None, |
| 559 | + version_attribute: Optional[str] = None, |
| 560 | + ) -> Result[bool | Json]: |
| 561 | + """Insert a new document. |
| 562 | +
|
| 563 | + Args: |
| 564 | + document (dict): Partial or full document with the updated values. |
| 565 | + It must contain the "_key" or "_id" field. |
| 566 | + ignore_revs (bool | None): If set to `True`, the `_rev` attribute in the |
| 567 | + document is ignored. If this is set to `False`, then the `_rev` |
| 568 | + attribute given in the body document is taken as a precondition. |
| 569 | + The document is only updated if the current revision is the one |
| 570 | + specified. |
| 571 | + wait_for_sync (bool | None): Wait until document has been synced to disk. |
| 572 | + return_new (bool | None): Additionally return the complete new document |
| 573 | + under the attribute `new` in the result. |
| 574 | + return_old (bool | None): Additionally return the complete old document |
| 575 | + under the attribute `old` in the result. |
| 576 | + silent (bool | None): If set to `True`, no document metadata is returned. |
| 577 | + This can be used to save resources. |
| 578 | + keep_null (bool | None): If the intention is to delete existing attributes |
| 579 | + with the patch command, set this parameter to `False`. |
| 580 | + merge_objects (bool | None): Controls whether objects (not arrays) are |
| 581 | + merged if present in both the existing and the patch document. |
| 582 | + If set to `False`, the value in the patch document overwrites the |
| 583 | + existing document’s value. If set to `True`, objects are merged. |
| 584 | + refill_index_caches (bool | None): Whether to add new entries to |
| 585 | + in-memory index caches if document updates affect the edge index |
| 586 | + or cache-enabled persistent indexes. |
| 587 | + version_attribute (str | None): Support for simple external versioning to |
| 588 | + document operations. |
| 589 | +
|
| 590 | + Returns: |
| 591 | + bool | dict: Document metadata (e.g. document id, key, revision) or `True` |
| 592 | + if **silent** is set to `True`. |
| 593 | +
|
| 594 | + Raises: |
| 595 | + DocumentRevisionError: If precondition was violated. |
| 596 | + DocumentUpdateError: If update fails. |
| 597 | +
|
| 598 | + References: |
| 599 | + - `update-a-document <https://docs.arangodb.com/stable/develop/http-api/documents/#update-a-document>`__ |
| 600 | + """ # noqa: E501 |
| 601 | + params: Params = {} |
| 602 | + if ignore_revs is not None: |
| 603 | + params["ignoreRevs"] = ignore_revs |
| 604 | + if wait_for_sync is not None: |
| 605 | + params["waitForSync"] = wait_for_sync |
| 606 | + if return_new is not None: |
| 607 | + params["returnNew"] = return_new |
| 608 | + if return_old is not None: |
| 609 | + params["returnOld"] = return_old |
| 610 | + if silent is not None: |
| 611 | + params["silent"] = silent |
| 612 | + if keep_null is not None: |
| 613 | + params["keepNull"] = keep_null |
| 614 | + if merge_objects is not None: |
| 615 | + params["mergeObjects"] = merge_objects |
| 616 | + if refill_index_caches is not None: |
| 617 | + params["refillIndexCaches"] = refill_index_caches |
| 618 | + if version_attribute is not None: |
| 619 | + params["versionAttribute"] = version_attribute |
| 620 | + |
| 621 | + request = Request( |
| 622 | + method=Method.PATCH, |
| 623 | + endpoint=f"/_api/document/{self._extract_id(cast(Json, document))}", |
| 624 | + params=params, |
| 625 | + data=self._doc_serializer.dumps(document), |
| 626 | + ) |
| 627 | + |
| 628 | + def response_handler(resp: Response) -> bool | Json: |
| 629 | + if resp.is_success: |
| 630 | + if silent is True: |
| 631 | + return True |
| 632 | + return self._executor.deserialize(resp.raw_body) |
| 633 | + msg: Optional[str] = None |
| 634 | + if resp.status_code == HTTP_PRECONDITION_FAILED: |
| 635 | + raise DocumentRevisionError(resp, request) |
| 636 | + elif resp.status_code == HTTP_NOT_FOUND: |
| 637 | + msg = "Document, collection or transaction not found." |
| 638 | + raise DocumentUpdateError(resp, request, msg) |
| 639 | + |
| 640 | + return await self._executor.execute(request, response_handler) |
0 commit comments