|
| 1 | +Helper Types |
| 2 | +------------ |
| 3 | + |
| 4 | +The driver comes with a set of helper types and wrappers to make it easier to work with the ArangoDB API. These are |
| 5 | +designed to behave like dictionaries, but with some additional features and methods. See the :class:`arangoasync.typings.JsonWrapper` class for more details. |
| 6 | + |
| 7 | +**Example:** |
| 8 | + |
| 9 | +.. code-block:: python |
| 10 | +
|
| 11 | + from arangoasync import ArangoClient |
| 12 | + from arangoasync.auth import Auth |
| 13 | + from arangoasync.typings import QueryProperties |
| 14 | +
|
| 15 | + # Initialize the client for ArangoDB. |
| 16 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 17 | + auth = Auth(username="root", password="passwd") |
| 18 | +
|
| 19 | + # Connect to "test" database as root user. |
| 20 | + db = await client.db("test", auth=auth) |
| 21 | +
|
| 22 | + properties = QueryProperties( |
| 23 | + allow_dirty_reads=True, |
| 24 | + allow_retry=False, |
| 25 | + fail_on_warning=True, |
| 26 | + fill_block_cache=False, |
| 27 | + full_count=True, |
| 28 | + intermediate_commit_count=1000, |
| 29 | + intermediate_commit_size=1048576, |
| 30 | + max_dnf_condition_members=10, |
| 31 | + max_nodes_per_callstack=100, |
| 32 | + max_number_of_plans=5, |
| 33 | + max_runtime=60.0, |
| 34 | + max_transaction_size=10485760, |
| 35 | + max_warning_count=10, |
| 36 | + optimizer={"rules": ["-all", "+use-indexes"]}, |
| 37 | + profile=1, |
| 38 | + satellite_sync_wait=10.0, |
| 39 | + skip_inaccessible_collections=True, |
| 40 | + spill_over_threshold_memory_usage=10485760, |
| 41 | + spill_over_threshold_num_rows=100000, |
| 42 | + stream=True, |
| 43 | + use_plan_cache=True, |
| 44 | + ) |
| 45 | +
|
| 46 | + # The types are fully serializable. |
| 47 | + print(properties) |
| 48 | +
|
| 49 | + await db.aql.execute( |
| 50 | + "FOR doc IN students RETURN doc", |
| 51 | + batch_size=1, |
| 52 | + options=properties, |
| 53 | + ) |
| 54 | +
|
| 55 | +You can easily customize the data representation using formatters. By default, keys are in the format used by the ArangoDB |
| 56 | +API, but you can change them to snake_case if you prefer. See :func:`arangoasync.typings.JsonWrapper.format` for more details. |
| 57 | + |
| 58 | +**Example:** |
| 59 | + |
| 60 | +.. code-block:: python |
| 61 | +
|
| 62 | + from arangoasync.typings import Json, UserInfo |
| 63 | +
|
| 64 | + data = { |
| 65 | + "user": "john", |
| 66 | + "password": "secret", |
| 67 | + "active": True, |
| 68 | + "extra": {"role": "admin"}, |
| 69 | + } |
| 70 | + user_info = UserInfo(**data) |
| 71 | +
|
| 72 | + def uppercase_formatter(data: Json) -> Json: |
| 73 | + result: Json = {} |
| 74 | + for key, value in data.items(): |
| 75 | + result[key.upper()] = value |
| 76 | + return result |
| 77 | +
|
| 78 | + print(user_info.format(uppercase_formatter)) |
| 79 | +
|
| 80 | +Helpers |
| 81 | +======= |
| 82 | + |
| 83 | +Below are all the available helpers. |
| 84 | + |
| 85 | +.. automodule:: arangoasync.typings |
| 86 | + :members: |
0 commit comments