@@ -1401,10 +1401,91 @@ async def get_multi(
14011401
14021402 return response
14031403
1404+ @overload
1405+ async def get_joined (
1406+ self ,
1407+ db : AsyncSession ,
1408+ * ,
1409+ schema_to_select : type [SelectSchemaType ],
1410+ return_as_model : Literal [True ],
1411+ join_model : Optional [ModelType ] = None ,
1412+ join_on : Optional [Union [Join , BinaryExpression ]] = None ,
1413+ join_prefix : Optional [str ] = None ,
1414+ join_schema_to_select : Optional [type [SelectSchemaType ]] = None ,
1415+ join_type : str = "left" ,
1416+ alias : Optional [AliasedClass ] = None ,
1417+ join_filters : Optional [dict ] = None ,
1418+ joins_config : Optional [list [JoinConfig ]] = None ,
1419+ nest_joins : bool = False ,
1420+ relationship_type : Optional [str ] = None ,
1421+ ** kwargs : Any ,
1422+ ) -> Optional [SelectSchemaType ]: ...
1423+
1424+ @overload
1425+ async def get_joined (
1426+ self ,
1427+ db : AsyncSession ,
1428+ * ,
1429+ schema_to_select : None = None ,
1430+ return_as_model : Literal [False ] = False ,
1431+ join_model : Optional [ModelType ] = None ,
1432+ join_on : Optional [Union [Join , BinaryExpression ]] = None ,
1433+ join_prefix : Optional [str ] = None ,
1434+ join_schema_to_select : Optional [type [SelectSchemaType ]] = None ,
1435+ join_type : str = "left" ,
1436+ alias : Optional [AliasedClass ] = None ,
1437+ join_filters : Optional [dict ] = None ,
1438+ joins_config : Optional [list [JoinConfig ]] = None ,
1439+ nest_joins : bool = False ,
1440+ relationship_type : Optional [str ] = None ,
1441+ ** kwargs : Any ,
1442+ ) -> Optional [dict [str , Any ]]: ...
1443+
1444+ @overload
1445+ async def get_joined (
1446+ self ,
1447+ db : AsyncSession ,
1448+ * ,
1449+ schema_to_select : type [SelectSchemaType ],
1450+ return_as_model : Literal [False ] = False ,
1451+ join_model : Optional [ModelType ] = None ,
1452+ join_on : Optional [Union [Join , BinaryExpression ]] = None ,
1453+ join_prefix : Optional [str ] = None ,
1454+ join_schema_to_select : Optional [type [SelectSchemaType ]] = None ,
1455+ join_type : str = "left" ,
1456+ alias : Optional [AliasedClass ] = None ,
1457+ join_filters : Optional [dict ] = None ,
1458+ joins_config : Optional [list [JoinConfig ]] = None ,
1459+ nest_joins : bool = False ,
1460+ relationship_type : Optional [str ] = None ,
1461+ ** kwargs : Any ,
1462+ ) -> Optional [dict [str , Any ]]: ...
1463+
1464+ @overload
1465+ async def get_joined (
1466+ self ,
1467+ db : AsyncSession ,
1468+ * ,
1469+ schema_to_select : Optional [type [SelectSchemaType ]] = None ,
1470+ return_as_model : bool = False ,
1471+ join_model : Optional [ModelType ] = None ,
1472+ join_on : Optional [Union [Join , BinaryExpression ]] = None ,
1473+ join_prefix : Optional [str ] = None ,
1474+ join_schema_to_select : Optional [type [SelectSchemaType ]] = None ,
1475+ join_type : str = "left" ,
1476+ alias : Optional [AliasedClass ] = None ,
1477+ join_filters : Optional [dict ] = None ,
1478+ joins_config : Optional [list [JoinConfig ]] = None ,
1479+ nest_joins : bool = False ,
1480+ relationship_type : Optional [str ] = None ,
1481+ ** kwargs : Any ,
1482+ ) -> Optional [Union [dict [str , Any ], SelectSchemaType ]]: ...
1483+
14041484 async def get_joined (
14051485 self ,
14061486 db : AsyncSession ,
14071487 schema_to_select : Optional [type [SelectSchemaType ]] = None ,
1488+ return_as_model : bool = False ,
14081489 join_model : Optional [ModelType ] = None ,
14091490 join_on : Optional [Union [Join , BinaryExpression ]] = None ,
14101491 join_prefix : Optional [str ] = None ,
@@ -1416,7 +1497,7 @@ async def get_joined(
14161497 nest_joins : bool = False ,
14171498 relationship_type : Optional [str ] = None ,
14181499 ** kwargs : Any ,
1419- ) -> Optional [dict [str , Any ]]:
1500+ ) -> Optional [Union [ dict [str , Any ], SelectSchemaType ]]:
14201501 """
14211502 Fetches a single record with one or multiple joins on other models. If `join_on` is not provided, the method attempts
14221503 to automatically detect the join condition using foreign key relationships. For multiple joins, use `joins_config` to
@@ -1427,6 +1508,7 @@ async def get_joined(
14271508 Args:
14281509 db: The SQLAlchemy async session.
14291510 schema_to_select: Pydantic schema for selecting specific columns from the primary model. Required if `return_as_model` is True.
1511+ return_as_model: If `True`, returns data as a Pydantic model instance based on `schema_to_select`. Defaults to `False`.
14301512 join_model: The model to join with.
14311513 join_on: SQLAlchemy Join object for specifying the `ON` clause of the join. If `None`, the join condition is auto-detected based on foreign keys.
14321514 join_prefix: Optional prefix to be added to all columns of the joined model. If `None`, no prefix is added.
@@ -1440,7 +1522,10 @@ async def get_joined(
14401522 **kwargs: Filters to apply to the primary model query, supporting advanced comparison operators for refined searching.
14411523
14421524 Returns:
1443- A dictionary representing the joined record, or `None` if no record matches the criteria.
1525+ A dictionary or Pydantic model instance representing the joined record, or `None` if no record matches the criteria:
1526+
1527+ - When `return_as_model=True` and `schema_to_select` is provided: `Optional[SelectSchemaType]`
1528+ - When `return_as_model=False`: `Optional[Dict[str, Any]]`
14441529
14451530 Raises:
14461531 ValueError: If both single join parameters and `joins_config` are used simultaneously.
@@ -1708,7 +1793,19 @@ async def get_joined(
17081793 else :
17091794 data_list = []
17101795
1711- return process_joined_data (data_list , join_definitions , nest_joins , self .model )
1796+ processed_data = process_joined_data (
1797+ data_list , join_definitions , nest_joins , self .model
1798+ )
1799+
1800+ if processed_data is None or not return_as_model :
1801+ return processed_data
1802+
1803+ if not schema_to_select :
1804+ raise ValueError (
1805+ "schema_to_select must be provided when return_as_model is True."
1806+ )
1807+
1808+ return schema_to_select (** processed_data )
17121809
17131810 @overload
17141811 async def get_multi_joined (
0 commit comments