| 1 |
Working with Controllers and Repositories |
| 2 |
----------------------------------------- |
| 3 |
We've been working our way up the stack, starting with the database models, and now we |
| 4 |
are ready to use the repository in an actual route. Let's see how we can use this in a |
| 5 |
controller. |
| 6 |
|
| 7 |
.. tip:: The full code for this tutorial can be found below in the :ref:`Full Code <03-repo-full-code>` section. |
| 8 |
|
| 9 |
First, we create a simple function that returns an instance of ``AuthorRepository``. |
| 10 |
This function will be used to inject a repository instance into our controller routes. |
| 11 |
Note that we are only passing in the database session in this example with no other |
| 12 |
parameters. |
| 13 |
|
| 14 |
.. literalinclude:: /examples/contrib/sqlalchemy/sqlalchemy_async_repository.py |
| 15 |
:language: python |
| 16 |
:caption: ``app.py`` |
| 17 |
:lines: 82-84 |
| 18 |
:linenos: |
| 19 |
|
| 20 |
Because we'll be using the SQLAlchemy plugin in Litestar, the session is automatically |
| 21 |
configured as a dependency. |
| 22 |
|
| 23 |
By default, the repository doesn't add any additional query options to your base |
| 24 |
statement, but provides the flexibility to override it, allowing you to pass your own |
| 25 |
statement: |
| 26 |
|
| 27 |
.. literalinclude:: /examples/contrib/sqlalchemy/sqlalchemy_async_repository.py |
| 28 |
:language: python |
| 29 |
:caption: ``app.py`` |
| 30 |
:lines: 87-94 |
| 31 |
:linenos: |
| 32 |
|
| 33 |
In this instance, we enhance the repository function by adding a ``selectinload`` |
| 34 |
option. This option configures the specified relationship to load via |
| 35 |
`SELECT .. IN ...` loading pattern, optimizing the query execution. |
| 36 |
|
| 37 |
Next, we define the ``AuthorController``. This controller exposes five routes for |
| 38 |
interacting with the ``Author`` model: |
| 39 |
|
| 40 |
.. dropdown:: ``AuthorController`` (click to toggle) |
| 41 |
|
| 42 |
.. literalinclude:: /examples/contrib/sqlalchemy/sqlalchemy_async_repository.py |
| 43 |
:language: python |
| 44 |
:caption: ``app.py`` |
| 45 |
:lines: 120-199 |
| 46 |
:linenos: |
| 47 |
|
| 48 |
In our list detail endpoint, we use the pagination filter for limiting the amount of |
| 49 |
data returned, allowing us to retrieve large datasets in smaller, more manageable chunks. |
| 50 |
|
| 51 |
In the above examples, we've used the asynchronous repository implementation. However, |
| 52 |
Litestar also supports synchronous database drivers with an identical implementation. |
| 53 |
Here's a corresponding synchronous version of the previous example: |
| 54 |
|
| 55 |
.. dropdown:: Synchronous Repository (click to toggle) |
| 56 |
|
| 57 |
.. literalinclude:: /examples/contrib/sqlalchemy/sqlalchemy_sync_repository.py |
| 58 |
:language: python |
| 59 |
:caption: ``app.py`` |
| 60 |
:linenos: |
| 61 |
|
| 62 |
The examples above enable a feature-complete CRUD service that includes pagination! In |
| 63 |
the next section, we'll explore how to extend the built-in repository to add additional |
| 64 |
functionality to our application. |
| 65 |
|
| 66 |
.. _03-repo-full-code: |
| 67 |
|
| 68 |
Full Code |
| 69 |
--------- |
| 70 |
|
| 71 |
.. dropdown:: Full Code (click to toggle) |
| 72 |
|
| 73 |
.. literalinclude:: /examples/contrib/sqlalchemy/sqlalchemy_async_repository.py |
| 74 |
:language: python |
| 75 |
:caption: ``app.py`` |
| 76 |
:emphasize-lines: 82-84, 87-94, 120-199 |
| 77 |
:linenos: |