-
-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: refactor example in dto factory, moving code to examples folder
- Loading branch information
Showing
9 changed files
with
104 additions
and
98 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
docs/examples/data_transfer_objects/factory/enveloping_return_data_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from dataclasses import dataclass | ||
from typing import Generic, TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
@dataclass | ||
class WithCount(Generic[T]): | ||
count: int | ||
data: List[T] |
6 changes: 6 additions & 0 deletions
6
docs/examples/data_transfer_objects/factory/enveloping_return_data_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from advanced_alchemy.dto import SQLAlchemyDTO | ||
from litestar.dto import DTOConfig | ||
|
||
|
||
class UserDTO(SQLAlchemyDTO[User]): | ||
config = DTOConfig(exclude={"password", "created_at"}) |
16 changes: 16 additions & 0 deletions
16
docs/examples/data_transfer_objects/factory/enveloping_return_data_3.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from litestar import get | ||
|
||
|
||
@get("/users", dto=UserDTO, sync_to_thread=False) | ||
def get_users() -> WithCount[User]: | ||
return WithCount( | ||
count=1, | ||
data=[ | ||
User( | ||
id=1, | ||
name="Litestar User", | ||
password="xyz", | ||
created_at=datetime.now(), | ||
), | ||
], | ||
) |
9 changes: 9 additions & 0 deletions
9
docs/examples/data_transfer_objects/factory/excluding_fields_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
config = DTOConfig( | ||
exclude={ | ||
"id", | ||
"address.id", | ||
"address.street", | ||
"pets.0.id", | ||
"pets.0.user_id", | ||
} | ||
) |
6 changes: 6 additions & 0 deletions
6
docs/examples/data_transfer_objects/factory/paginated_return_data_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from advanced_alchemy.dto import SQLAlchemyDTO | ||
from litestar.dto import DTOConfig | ||
|
||
|
||
class UserDTO(SQLAlchemyDTO[User]): | ||
config = DTOConfig(exclude={"password", "created_at"}) |
19 changes: 19 additions & 0 deletions
19
docs/examples/data_transfer_objects/factory/paginated_return_data_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from litestar import get | ||
from litestar.pagination import ClassicPagination | ||
|
||
|
||
@get("/users", dto=UserDTO, sync_to_thread=False) | ||
def get_users() -> ClassicPagination[User]: | ||
return ClassicPagination( | ||
page_size=10, | ||
total_pages=1, | ||
current_page=1, | ||
items=[ | ||
User( | ||
id=1, | ||
name="Litestar User", | ||
password="xyz", | ||
created_at=datetime.now(), | ||
), | ||
], | ||
) |
6 changes: 6 additions & 0 deletions
6
docs/examples/data_transfer_objects/factory/response_return_data_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from advanced_alchemy.dto import SQLAlchemyDTO | ||
from litestar.dto import DTOConfig | ||
|
||
|
||
class UserDTO(SQLAlchemyDTO[User]): | ||
config = DTOConfig(exclude={"password", "created_at"}) |
14 changes: 14 additions & 0 deletions
14
docs/examples/data_transfer_objects/factory/response_return_data_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from litestar import get, Response | ||
|
||
|
||
@get("/users", dto=UserDTO, sync_to_thread=False) | ||
def get_users() -> Response[User]: | ||
return Response( | ||
content=User( | ||
id=1, | ||
name="Litestar User", | ||
password="xyz", | ||
created_at=datetime.now(), | ||
), | ||
headers={"X-Total-Count": "1"}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters