1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
Using the serialization plugin
------------------------------
Our next improvement is to leverage the
:class:`SQLAlchemySerializationPlugin <litestar.contrib.sqlalchemy.plugins.SQLAlchemySerializationPlugin>`
so that we can receive and return our SQLAlchemy models directly to and from our handlers.
Here's the code:
.. literalinclude:: /examples/contrib/sqlalchemy/plugins/tutorial/full_app_with_serialization_plugin.py
:language: python
:linenos:
:emphasize-lines: 12, 75-77, 80-83, 86-91, 98
We've simply imported the plugin and added it to our app's plugins list, and now we can receive and return our
SQLAlchemy data models directly to and from our handler.
We've also been able to remove the ``TodoType`` and ``TodoCollectionType`` aliases, and the ``serialize_todo()``
function, making the implementation even more concise.
Compare handlers before and after Serialization Plugin
======================================================
Once more, let's compare the sets of application handlers before and after our refactoring:
.. tab-set::
.. tab-item:: After
.. literalinclude:: /examples/contrib/sqlalchemy/plugins/tutorial/full_app_with_serialization_plugin.py
:language: python
:linenos:
:lines: 1-13, 73-99
.. tab-item:: Before
.. literalinclude:: /examples/contrib/sqlalchemy/plugins/tutorial/full_app_no_plugins.py
:language: python
:linenos:
:lines: 1-12, 67-100
Very nice! But, we can do better.
Next steps
==========
In our application, we've had to build a bit of scaffolding to integrate SQLAlchemy with our application. We've had to
define the ``db_connection()`` lifespan context manager, and the ``provide_transaction()`` dependency provider.
Next we'll look at how the :class:`SQLAlchemyInitPlugin <litestar.contrib.sqlalchemy.plugins.SQLAlchemyInitPlugin>` can
help us.
|