|
| 1 | +# Plugins |
| 2 | + |
| 3 | +You can extend Starlite to support non-pydantic / dataclass types using plugins. |
| 4 | + |
| 5 | +Using plugins, you can have Starlite parse and validate inbound values (e.g. request-body data or parameters) as if they |
| 6 | +were pydantic models, and then serialize the data into the desired model type, or list thereof. Plugins also allow you |
| 7 | +to return an instance or list of instances of a model, and have it serialized correctly. |
| 8 | + |
| 9 | +## Builtin Plugins |
| 10 | + |
| 11 | +Currently, Starlite includes a single plugin `starlite.plugins.sql_alchemy.SQLAlchemyPlugin`, with other plugins being |
| 12 | +planned / discussed - see the pinned issues in github for the current state of these. |
| 13 | + |
| 14 | +### SQLAlchemyPlugin |
| 15 | + |
| 16 | +To use the `SQLAlchemyPlugin` simply import it and pass it to the `Starlite` constructor: |
| 17 | + |
| 18 | +```python title="my_app/main.py" |
| 19 | +from starlite import Starlite, SQLAlchemyPlugin |
| 20 | + |
| 21 | +app = Starlite(route_handlers=[...], plugins=[SQLAlchemyPlugin()]) |
| 22 | +``` |
| 23 | + |
| 24 | +!!! note |
| 25 | + The `SQLAlchemyPlugin` *will not* create a DB connection, a sessionmaker or anything of this kind. This |
| 26 | + you will need to implement on your own according to the pattern of your choice, or using a 3rd party solution of some |
| 27 | + sort. The reason for this is that SQL Alchemy is very flexible and allows you to interact with it in various ways. |
| 28 | + We cannot decide upon the pattern that will fit your architecture in advance, and hence it is left to the user to decide. |
| 29 | + |
| 30 | +You can now use SQL alchemy declarative classes as route handler parameters or return values: |
| 31 | + |
| 32 | +```python title="my_app/company/models/company.py" |
| 33 | +from sqlalchemy import Column, Float, Integer, String |
| 34 | +from sqlalchemy.orm import declarative_base |
| 35 | + |
| 36 | +Base = declarative_base() |
| 37 | + |
| 38 | +class Company(Base): |
| 39 | + id = Column(Integer, primary_key=True) |
| 40 | + name = Column(String) |
| 41 | + worth = Column(Float) |
| 42 | +``` |
| 43 | + |
| 44 | +```python title="my_app/company/endpoints.py" |
| 45 | +from starlite import post, get |
| 46 | + |
| 47 | +from my_app.company.models import Company |
| 48 | + |
| 49 | +@post(path="/companies") |
| 50 | +def create_company(data: Company) -> Company: |
| 51 | + ... |
| 52 | + |
| 53 | + |
| 54 | +@get(path="/companies") |
| 55 | +def get_companies() -> List[Company]: |
| 56 | + ... |
| 57 | +``` |
| 58 | + |
| 59 | +!!! important |
| 60 | + The `SQLAlchemyPlugin` supports only `declarative` style classes, it does not support the older `imperative` style |
| 61 | + because this style does not use classes, and is very hard to convert to pydantic correctly. |
| 62 | + |
| 63 | + |
| 64 | +## Creating Plugins |
| 65 | + |
| 66 | +A plugin is a class the implements the `starlite.plugins.base.PluginProtocol` class, which expects a generic `T` |
| 67 | +representing the model type to be used. |
| 68 | + |
| 69 | +To create a plugin you must implement the following methods: |
| 70 | + |
| 71 | +```python |
| 72 | +def to_pydantic_model_class( |
| 73 | + self, model_class: Type[T], **kwargs: Any |
| 74 | +) -> Type[BaseModel]: |
| 75 | + """ |
| 76 | + Given a model_class T, convert it to a subclass of the pydantic BaseModel |
| 77 | + """ |
| 78 | + ... |
| 79 | + |
| 80 | + |
| 81 | +@staticmethod |
| 82 | +def is_plugin_supported_type(value: Any) -> bool: |
| 83 | + """ |
| 84 | + Given a value of indeterminate type, determine if this value is supported by the plugin by returning a bool. |
| 85 | + """ |
| 86 | + ... |
| 87 | + |
| 88 | + |
| 89 | +def from_pydantic_model_instance( |
| 90 | + self, model_class: Type[T], pydantic_model_instance: BaseModel |
| 91 | +) -> T: |
| 92 | + """ |
| 93 | + Given an instance of a pydantic model created using a plugin's 'to_pydantic_model_class', |
| 94 | + return an instance of the class from which that pydantic model has been created. |
| 95 | +
|
| 96 | + This class is passed in as the 'model_class' kwarg. |
| 97 | + """ |
| 98 | + ... |
| 99 | + |
| 100 | + |
| 101 | +def to_dict(self, model_instance: T) -> Dict[str, Any]: |
| 102 | + """ |
| 103 | + Given an instance of a model supported by the plugin, return a dictionary of serilizable values. |
| 104 | + """ |
| 105 | + ... |
| 106 | +``` |
0 commit comments