mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 03:24:20 +00:00
✨ Add experimental Asyncio SQLAlchemy
https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html#synopsis-core
This commit is contained in:
parent
857ceb7933
commit
d9c6b4b66d
2 changed files with 55 additions and 1 deletions
|
@ -20,7 +20,7 @@ classifiers = [
|
||||||
|
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = "^3.8"
|
python = "^3.8"
|
||||||
sqlalchemy = "^1.4.5"
|
sqlalchemy = "^1.4.9"
|
||||||
toml = "^0.10.1"
|
toml = "^0.10.1"
|
||||||
pydantic = "^1.8.1"
|
pydantic = "^1.8.1"
|
||||||
async-property = "^0.2.1"
|
async-property = "^0.2.1"
|
||||||
|
|
|
@ -6,6 +6,7 @@ This module contains the :class:`~royalnet.engineer.pda.extensions.base.PDAExten
|
||||||
import royalnet.royaltyping as t
|
import royalnet.royaltyping as t
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
import sqlalchemy.orm
|
import sqlalchemy.orm
|
||||||
|
import sqlalchemy.ext.asyncio as sea
|
||||||
import contextlib
|
import contextlib
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -62,6 +63,59 @@ class SQLAlchemyExtension(base.PDAExtension):
|
||||||
log.debug(f"{self!r}: Session closed!")
|
log.debug(f"{self!r}: Session closed!")
|
||||||
|
|
||||||
|
|
||||||
|
class AsyncSQLAlchemyExtension(base.PDAExtension):
|
||||||
|
"""
|
||||||
|
Extends a :class:`~royalnet.engineer.pda.implementations.base.PDAImplementation` by adding an asyncronous
|
||||||
|
:mod:`sqlalchemy` session to conversations through the ``_asession`` kwarg.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, engine: sea.AsyncEngine, session_kwargs: t.Kwargs = None, kwarg_name: str = "_asession"):
|
||||||
|
super().__init__()
|
||||||
|
self.engine: sea.AsyncEngine = engine
|
||||||
|
"""
|
||||||
|
The :class:`sqlalchemy.engine.Engine` to use.
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.AsyncSession: sqlalchemy.orm.sessionmaker = sqlalchemy.orm.sessionmaker(
|
||||||
|
bind=self.engine,
|
||||||
|
expire_on_commit=False,
|
||||||
|
class_=sea.AsyncSession,
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
The :class:`sqlalchemy.orm.sessionmaker` to use when creating new sessions.
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.session_kwargs: t.Kwargs = {"future": True, **(session_kwargs or {})}
|
||||||
|
"""
|
||||||
|
Additional kwargs to be passed to the :class:`sqlalchemy.orm.sessionmaker` when instantiating a new Session.
|
||||||
|
|
||||||
|
Defaults to ``{"future": True}`` .
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.kwarg_name: str = kwarg_name
|
||||||
|
"""
|
||||||
|
The name of the kwarg to add.
|
||||||
|
|
||||||
|
Defaults to ``"_asession"``.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<{self.__class__.__qualname__} with engine {self.engine}>"
|
||||||
|
|
||||||
|
@contextlib.asynccontextmanager
|
||||||
|
async def kwargs(self, kwargs: t.Kwargs) -> t.Kwargs:
|
||||||
|
log.debug(f"{self!r}: Creating session...")
|
||||||
|
async with self.AsyncSession(**self.session_kwargs) as session:
|
||||||
|
log.debug(f"{self!r}: Yielding kwargs...")
|
||||||
|
yield {
|
||||||
|
**kwargs,
|
||||||
|
self.kwarg_name: session,
|
||||||
|
}
|
||||||
|
log.debug(f"{self!r}: Closing session...")
|
||||||
|
log.debug(f"{self!r}: Session closed!")
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
"SQLAlchemyExtension",
|
"SQLAlchemyExtension",
|
||||||
|
"AsyncSQLAlchemyExtension",
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue