57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
# Importações de bibliotecas padrão
|
|
from collections.abc import Callable
|
|
from typing import Type, TypeVar
|
|
|
|
# Importações de bibliotecas de terceiros
|
|
from fastapi import Depends
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
# Importações do seu próprio projeto
|
|
from app.database import models, RelationalTableRepository
|
|
from app.multi_tenant.tenant_utils import get_tenant_schema
|
|
|
|
|
|
# def get_repository_simple_table(
|
|
# model: type[models.Base],
|
|
# ) -> Callable[[AsyncSession], RepositoryBase.RepositoryBase]:
|
|
# def func(session_simple_table: AsyncSession = Depends(get_tenant_schema)): # Sem parênteses
|
|
# return RepositoryBase.RepositoryBase(model, session_simple_table)
|
|
#
|
|
# return func
|
|
|
|
|
|
def get_repository_relational_table(
|
|
model: type[models.Base],
|
|
) -> Callable[[AsyncSession], RelationalTableRepository.RelationalTableRepository]:
|
|
def func(session_relational_table: AsyncSession = Depends(get_tenant_schema)):
|
|
return RelationalTableRepository.RelationalTableRepository(model, session_relational_table)
|
|
|
|
return func
|
|
|
|
|
|
# def get_repository_s3(
|
|
# model: type[models.Base],
|
|
# ) -> Callable[[AsyncSession], RepositoryS3.RepositoryS3]:
|
|
# def func(session_relational_table: AsyncSession = Depends(get_tenant_schema)):
|
|
# return RepositoryS3.RepositoryS3(model, session_relational_table)
|
|
#
|
|
# return func
|
|
|
|
|
|
# Tipo genérico para repositórios
|
|
T = TypeVar("T")
|
|
|
|
|
|
def get_repository(
|
|
model: Type[models.Base],
|
|
repository_class: Type[T]
|
|
) -> Callable[[AsyncSession], T]:
|
|
"""
|
|
Função genérica para criar dependências de repositórios.
|
|
"""
|
|
|
|
def func(session: AsyncSession = Depends(get_tenant_schema)) -> T:
|
|
return repository_class(model, session)
|
|
|
|
return func
|