Soem crudkit fixes aimed at eliminating session sharing.

This commit is contained in:
Yaro Kasear 2025-09-15 11:51:56 -05:00
parent d045a1a05f
commit cf795086f1
10 changed files with 107 additions and 47 deletions

View file

@ -1,4 +1,4 @@
from typing import Any, Type, TypeVar, Generic, Optional, Protocol, runtime_checkable, cast
from typing import Any, Callable, Type, TypeVar, Generic, Optional, Protocol, runtime_checkable, cast
from sqlalchemy.orm import Load, Session, raiseload, with_polymorphic, Mapper
from sqlalchemy.orm.attributes import InstrumentedAttribute
from sqlalchemy.orm.util import AliasedClass
@ -37,13 +37,13 @@ class CRUDService(Generic[T]):
def __init__(
self,
model: Type[T],
session: Session,
session_factory: Callable[[], Session],
polymorphic: bool = False,
*,
backend: Optional[BackendInfo] = None
):
self.model = model
self.session = session
self._session_factory = session_factory
self.polymorphic = polymorphic
self.supports_soft_delete = hasattr(model, 'is_deleted')
# Cache backend info once. If not provided, derive from session bind.
@ -51,6 +51,10 @@ class CRUDService(Generic[T]):
eng: Engine = bind.engine if isinstance(bind, Connection) else cast(Engine, bind)
self.backend = backend or make_backend_info(eng)
@property
def session(self) -> Session:
return self._session_factory()
def get_query(self):
if self.polymorphic:
poly = with_polymorphic(self.model, "*")
@ -69,7 +73,6 @@ class CRUDService(Generic[T]):
return cols or [text("1")]
def get(self, id: int, params=None) -> T | None:
print(f"I AM GETTING A THING! A THINGS! {params}")
query, root_alias = self.get_query()
include_deleted = False