5
5
]
6
6
7
7
8
- from typing import List , Optional , Sequence , TypeVar , cast
8
+ from typing import Any , List , Optional , Sequence , TypeVar , cast
9
+ from warnings import warn
9
10
10
11
from arangoasync .collection import StandardCollection
11
12
from arangoasync .connection import Connection
27
28
ServerStatusError ,
28
29
TransactionAbortError ,
29
30
TransactionCommitError ,
31
+ TransactionExecuteError ,
30
32
TransactionInitError ,
31
33
TransactionListError ,
32
34
TransactionStatusError ,
@@ -1099,6 +1101,85 @@ def response_handler(resp: Response) -> Jsons:
1099
1101
1100
1102
return await self ._executor .execute (request , response_handler )
1101
1103
1104
+ async def execute_transaction (
1105
+ self ,
1106
+ command : str ,
1107
+ params : Optional [Json ] = None ,
1108
+ read : Optional [str | Sequence [str ]] = None ,
1109
+ write : Optional [str | Sequence [str ]] = None ,
1110
+ exclusive : Optional [str | Sequence [str ]] = None ,
1111
+ allow_implicit : Optional [bool ] = None ,
1112
+ wait_for_sync : Optional [bool ] = None ,
1113
+ lock_timeout : Optional [int ] = None ,
1114
+ max_transaction_size : Optional [int ] = None ,
1115
+ ) -> Result [Any ]:
1116
+ """Execute a JavaScript Transaction.
1117
+
1118
+ Warning:
1119
+ JavaScript Transactions are deprecated from ArangoDB v3.12.0 onward and
1120
+ will be removed in a future version.
1121
+
1122
+ Args:
1123
+ command (str): The actual transaction operations to be executed, in the
1124
+ form of stringified JavaScript code.
1125
+ params (dict): Optional parameters passed into the JavaScript command.
1126
+ read (str | list | None): Name(s) of collections read during transaction.
1127
+ write (str | list | None): Name(s) of collections written to during
1128
+ transaction with shared access.
1129
+ exclusive (str | list | None): Name(s) of collections written to during
1130
+ transaction with exclusive access.
1131
+ allow_implicit (bool | None): Allow reading from undeclared collections.
1132
+ wait_for_sync (bool | None): If `True`, will force the transaction to write
1133
+ all data to disk before returning.
1134
+ lock_timeout (int | None): Timeout for waiting on collection locks. Setting
1135
+ it to 0 will prevent ArangoDB from timing out while waiting for a lock.
1136
+ max_transaction_size (int | None): Transaction size limit in bytes.
1137
+
1138
+ Returns:
1139
+ Any: Result of the transaction.
1140
+
1141
+ Raises:
1142
+ TransactionExecuteError: If the operation fails on the server side.
1143
+
1144
+ References:
1145
+ - `execute-a-javascript-transaction <https://docs.arangodb.com/stable/develop/http-api/transactions/javascript-transactions/#execute-a-javascript-transaction>`__
1146
+ """ # noqa: 501
1147
+ m = "JavaScript Transactions are deprecated from ArangoDB v3.12.0 onward and will be removed in a future version." # noqa: E501
1148
+ warn (m , DeprecationWarning , stacklevel = 2 )
1149
+
1150
+ collections = dict ()
1151
+ if read is not None :
1152
+ collections ["read" ] = read
1153
+ if write is not None :
1154
+ collections ["write" ] = write
1155
+ if exclusive is not None :
1156
+ collections ["exclusive" ] = exclusive
1157
+
1158
+ data : Json = dict (collections = collections , action = command )
1159
+ if params is not None :
1160
+ data ["params" ] = params
1161
+ if wait_for_sync is not None :
1162
+ data ["waitForSync" ] = wait_for_sync
1163
+ if allow_implicit is not None :
1164
+ data ["allowImplicit" ] = allow_implicit
1165
+ if lock_timeout is not None :
1166
+ data ["lockTimeout" ] = lock_timeout
1167
+ if max_transaction_size is not None :
1168
+ data ["maxTransactionSize" ] = max_transaction_size
1169
+
1170
+ request = Request (
1171
+ method = Method .POST ,
1172
+ endpoint = "/_api/transaction" ,
1173
+ data = self .serializer .dumps (data ),
1174
+ )
1175
+
1176
+ def response_handler (resp : Response ) -> Any :
1177
+ if not resp .is_success :
1178
+ raise TransactionExecuteError (resp , request )
1179
+ return self .deserializer .loads (resp .raw_body )["result" ]
1180
+
1181
+ return await self ._executor .execute (request , response_handler )
1182
+
1102
1183
1103
1184
class StandardDatabase (Database ):
1104
1185
"""Standard database API wrapper.
@@ -1139,7 +1220,7 @@ async def begin_transaction(
1139
1220
all data to disk before returning
1140
1221
allow_implicit (bool | None): Allow reading from undeclared collections.
1141
1222
lock_timeout (int | None): Timeout for waiting on collection locks. Setting
1142
- it to 0 will make ArangoDB not time out waiting for a lock.
1223
+ it to 0 will prevent ArangoDB from timing out while waiting for a lock.
1143
1224
max_transaction_size (int | None): Transaction size limit in bytes.
1144
1225
allow_dirty_read (bool | None): If `True`, allows the Coordinator to ask any
1145
1226
shard replica for the data, not only the shard leader. This may result
@@ -1155,7 +1236,10 @@ async def begin_transaction(
1155
1236
1156
1237
Raises:
1157
1238
TransactionInitError: If the operation fails on the server side.
1158
- """
1239
+
1240
+ References:
1241
+ - `begin-a-stream-transaction <https://docs.arangodb.com/stable/develop/http-api/transactions/stream-transactions/#begin-a-stream-transaction>`__
1242
+ """ # noqa: E501
1159
1243
collections = dict ()
1160
1244
if read is not None :
1161
1245
collections ["read" ] = read
@@ -1244,7 +1328,10 @@ async def transaction_status(self) -> str:
1244
1328
1245
1329
Raises:
1246
1330
TransactionStatusError: If the transaction is not found.
1247
- """
1331
+
1332
+ References:
1333
+ - `get-the-status-of-a-stream-transaction <https://docs.arangodb.com/stable/develop/http-api/transactions/stream-transactions/#get-the-status-of-a-stream-transaction>`__
1334
+ """ # noqa: E501
1248
1335
request = Request (
1249
1336
method = Method .GET ,
1250
1337
endpoint = f"/_api/transaction/{ self .transaction_id } " ,
@@ -1263,7 +1350,10 @@ async def commit_transaction(self) -> None:
1263
1350
1264
1351
Raises:
1265
1352
TransactionCommitError: If the operation fails on the server side.
1266
- """
1353
+
1354
+ References:
1355
+ - `commit-a-stream-transaction <https://docs.arangodb.com/stable/develop/http-api/transactions/stream-transactions/#commit-a-stream-transaction>`__
1356
+ """ # noqa: E501
1267
1357
request = Request (
1268
1358
method = Method .PUT ,
1269
1359
endpoint = f"/_api/transaction/{ self .transaction_id } " ,
@@ -1276,7 +1366,14 @@ def response_handler(resp: Response) -> None:
1276
1366
await self ._executor .execute (request , response_handler )
1277
1367
1278
1368
async def abort_transaction (self ) -> None :
1279
- """Abort the transaction."""
1369
+ """Abort the transaction.
1370
+
1371
+ Raises:
1372
+ TransactionAbortError: If the operation fails on the server side.
1373
+
1374
+ References:
1375
+ - `abort-a-stream-transaction <https://docs.arangodb.com/stable/develop/http-api/transactions/stream-transactions/#abort-a-stream-transaction>`__
1376
+ """ # noqa: E501
1280
1377
request = Request (
1281
1378
method = Method .DELETE ,
1282
1379
endpoint = f"/_api/transaction/{ self .transaction_id } " ,
0 commit comments