Mixin ClassesΒΆ
SQLAlchemy supports mixin classes for models to add functionality. OpenAlchemy
supports this through the x-mixins
extension property that may define
one or more importable class to mix into the model class.
See also
- Mixin Classes
OpenAlchemy documentation for the
x-mixins
value.
The following example adds the sqlalchemy_mixins.TimestampsMixin
to
the Employee
model:
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 52 | openapi: "3.0.0"
info:
title: Test Schema
description: API to illustrate the OpenAlchemy mixin feature.
version: "0.1"
paths:
/employee:
get:
summary: Used to retrieve all employees.
responses:
200:
description: Return all employees from the database.
content:
application/json:
schema:
type: array
items:
"$ref": "#/components/schemas/Employee"
components:
schemas:
Employee:
description: Person that works for a company.
type: object
x-tablename: employee
x-mixins: sqlalchemy_mixins.TimestampsMixin
properties:
id:
type: integer
description: Unique identifier for the employee.
example: 0
x-primary-key: true
x-autoincrement: true
name:
type: string
description: The name of the employee.
example: David Andersson
x-index: true
division:
type: string
description: The part of the company the employee works in.
example: Engineering
x-index: true
salary:
type: number
description: The amount of money the employee is paid.
example: 1000000.00
required:
- name
- division
|
The following file uses OpenAlchemy to generate the SQLAlchemy models:
1 2 3 | from open_alchemy import init_yaml
init_yaml("example-spec.yml", models_filename="models_auto.py")
|
The SQLAlchemy models generated by OpenAlchemy are equivalent to the following traditional models file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_mixins import TimestampsMixin
Base = declarative_base()
class Employee(Base, TimestampsMixin):
"""Person that works for a company."""
__tablename__ = "employee"
__abstract__ = False
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
name = sa.Column(sa.String, index=True)
division = sa.Column(sa.String, index=True)
salary = sa.Column(sa.Float)
|
OpenAlchemy will generate the following typed models:
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | """Autogenerated SQLAlchemy models based on OpenAlchemy models."""
# pylint: disable=no-member,super-init-not-called,unused-argument
import typing
import sqlalchemy
from sqlalchemy import orm
from open_alchemy import models
class _EmployeeDictBase(typing.TypedDict, total=True):
"""TypedDict for properties that are required."""
name: str
division: str
class EmployeeDict(_EmployeeDictBase, total=False):
"""TypedDict for properties that are not required."""
id: int
salary: typing.Optional[float]
class TEmployee(typing.Protocol):
"""
SQLAlchemy model protocol.
Person that works for a company.
Attrs:
id: Unique identifier for the employee.
name: The name of the employee.
division: The part of the company the employee works in.
salary: The amount of money the employee is paid.
"""
# SQLAlchemy properties
__table__: sqlalchemy.Table
__tablename__: str
query: orm.Query
# Model properties
id: "sqlalchemy.Column[int]"
name: "sqlalchemy.Column[str]"
division: "sqlalchemy.Column[str]"
salary: "sqlalchemy.Column[typing.Optional[float]]"
def __init__(
self,
name: str,
division: str,
id: typing.Optional[int] = None,
salary: typing.Optional[float] = None,
) -> None:
"""
Construct.
Args:
id: Unique identifier for the employee.
name: The name of the employee.
division: The part of the company the employee works in.
salary: The amount of money the employee is paid.
"""
...
@classmethod
def from_dict(
cls,
name: str,
division: str,
id: typing.Optional[int] = None,
salary: typing.Optional[float] = None,
) -> "TEmployee":
"""
Construct from a dictionary (eg. a POST payload).
Args:
id: Unique identifier for the employee.
name: The name of the employee.
division: The part of the company the employee works in.
salary: The amount of money the employee is paid.
Returns:
Model instance based on the dictionary.
"""
...
@classmethod
def from_str(cls, value: str) -> "TEmployee":
"""
Construct from a JSON string (eg. a POST payload).
Returns:
Model instance based on the JSON string.
"""
...
def to_dict(self) -> EmployeeDict:
"""
Convert to a dictionary (eg. to send back for a GET request).
Returns:
Dictionary based on the model instance.
"""
...
def to_str(self) -> str:
"""
Convert to a JSON string (eg. to send back for a GET request).
Returns:
JSON string based on the model instance.
"""
...
Employee: typing.Type[TEmployee] = models.Employee # type: ignore
|
See also