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
55 changes: 55 additions & 0 deletions marklogic/rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,61 @@ def query(
not 2xx, then the entire response is always returned.
"""
path = "v1/rows/graphql" if graphql else "v1/rows"
return self.__send_request(
path,
dsl,
plan,
sql,
sparql,
graphql,
format,
tx,
return_response,
**kwargs,
)

def update(
self,
dsl: str = None,
format: str = "json",
tx: Transaction = None,
return_response: bool = False,
**kwargs,
):
"""
Sends an update query to an endpoint at the MarkLogic rows service defined at
https://docs.marklogic.com/REST/client/row-management. Note that this feature
requires the use of MarkLogic version 11.2 or later.

For more information about Optic Update and using the Optic DSL,
see https://docs.marklogic.com/guide/app-dev/OpticAPI.
TODO - add links for Optic Update.

:param dsl: an Optic DSL query
:param tx: optional REST transaction in which to service this request.
:param return_response: boolean specifying if the entire original response
object should be returned (True) or if only the data should be returned (False)
upon a success (2xx) response. Note that if the status code of the response is
not 2xx, then the entire response is always returned.
"""
path = "v1/rows/update"
return self.__send_request(
path, dsl, None, None, None, None, format, tx, return_response, **kwargs
)

def __send_request(
self,
path: str = None,
dsl: str = None,
plan: dict = None,
sql: str = None,
sparql: str = None,
graphql: str = None,
format: str = "json",
tx: Transaction = None,
return_response: bool = False,
**kwargs,
):
headers = kwargs.pop("headers", {})
data = None
if graphql:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_rows_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from marklogic.documents import DefaultMetadata, Document


def test_update_dsl_remove(admin_client):
DEFAULT_PERMS = {"python-tester": ["read", "update"]}
DOC_URI = "/temp/doc1.json"
response = admin_client.documents.write(
[Document(DOC_URI, {"doc": 1}, permissions=DEFAULT_PERMS)]
)

update_query_remove = 'op.fromDocUris("' + DOC_URI + '").lockForUpdate().remove()'
response = admin_client.rows.update(update_query_remove, return_response=True)
assert 200 == response.status_code

docs = admin_client.documents.read([DOC_URI])
assert 0 == len(docs)


def test_update_dsl_wrong_path(admin_client):
DEFAULT_PERMS = {"python-tester": ["read", "update"]}
DOC_URI = "/temp/doc1.json"
response = admin_client.documents.write(
[Document(DOC_URI, {"doc": 1}, permissions=DEFAULT_PERMS)]
)

update_query_remove = 'op.fromDocUris("' + DOC_URI + '").lockForUpdate().remove()'
response = admin_client.rows.query(update_query_remove, return_response=True)
assert 400 == response.status_code
assert (
"Optic Update need to be run as update transaction"
in response.content.decode("utf-8")
)