Skip to content

Commit 1acaba6

Browse files
committed
chore: docs
1 parent 504b849 commit 1acaba6

3 files changed

Lines changed: 91 additions & 23 deletions

File tree

docs/changelog.rst

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,13 @@
1111
:pr: 482
1212
:issue: 481
1313

14-
<!--
15-
By submitting this pull request, you agree to:
16-
- follow [Litestar's Code of Conduct](https://github.com/litestar-org/.github/blob/main/CODE_OF_CONDUCT.md)
17-
- follow [Litestar's contribution guidelines](https://github.com/litestar-org/.github/blob/main/CONTRIBUTING.md)
18-
- follow the [PSFs's Code of Conduct](https://www.python.org/psf/conduct/)
19-
-->
20-
## Description
21-
2214
Adds [`DefaultBase`](https://github.com/litestar-org/advanced-alchemy/blob/6cc26ef8d53bc04f89a070337f8b0ab07a1bac46/advanced_alchemy/base.py#L517) class to `__all__` to match other public classes in the module.
2315

24-
<!--
25-
Please add in issue numbers this pull request will close, if applicable
26-
Examples: Fixes #4321 or Closes #1234
27-
28-
Ensure you are using a supported keyword to properly link an issue:
29-
https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
30-
-->
31-
## Fixes
32-
33-
#481
34-
3516
.. change:: Update list and count
3617
:type: bugfix
3718
:pr: 487
3819

20+
Minor adjustment to the list and count method
3921

4022

4123
.. changelog:: 1.4.4

docs/usage/services.rst

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ Understanding Services
1212
Services provide:
1313

1414
- Business logic abstraction
15-
- Data transformation using Pydantic or Msgspec models
16-
- Input validation
15+
- Data transformation using Pydantic, Msgspec, or attrs models
16+
- Input validation and type-safe schema conversion
1717
- Complex operations involving multiple repositories
1818
- Consistent error handling
1919
- Automatic schema validation and transformation
20+
- Support for SQLAlchemy query results (Row types) and RowMapping objects
2021

2122
Basic Service Usage
2223
-------------------
@@ -210,6 +211,91 @@ The code below shows a service coordinating posts and tags.
210211
data["slug"] = await self.repository.get_available_slug(data["name"])
211212
return await super().to_model(data, operation)
212213
214+
Schema Integration
215+
------------------
216+
217+
Advanced Alchemy services support multiple schema libraries for data transformation and validation:
218+
219+
Pydantic Models
220+
***************
221+
222+
.. code-block:: python
223+
224+
from pydantic import BaseModel
225+
from typing import Optional
226+
227+
class UserSchema(BaseModel):
228+
name: str
229+
email: str
230+
age: Optional[int] = None
231+
232+
model_config = {"from_attributes": True}
233+
234+
# Convert database model to Pydantic schema
235+
user_data = service.to_schema(user_model, schema_type=UserSchema)
236+
237+
Msgspec Structs
238+
***************
239+
240+
.. code-block:: python
241+
242+
from msgspec import Struct
243+
from typing import Optional
244+
245+
class UserStruct(Struct):
246+
name: str
247+
email: str
248+
age: Optional[int] = None
249+
250+
# Convert database model to Msgspec struct
251+
user_data = service.to_schema(user_model, schema_type=UserStruct)
252+
253+
Attrs Classes
254+
*************
255+
256+
.. code-block:: python
257+
258+
from attrs import define
259+
from typing import Optional
260+
261+
@define
262+
class UserAttrs:
263+
name: str
264+
email: str
265+
age: Optional[int] = None
266+
267+
# Convert database model to attrs class
268+
user_data = service.to_schema(user_model, schema_type=UserAttrs)
269+
270+
.. note::
271+
272+
**Enhanced attrs Support with cattrs**: When both ``attrs`` and ``cattrs`` are installed,
273+
Advanced Alchemy automatically uses ``cattrs.structure()`` and ``cattrs.unstructure()``
274+
for improved performance and type-aware serialization. This provides better handling of
275+
complex types, nested structures, and custom converters.
276+
277+
SQLAlchemy Query Result Support
278+
*******************************
279+
280+
Services now provide comprehensive support for SQLAlchemy query results:
281+
282+
.. code-block:: python
283+
284+
from sqlalchemy import select
285+
286+
# Direct support for SQLAlchemy Row objects
287+
query_results = await session.execute(select(User))
288+
rows = query_results.fetchall() # Returns list[Row[Any]]
289+
290+
# Convert Row objects to schema types
291+
user_data = service.to_schema(rows[0], schema_type=UserSchema)
292+
users_paginated = service.to_schema(rows, schema_type=UserSchema)
293+
294+
# Also supports RowMapping objects
295+
row_mapping_results = await session.execute(select(User)).mappings()
296+
mapping_data = service.to_schema(row_mapping_results.first(), schema_type=UserSchema)
297+
298+
213299
Framework Integration
214300
---------------------
215301

tests/unit/test_attrs_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import Any
5+
from typing import Any, Optional
66

77
import pytest
88
from attrs import define, field
@@ -40,7 +40,7 @@ class AttrsWithOptional:
4040
"""attrs class with optional fields."""
4141

4242
name: str
43-
email: str | None = None
43+
email: Optional[str] = None # noqa: UP045
4444
active: bool = True
4545

4646

0 commit comments

Comments
 (0)