|
| 1 | +Coming from python-arango |
| 2 | +------------------------- |
| 3 | + |
| 4 | +Generally, migrating from `python-arango`_ should be a smooth transition. For the most part, the API is similar, |
| 5 | +but there are a few things to note._ |
| 6 | + |
| 7 | +Helpers |
| 8 | +======= |
| 9 | + |
| 10 | +The current driver comes with :ref:`Helpers`, because we want to: |
| 11 | + |
| 12 | +1. Facilitate better type hinting and auto-completion in IDEs. |
| 13 | +2. Ensure an easier 1-to-1 mapping of the ArangoDB API. |
| 14 | + |
| 15 | +For example, coming from the synchronous driver, creating a new user looks like this: |
| 16 | + |
| 17 | +.. code-block:: python |
| 18 | +
|
| 19 | + sys_db.create_user( |
| 20 | + username="johndoe@gmail.com", |
| 21 | + password="first_password", |
| 22 | + active=True, |
| 23 | + extra={"team": "backend", "title": "engineer"} |
| 24 | + ) |
| 25 | +
|
| 26 | +In the asynchronous driver, it looks like this: |
| 27 | + |
| 28 | +.. code-block:: python |
| 29 | +
|
| 30 | + from arangoasync.typings import UserInfo |
| 31 | +
|
| 32 | + user_info = UserInfo( |
| 33 | + username="johndoe@gmail.com", |
| 34 | + password="first_password", |
| 35 | + active=True, |
| 36 | + extra={"team": "backend", "title": "engineer"} |
| 37 | + ) |
| 38 | + await sys_db.create_user(user_info) |
| 39 | +
|
| 40 | +CamelCase vs. snake_case |
| 41 | +======================== |
| 42 | + |
| 43 | +Upon returning results, for the most part, the synchronous driver mostly tries to stick to snake case. Unfortunately, |
| 44 | +this is not always consistent. |
| 45 | + |
| 46 | +.. code-block:: python |
| 47 | +
|
| 48 | + status = db.status() |
| 49 | + assert "host" in status |
| 50 | + assert "operation_mode" in status |
| 51 | +
|
| 52 | +The asynchronous driver, however, tries to stick to a simple rule: |
| 53 | + |
| 54 | +* If the API returns a camel case key, it will be returned as is. |
| 55 | +* Parameters passed from client to server use the snake case equivalent of the camel case keys required by the API |
| 56 | + (e.g. `userName` becomes `user_name`). This is done to ensure PEP8 compatibility. |
| 57 | + |
| 58 | +.. code-block:: python |
| 59 | +
|
| 60 | + from arangoasync.typings import ServerStatusInformation |
| 61 | +
|
| 62 | + status: ServerStatusInformation = await db.status() |
| 63 | + assert "host" in status |
| 64 | + assert "operationMode" in status |
| 65 | + print(status.host) |
| 66 | + print(status.operation_mode) |
| 67 | +
|
| 68 | +You can use the :func:`arangoasync.typings.JsonWrapper.format` method to gain more control over the formatting of |
| 69 | +keys. |
| 70 | + |
| 71 | +Serialization |
| 72 | +============= |
| 73 | + |
| 74 | +Check out the :ref:`Serialization` section to learn more about how to implement your own serializer/deserializer. The |
| 75 | +current driver makes use of generic types and allows for a higher degree of customization. |
| 76 | + |
| 77 | +Mixing sync and async |
| 78 | +===================== |
| 79 | + |
| 80 | +Sometimes you may need to mix the two. This is not recommended, but it takes time to migrate everything. If you need to |
| 81 | +do this, you can use the :func:`asyncio.to_thread` function to run a synchronous function in separate thread, without |
| 82 | +compromising the async event loop. |
| 83 | + |
| 84 | +.. code-block:: python |
| 85 | +
|
| 86 | + # Use a python-arango synchronous client |
| 87 | + sys_db = await asyncio.to_thread( |
| 88 | + client.db, |
| 89 | + "_system", |
| 90 | + username="root", |
| 91 | + password="passwd" |
| 92 | + ) |
| 93 | +
|
| 94 | +.. _python-arango: https://docs.python-arango.com |
0 commit comments