|
13 | 13 | DocumentGetError,
|
14 | 14 | DocumentInsertError,
|
15 | 15 | DocumentParseError,
|
| 16 | + DocumentReplaceError, |
16 | 17 | DocumentRevisionError,
|
17 | 18 | DocumentUpdateError,
|
18 | 19 | IndexCreateError,
|
@@ -638,3 +639,85 @@ def response_handler(resp: Response) -> bool | Json:
|
638 | 639 | raise DocumentUpdateError(resp, request, msg)
|
639 | 640 |
|
640 | 641 | return await self._executor.execute(request, response_handler)
|
| 642 | + |
| 643 | + async def replace( |
| 644 | + self, |
| 645 | + document: T, |
| 646 | + ignore_revs: Optional[bool] = None, |
| 647 | + wait_for_sync: Optional[bool] = None, |
| 648 | + return_new: Optional[bool] = None, |
| 649 | + return_old: Optional[bool] = None, |
| 650 | + silent: Optional[bool] = None, |
| 651 | + refill_index_caches: Optional[bool] = None, |
| 652 | + version_attribute: Optional[str] = None, |
| 653 | + ) -> Result[bool | Json]: |
| 654 | + """Replace a document. |
| 655 | +
|
| 656 | + Args: |
| 657 | + document (dict): New document. It must contain the "_key" or "_id" field. |
| 658 | + Edge document must also have "_from" and "_to" fields. |
| 659 | + ignore_revs (bool | None): If set to `True`, the `_rev` attribute in the |
| 660 | + document is ignored. If this is set to `False`, then the `_rev` |
| 661 | + attribute given in the body document is taken as a precondition. |
| 662 | + The document is only replaced if the current revision is the one |
| 663 | + specified. |
| 664 | + wait_for_sync (bool | None): Wait until document has been synced to disk. |
| 665 | + return_new (bool | None): Additionally return the complete new document |
| 666 | + under the attribute `new` in the result. |
| 667 | + return_old (bool | None): Additionally return the complete old document |
| 668 | + under the attribute `old` in the result. |
| 669 | + silent (bool | None): If set to `True`, no document metadata is returned. |
| 670 | + This can be used to save resources. |
| 671 | + refill_index_caches (bool | None): Whether to add new entries to |
| 672 | + in-memory index caches if document updates affect the edge index |
| 673 | + or cache-enabled persistent indexes. |
| 674 | + version_attribute (str | None): Support for simple external versioning to |
| 675 | + document operations. |
| 676 | +
|
| 677 | + Returns: |
| 678 | + bool | dict: Document metadata (e.g. document id, key, revision) or `True` |
| 679 | + if **silent** is set to `True`. |
| 680 | +
|
| 681 | + Raises: |
| 682 | + DocumentRevisionError: If precondition was violated. |
| 683 | + DocumentReplaceError: If replace fails. |
| 684 | +
|
| 685 | + References: |
| 686 | + - `replace-a-document <https://docs.arangodb.com/stable/develop/http-api/documents/#replace-a-document>`__ |
| 687 | + """ # noqa: E501 |
| 688 | + params: Params = {} |
| 689 | + if ignore_revs is not None: |
| 690 | + params["ignoreRevs"] = ignore_revs |
| 691 | + if wait_for_sync is not None: |
| 692 | + params["waitForSync"] = wait_for_sync |
| 693 | + if return_new is not None: |
| 694 | + params["returnNew"] = return_new |
| 695 | + if return_old is not None: |
| 696 | + params["returnOld"] = return_old |
| 697 | + if silent is not None: |
| 698 | + params["silent"] = silent |
| 699 | + if refill_index_caches is not None: |
| 700 | + params["refillIndexCaches"] = refill_index_caches |
| 701 | + if version_attribute is not None: |
| 702 | + params["versionAttribute"] = version_attribute |
| 703 | + |
| 704 | + request = Request( |
| 705 | + method=Method.PUT, |
| 706 | + endpoint=f"/_api/document/{self._extract_id(cast(Json, document))}", |
| 707 | + params=params, |
| 708 | + data=self._doc_serializer.dumps(document), |
| 709 | + ) |
| 710 | + |
| 711 | + def response_handler(resp: Response) -> bool | Json: |
| 712 | + if resp.is_success: |
| 713 | + if silent is True: |
| 714 | + return True |
| 715 | + return self._executor.deserialize(resp.raw_body) |
| 716 | + msg: Optional[str] = None |
| 717 | + if resp.status_code == HTTP_PRECONDITION_FAILED: |
| 718 | + raise DocumentRevisionError(resp, request) |
| 719 | + elif resp.status_code == HTTP_NOT_FOUND: |
| 720 | + msg = "Document, collection or transaction not found." |
| 721 | + raise DocumentReplaceError(resp, request, msg) |
| 722 | + |
| 723 | + return await self._executor.execute(request, response_handler) |
0 commit comments