| 1 |
Adding Additional Features to the Repository |
| 2 |
-------------------------------------------- |
| 3 |
|
| 4 |
While most of the functionality you need is built into the repository, there are still |
| 5 |
cases where you need to add in additional functionality. Let's explore ways that we |
| 6 |
can add functionality on top of the repository pattern. |
| 7 |
|
| 8 |
.. tip:: The full code for this tutorial can be found below in the :ref:`Full Code <04-repo-full-code>` section. |
| 9 |
|
| 10 |
Slug Fields |
| 11 |
----------- |
| 12 |
|
| 13 |
.. literalinclude:: /examples/contrib/sqlalchemy/sqlalchemy_repository_extension.py |
| 14 |
:language: python |
| 15 |
:caption: ``app.py`` |
| 16 |
:lines: 10-23, 33-34, 37-42, 101-102, 106-108 |
| 17 |
:linenos: |
| 18 |
|
| 19 |
In this example, we are using a ``BlogPost`` model to hold blog post titles and |
| 20 |
contents. The primary key for this model is a ``UUID`` type. ``UUID`` and ``int`` are |
| 21 |
good options for primary keys, but there are a number of reasons you may not want to use |
| 22 |
them in your routes. For instance, it can be a security problem to expose integer-based |
| 23 |
primary keys in the URL. While UUIDs don't have this same problem, they are not |
| 24 |
user-friendly or easy-to-remember, and create complex URLs. One way to solve this is to |
| 25 |
add a user friendly unique identifier to the table that can be used for urls. This is |
| 26 |
often called a "slug". |
| 27 |
|
| 28 |
First, we'll create a ``SlugKey`` field mixin that adds a text-based, URL-friendly, |
| 29 |
unique column ``slug`` to the table. We want to ensure we create a slug value based on |
| 30 |
the data passed to the ``title`` field. To demonstrate what we are trying to |
| 31 |
accomplish, we want a record that has a blog title of "Follow the Yellow Brick Road!" |
| 32 |
to have the slugified value of "follow-the-yellow-brick-road". |
| 33 |
|
| 34 |
.. literalinclude:: /examples/contrib/sqlalchemy/sqlalchemy_repository_extension.py |
| 35 |
:language: python |
| 36 |
:caption: ``app.py`` |
| 37 |
:lines: 1-8, 14-23, 43-44, 46-100 |
| 38 |
:linenos: |
| 39 |
|
| 40 |
Since the ``BlogPost.title`` field is not marked as unique, this means that we'll have |
| 41 |
to test the slug value for uniqueness before the insert. If the initial slug is found, |
| 42 |
a random set of digits are appended to the end of the slug to make it unique. |
| 43 |
|
| 44 |
.. literalinclude:: /examples/contrib/sqlalchemy/sqlalchemy_repository_extension.py |
| 45 |
:language: python |
| 46 |
:caption: ``app.py`` |
| 47 |
:lines: 1-23, 27-32, 101-102, 106-126, 170-180 |
| 48 |
:linenos: |
| 49 |
|
| 50 |
We are all set to use this in our routes now. First, we'll convert our incoming |
| 51 |
Pydantic model to a dictionary. Next, we'll fetch a unique slug for our text. |
| 52 |
Finally, we insert the model with the added slug. |
| 53 |
|
| 54 |
.. note:: |
| 55 |
|
| 56 |
Using this method does introduce an additional query on each insert. This should be |
| 57 |
considered when determining which fields actually need this type of functionality. |
| 58 |
|
| 59 |
.. _04-repo-full-code: |
| 60 |
|
| 61 |
Full Code |
| 62 |
--------- |
| 63 |
|
| 64 |
.. dropdown:: Full Code (click to toggle) |
| 65 |
|
| 66 |
.. literalinclude:: /examples/contrib/sqlalchemy/sqlalchemy_repository_extension.py |
| 67 |
:language: python |
| 68 |
:caption: ``app.py`` |
| 69 |
:emphasize-lines: 12, 37-42, 106-108, 177 |
| 70 |
:linenos: |