@@ -12,11 +12,12 @@ Understanding Services
1212Services 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
2122Basic 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
0 commit comments