PaiGram/core/dependence/database.py

52 lines
1.5 KiB
Python
Raw Normal View History

import contextlib
from typing import Optional
from sqlalchemy.engine import URL
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import sessionmaker
from typing_extensions import Self
from core.base_service import BaseService
from core.config import ApplicationConfig
from core.sqlmodel.session import AsyncSession
2023-03-25 03:17:38 +00:00
__all__ = ("Database",)
2023-03-25 03:17:38 +00:00
class Database(BaseService.Dependence):
@classmethod
def from_config(cls, config: ApplicationConfig) -> Self:
2023-03-25 03:17:38 +00:00
return cls(**config.database.dict())
def __init__(
self,
2023-03-25 03:17:38 +00:00
driver_name: str,
host: Optional[str] = None,
port: Optional[int] = None,
username: Optional[str] = None,
password: Optional[str] = None,
database: Optional[str] = None,
):
2023-03-25 03:17:38 +00:00
self.database = database # skipcq: PTC-W0052
self.password = password
self.username = username
self.port = port
self.host = host
self.url = URL.create(
2023-03-25 03:17:38 +00:00
driver_name,
username=self.username,
password=self.password,
host=self.host,
port=self.port,
database=self.database,
)
self.engine = create_async_engine(self.url)
self.Session = sessionmaker(bind=self.engine, class_=AsyncSession)
@contextlib.asynccontextmanager
async def session(self) -> AsyncSession:
yield self.Session()
async def shutdown(self):
self.Session.close_all()