Skip to content

Commit a0e3057

Browse files
committed
Add documentation
1 parent a3feed8 commit a0e3057

File tree

8 files changed

+99
-3
lines changed

8 files changed

+99
-3
lines changed

docs/source/examples/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ Examples
1818
all_of
1919
inheritance
2020
json
21+
mixins

docs/source/examples/mixins.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Mixin Classes
2+
=============
3+
4+
SQLAlchemy supports mixin classes for models to add functionality. OpenAlchemy
5+
supports this through the :samp:`x-mixins` extension property that may define
6+
one or more importable class to mix into the model class.
7+
8+
.. seealso::
9+
10+
:ref:`mixins`
11+
OpenAlchemy documentation for the :samp:`x-mixins` value.
12+
13+
The following example adds the :samp:`sqlalchemy_mixins.TimestampsMixin` to
14+
the :samp:`Employee` model:
15+
16+
.. literalinclude:: ../../../examples/mixins/example-spec.yml
17+
:language: yaml
18+
:linenos:
19+
20+
The following file uses OpenAlchemy to generate the SQLAlchemy models:
21+
22+
.. literalinclude:: ../../../examples/mixins/models.py
23+
:language: python
24+
:linenos:
25+
26+
The SQLAlchemy models generated by OpenAlchemy are equivalent to the following
27+
traditional models file:
28+
29+
.. literalinclude:: ../../../examples/mixins/models_traditional.py
30+
:language: python
31+
:linenos:
32+
33+
OpenAlchemy will generate the following typed models:
34+
35+
.. literalinclude:: ../../../examples/mixins/models_auto.py
36+
:language: python
37+
:linenos:

docs/source/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ the documentation:
360360
+------------------------------+----------------------------------------------------+
361361
| :samp:`x-foreign-key-colum` | :ref:`custom-foreign-key` |
362362
+------------------------------+----------------------------------------------------+
363+
| :samp:`x-mixins` | :ref:`mixins` |
364+
+------------------------------+----------------------------------------------------+
363365
| :samp:`x-backrefs` | :ref:`Models File Note <backrefs>` |
364366
+------------------------------+----------------------------------------------------+
365367
| :samp:`x-de-$ref` | :ref:`from_dict Note <de-ref>` |

docs/source/technical_details/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Technical Details
1212
relationships
1313
inheritance
1414
model
15+
mixins

docs/source/technical_details/inheritance.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ The required :samp:`__mapper_args__` must be added using :samp:`x-kwargs`. For
120120
example, for :samp:`Employee`:
121121

122122
.. code-block:: yaml
123-
:linenos:
123+
:linenos:
124124
125-
Employee:
125+
Employee:
126126
type: object
127127
...
128128
x-kwargs:
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.. _mixins:
2+
3+
Mixin Classes
4+
=============
5+
6+
SQLAlchemy supports adding common functionality from a class to many other
7+
classes through mixin classes. OpenAlchemy supports this through the
8+
:samp:`x-mixins` extension property.
9+
10+
.. seealso::
11+
12+
`SQLAlchemy mixin documentation <https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/mixins.html#mixing-in-columns>`_
13+
Documentation for SQLAlchemy mixin.
14+
15+
The following rules apply to the :samp:`x-mixins` extension property:
16+
17+
* it can either be a string or a list of strings each of which defines one of
18+
the mixin classes and
19+
* each value must be the fully qualified dotted name of the class to use as the
20+
mixin. Everything before the last dot will be used to import the module and
21+
everything after will be used to retrieve the class from the module.
22+
23+
The following example adds the :samp:`sqlalchemy_mixins.TimestampsMixin` to the
24+
:samp:`Employee` model:
25+
26+
.. code-block:: yaml
27+
:linenos:
28+
29+
Employee:
30+
type: object
31+
x-mixins: sqlalchemy_mixins.TimestampsMixin
32+
...
33+
34+
Alternatively, multiple mixins can be defined using a list:
35+
36+
.. code-block:: yaml
37+
:linenos:
38+
39+
Employee:
40+
type: object
41+
x-mixins:
42+
- sqlalchemy_mixins.TimestampsMixin
43+
- sqlalchemy_mixins.EagerLoadMixin
44+
...
45+
46+
.. seealso::
47+
48+
`sqlalchemy_mixins package <https://github.com/absent1706/sqlalchemy-mixins>`_
49+
Package that defines some helpful SQLAlchemy mixins.

examples/mixins/models_traditional.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Employee(Base, TimestampsMixin):
99
"""Person that works for a company."""
1010

1111
__tablename__ = "employee"
12+
__abstract__ = False
13+
1214
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
1315
name = sa.Column(sa.String, index=True)
1416
division = sa.Column(sa.String, index=True)

tests/open_alchemy/test_model_factory.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,10 @@ def test_mixin(monkeypatch):
562562
mixin_class = type(
563563
"Mixin1",
564564
(),
565-
{"property_2": sqlalchemy.column.Column(sqlalchemy.column.Integer)},
565+
{
566+
"property_2": sqlalchemy.column.Column(sqlalchemy.column.Integer),
567+
"__abstract__": True,
568+
},
566569
)
567570
mock_import_module.return_value.Mixin1 = mixin_class
568571
monkeypatch.setattr(importlib, "import_module", mock_import_module)
@@ -581,6 +584,7 @@ def test_mixin(monkeypatch):
581584
)
582585

583586
assert hasattr(model, "property_2")
587+
assert model.__abstract__ is False
584588

585589

586590
class TestGetSchema:

0 commit comments

Comments
 (0)