@@ -524,39 +524,43 @@ async def get_by_name_eager(
524524 ) -> Optional ["Node" ]:
525525 """
526526 Get a node by name with eager loading to minimize database queries.
527-
527+
528528 This method loads all commonly needed relationships in 1-2 queries instead
529529 of the N+1 pattern that happens with lazy loading. This significantly speeds
530530 up query building by eliminating redundant database round trips.
531-
531+
532532 Args:
533533 session: Database session
534534 name: Node name
535535 load_dimensions: Whether to eagerly load dimension links and their targets
536536 load_parents: Whether to eagerly load parent node relationships
537537 raise_if_not_exists: Whether to raise exception if node not found
538-
538+
539539 Returns:
540540 Node with all relationships eagerly loaded, or None if not found
541-
541+
542542 Example:
543543 >>> node = await Node.get_by_name_eager(session, "default.revenue", load_dimensions=True)
544544 >>> # All columns, dimension_links, availability already loaded - no extra queries!
545545 >>> columns = node.current.columns # Already loaded
546546 >>> links = node.current.dimension_links # Already loaded
547547 """
548548 from datajunction_server .database .dimensionlink import DimensionLink
549-
549+
550550 # Build base query
551- statement = select (Node ).where (Node .name == name ).where (is_ (Node .deactivated_at , None ))
552-
551+ statement = (
552+ select (Node ).where (Node .name == name ).where (is_ (Node .deactivated_at , None ))
553+ )
554+
553555 # Eagerly load current revision with all its relationships
554556 options = [
555557 joinedload (Node .current ).options (
556558 # Load columns with their attributes
557559 selectinload (NodeRevision .columns ).options (
558560 selectinload (Column .attributes ),
559- joinedload (Column .dimension ), # Load dimension references on columns
561+ joinedload (
562+ Column .dimension ,
563+ ), # Load dimension references on columns
560564 ),
561565 # Load availability state
562566 joinedload (NodeRevision .availability ),
@@ -574,11 +578,13 @@ async def get_by_name_eager(
574578 selectinload (Node .created_by ),
575579 selectinload (Node .owners ),
576580 ]
577-
581+
578582 # Optionally load dimension links (common for query building with dimensions)
579583 if load_dimensions :
580584 options .append (
581- joinedload (Node .current ).selectinload (NodeRevision .dimension_links ).options (
585+ joinedload (Node .current )
586+ .selectinload (NodeRevision .dimension_links )
587+ .options (
582588 # Load the target dimension node
583589 joinedload (DimensionLink .dimension ).options (
584590 # Load dimension's current revision
@@ -589,31 +595,33 @@ async def get_by_name_eager(
589595 joinedload (NodeRevision .availability ),
590596 ),
591597 ),
592- )
598+ ),
593599 )
594-
600+
595601 # Optionally load parent nodes (for building upstream references)
596602 if load_parents :
597603 options .append (
598- joinedload (Node .current ).selectinload (NodeRevision .parents ).options (
604+ joinedload (Node .current )
605+ .selectinload (NodeRevision .parents )
606+ .options (
599607 joinedload (Node .current ).options (
600608 selectinload (NodeRevision .columns ),
601609 joinedload (NodeRevision .availability ),
602610 ),
603- )
611+ ),
604612 )
605-
613+
606614 statement = statement .options (* options )
607-
615+
608616 result = await session .execute (statement )
609617 node = result .unique ().scalar_one_or_none ()
610-
618+
611619 if not node and raise_if_not_exists :
612620 raise DJNodeNotFound (
613621 message = f"Node `{ name } ` does not exist." ,
614622 http_status_code = 404 ,
615623 )
616-
624+
617625 return node
618626
619627 @classmethod
0 commit comments