Skip to content

Conversation

@NFUChen
Copy link
Contributor

@NFUChen NFUChen commented Jan 7, 2025

Description:

This PR introduces several enhancements to the CrudRepositoryImplementationService to provide better support for dynamic query skipping and handling query methods with templates.

Key Changes:

  1. Skip Functions Mechanism:

    • Introduced a class-level variable skip_functions in CrudRepositoryImplementationService to store a list of method names that should be skipped while building queries.
    • A new method add_skip_function allows adding methods to this skip list.
  2. Dynamic Query Skipping:

    • The _implemenmt_query method has been updated to check if a method is in the skip_functions list. If so, it will skip processing that method, logging the action.
  3. Query Decorator:

    • Added a new Query decorator to enable marking methods with a query template.
    • The decorator checks for the correct return type and argument validation.
    • Supports return types of BaseModel or list[BaseModel], with proper handling of results from the database query.
    • Improved error handling for missing arguments and return type mismatches.
  4. Session Management:

    • Integrated PySpringModel.create_session() for handling database sessions within the Query decorator to execute SQL queries.

Benefits:

  • Flexibility: This allows skipping certain repository methods from query implementation without changing the method code itself.
  • Query Customization: The Query decorator provides an easy way to attach SQL templates to methods and automatically handle session and return types.
  • Error Handling: Ensures required arguments are passed and validates return types, preventing runtime issues.

Additional Notes:

  • Please ensure the database session management aligns with your project's session handling mechanism.
  • The Query decorator assumes the BaseModel class has a model_validate method for result parsing.

Related Issues:

  • This change is part of an ongoing effort to enhance the flexibility and scalability of the repository pattern in our codebase.
…dRepository - Introduced a `skip_functions` class variable and `add_skip_function` method to mark repository methods that should be skipped during query implementation. - Modified `_implemenmt_query` to skip marked methods while building queries. - Added `Query` decorator to allow query templates for methods and automatically skip them in the query process. - Enhanced the decorator to validate the return type and session management, supporting both single results and lists of `BaseModel` types. - Added better error handling for missing required arguments and invalid return types.
@NFUChen NFUChen added the enhancement New feature or request label Jan 7, 2025
@NFUChen NFUChen self-assigned this Jan 7, 2025
@NFUChen NFUChen merged commit 3370694 into main Jan 7, 2025
@NFUChen NFUChen deleted the pyspring-query branch January 7, 2025 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

3 participants