You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

factory.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. DifyAPI Repository Factory for creating repository instances.
  3. This factory is specifically designed for DifyAPI repositories that handle
  4. service-layer operations with dependency injection patterns.
  5. """
  6. from sqlalchemy.orm import sessionmaker
  7. from configs import dify_config
  8. from core.repositories import DifyCoreRepositoryFactory, RepositoryImportError
  9. from libs.module_loading import import_string
  10. from repositories.api_workflow_node_execution_repository import DifyAPIWorkflowNodeExecutionRepository
  11. from repositories.api_workflow_run_repository import APIWorkflowRunRepository
  12. class DifyAPIRepositoryFactory(DifyCoreRepositoryFactory):
  13. """
  14. Factory for creating DifyAPI repository instances based on configuration.
  15. This factory handles the creation of repositories that are specifically designed
  16. for service-layer operations and use dependency injection with sessionmaker
  17. for better testability and separation of concerns.
  18. """
  19. @classmethod
  20. def create_api_workflow_node_execution_repository(
  21. cls, session_maker: sessionmaker
  22. ) -> DifyAPIWorkflowNodeExecutionRepository:
  23. """
  24. Create a DifyAPIWorkflowNodeExecutionRepository instance based on configuration.
  25. This repository is designed for service-layer operations and uses dependency injection
  26. with a sessionmaker for better testability and separation of concerns. It provides
  27. database access patterns specifically needed by service classes, handling queries
  28. that involve database-specific fields and multi-tenancy concerns.
  29. Args:
  30. session_maker: SQLAlchemy sessionmaker to inject for database session management.
  31. Returns:
  32. Configured DifyAPIWorkflowNodeExecutionRepository instance
  33. Raises:
  34. RepositoryImportError: If the configured repository cannot be imported or instantiated
  35. """
  36. class_path = dify_config.API_WORKFLOW_NODE_EXECUTION_REPOSITORY
  37. try:
  38. repository_class = import_string(class_path)
  39. return repository_class(session_maker=session_maker) # type: ignore[no-any-return]
  40. except (ImportError, Exception) as e:
  41. raise RepositoryImportError(
  42. f"Failed to create DifyAPIWorkflowNodeExecutionRepository from '{class_path}': {e}"
  43. ) from e
  44. @classmethod
  45. def create_api_workflow_run_repository(cls, session_maker: sessionmaker) -> APIWorkflowRunRepository:
  46. """
  47. Create an APIWorkflowRunRepository instance based on configuration.
  48. This repository is designed for service-layer WorkflowRun operations and uses dependency
  49. injection with a sessionmaker for better testability and separation of concerns. It provides
  50. database access patterns specifically needed by service classes for workflow run management,
  51. including pagination, filtering, and bulk operations.
  52. Args:
  53. session_maker: SQLAlchemy sessionmaker to inject for database session management.
  54. Returns:
  55. Configured APIWorkflowRunRepository instance
  56. Raises:
  57. RepositoryImportError: If the configured repository cannot be imported or instantiated
  58. """
  59. class_path = dify_config.API_WORKFLOW_RUN_REPOSITORY
  60. try:
  61. repository_class = import_string(class_path)
  62. return repository_class(session_maker=session_maker) # type: ignore[no-any-return]
  63. except (ImportError, Exception) as e:
  64. raise RepositoryImportError(f"Failed to create APIWorkflowRunRepository from '{class_path}': {e}") from e