Skip to content

Conversation

@igorbenav
Copy link
Collaborator

Summary

 🚀Features

  • Now it's finally possible to get_joined and get_multi_joined in CRUDBase class🎉

📝Docs

Get Joined

To retrieve data with a join operation, you can use the get_joined method from your CRUD module. Here's how to do it:

# Fetch a single record with a join on another model (e.g., User and Tier).
result = await crud_users.get_joined(
    db=db,  # The SQLAlchemy async session.
    join_model=Tier,  # The model to join with (e.g., Tier).
    schema_to_select=UserSchema,  # Pydantic schema for selecting User model columns (optional).
    join_schema_to_select=TierSchema  # Pydantic schema for selecting Tier model columns (optional).
)

Relevant Parameters:

  • join_model: The model you want to join with (e.g., Tier).
  • join_prefix: Optional prefix to be added to all columns of the joined model. If None, no prefix is added.
  • 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.
  • schema_to_select: A Pydantic schema to select specific columns from the primary model (e.g., UserSchema).
  • join_schema_to_select: A Pydantic schema to select specific columns from the joined model (e.g., TierSchema).
  • join_type: pecifies the type of join operation to perform. Can be "left" for a left outer join or "inner" for an inner join. Default "left".
  • kwargs: Filters to apply to the primary query.

This method allows you to perform a join operation, selecting columns from both models, and retrieve a single record.

Get Multi Joined

Similarly, to retrieve multiple records with a join operation, you can use the get_multi_joined method. Here's how:

# Retrieve a list of objects with a join on another model (e.g., User and Tier).
result = await crud_users.get_multi_joined(
    db=db,  # The SQLAlchemy async session.
    join_model=Tier,  # The model to join with (e.g., Tier).
    join_prefix="tier_",  # Optional prefix for joined model columns.
    join_on=and_(User.tier_id == Tier.id, User.is_superuser == True),  # Custom join condition.
    schema_to_select=UserSchema,  # Pydantic schema for selecting User model columns.
    join_schema_to_select=TierSchema,  # Pydantic schema for selecting Tier model columns.
    username="john_doe"  # Additional filter parameters.
)

Relevant Parameters:

  • join_model: The model you want to join with (e.g., Tier).
  • join_prefix: Optional prefix to be added to all columns of the joined model. If None, no prefix is added.
  • 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.
  • schema_to_select: A Pydantic schema to select specific columns from the primary model (e.g., UserSchema).
  • join_schema_to_select: A Pydantic schema to select specific columns from the joined model (e.g., TierSchema).
  • join_type: pecifies the type of join operation to perform. Can be "left" for a left outer join or "inner" for an inner join. Default "left".
  • kwargs: Filters to apply to the primary query.
  • offset: The offset (number of records to skip) for pagination. Default 0.
  • limit: The limit (maximum number of records to return) for pagination. Default 100.
  • kwargs: Filters to apply to the primary query.

🚚Migration

  • Migration should be pretty smooth for this one. You are able to just use the get_joined and get_multi_joined methods out of the box with your CRUD classes inheriting from CRUDBase.

 🔎Bug fixes

  • ForeignKey added to tier_id in user model

@igorbenav igorbenav added the enhancement New feature or request label Nov 22, 2023
@igorbenav igorbenav self-assigned this Nov 22, 2023
@igorbenav igorbenav merged commit cc3e465 into main Nov 22, 2023
@igorbenav igorbenav deleted the crud-joined branch November 22, 2023 02:14
@igorbenav igorbenav mentioned this pull request Nov 22, 2023
15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants