| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column |
| 6 |
|
| 7 |
from litestar import Litestar, post |
| 8 |
from litestar.dto import dto_field |
| 9 |
from litestar.plugins.sqlalchemy import SQLAlchemySerializationPlugin |
| 10 |
|
| 11 |
if TYPE_CHECKING: |
| 12 |
from typing import List |
| 13 |
|
| 14 |
|
| 15 |
class Base(DeclarativeBase): ... |
| 16 |
|
| 17 |
|
| 18 |
class TodoItem(Base): |
| 19 |
__tablename__ = "todo_item" |
| 20 |
title: Mapped[str] = mapped_column(primary_key=True) |
| 21 |
done: Mapped[bool] |
| 22 |
super_secret_value: Mapped[str] = mapped_column(info=dto_field("private")) |
| 23 |
|
| 24 |
|
| 25 |
@post("/") |
| 26 |
async def add_item(data: TodoItem) -> List[TodoItem]: |
| 27 |
data.super_secret_value = "This is a secret" |
| 28 |
return [data] |
| 29 |
|
| 30 |
|
| 31 |
app = Litestar(route_handlers=[add_item], plugins=[SQLAlchemySerializationPlugin()]) |