Debian Codegraph @

docs/tutorials/sqlalchemy/4-final-touches-and-recap.rst

raw
1 Final touches and recap
2 -----------------------
3
4 There is one more improvement that we can make to our application. Currently, we utilize both the
5 :class:`SQLAlchemyInitPlugin <litestar.contrib.sqlalchemy.plugins.SQLAlchemyInitPlugin>` and the
6 :class:`SQLAlchemySerializationPlugin <litestar.contrib.sqlalchemy.plugins.SQLAlchemySerializationPlugin>`, but there
7 is a shortcut for this configuration: the
8 :class:`SQLAlchemyPlugin <litestar.contrib.sqlalchemy.plugins.SQLAlchemyPlugin>` is a combination of the two, so we
9 can simplify our configuration by using it instead.
10
11 Here is our final application:
12
13 .. literalinclude:: /examples/contrib/sqlalchemy/plugins/tutorial/full_app_with_plugin.py
14 :language: python
15 :linenos:
16 :emphasize-lines: 10, 82
17
18 Recap
19 =====
20
21 In this tutorial, we have learned how to use the SQLAlchemy plugin to create a simple application that uses a database
22 to store and retrieve data.
23
24 In the final application ``TodoItem`` is defined, representing a TODO item. It extends from the
25 :class:`DeclarativeBase <sqlalchemy.orm.DeclarativeBase>` class provided by `SQLAlchemy <http://www.sqlalchemy.org/>`_:
26
27 .. literalinclude:: /examples/contrib/sqlalchemy/plugins/tutorial/full_app_with_plugin.py
28 :language: python
29 :linenos:
30 :lines: 1-6, 12-21
31
32 Next, we define a dependency that centralizes our database transaction management and error handling. This dependency
33 depends on the ``db_session`` dependency, which is provided by the SQLAlchemy plugin, and is made available to our
34 handlers via the ``transaction`` argument:
35
36 .. literalinclude:: /examples/contrib/sqlalchemy/plugins/tutorial/full_app_with_plugin.py
37 :language: python
38 :linenos:
39 :lines: 1-11, 22-32
40
41 We also define a couple of utility functions, that help us to retrieve our TODO items from the database:
42
43 .. literalinclude:: /examples/contrib/sqlalchemy/plugins/tutorial/full_app_with_plugin.py
44 :language: python
45 :linenos:
46 :lines: 1-11, 33-50
47
48 We define our route handlers, which are the interface through which TODO items can be created, retrieved and updated:
49
50 .. literalinclude:: /examples/contrib/sqlalchemy/plugins/tutorial/full_app_with_plugin.py
51 :language: python
52 :linenos:
53 :lines: 1-11, 51-69
54
55 Finally, we define our application, using the
56 :class:`SQLAlchemyPlugin <litestar.contrib.sqlalchemy.plugins.SQLAlchemyPlugin>` to configure SQLAlchemy and manage the
57 engine and session lifecycle, and register our ``transaction`` dependency.
58
59 .. literalinclude:: /examples/contrib/sqlalchemy/plugins/tutorial/full_app_with_plugin.py
60 :language: python
61 :linenos:
62 :lines: 1-11, 78-83
63
64 .. seealso::
65
66 * :doc:`SQLAlchemy Plugins Usage Guide </usage/databases/sqlalchemy/plugins/index>`