From 8bc6972ca2b9bc836e55aae8785218ce2767468f Mon Sep 17 00:00:00 2001 From: Artem Litvinov Date: Sat, 23 Sep 2023 21:30:23 +0100 Subject: [PATCH] feat: extened settings --- .../lib/app/split_settings/postgres.py | 14 ++++++++++++++ src/fastapi_app/lib/db/postgres.py | 15 ++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/fastapi_app/lib/app/split_settings/postgres.py b/src/fastapi_app/lib/app/split_settings/postgres.py index 1a33f64..b9373f3 100644 --- a/src/fastapi_app/lib/app/split_settings/postgres.py +++ b/src/fastapi_app/lib/app/split_settings/postgres.py @@ -14,6 +14,13 @@ class DBSettings(pydantic_settings.BaseSettings): user: str password: pydantic.SecretStr + pool_size: int + pool_pre_ping: bool + echo: bool + auto_commit: bool + auto_flush: bool + expire_on_commit: bool + @property def dsn(self) -> str: """Get database DSN.""" @@ -36,3 +43,10 @@ class PostgresSettings(DBSettings): password: pydantic.SecretStr = pydantic.Field( default=..., validation_alias=pydantic.AliasChoices("password", "postgres_password") ) + + pool_size: int = 50 + pool_pre_ping: bool = True + echo: bool = False + auto_commit: bool = False + auto_flush: bool = False + expire_on_commit: bool = False diff --git a/src/fastapi_app/lib/db/postgres.py b/src/fastapi_app/lib/db/postgres.py index 7986a33..2e7ab6c 100644 --- a/src/fastapi_app/lib/db/postgres.py +++ b/src/fastapi_app/lib/db/postgres.py @@ -9,10 +9,19 @@ class AsyncDB: """Async DB connection.""" def __init__(self, settings: app_settings.Settings): - self.database_dsn = settings.db.dsn - self.engine = sa_asyncio.create_async_engine(self.database_dsn, echo=settings.project.debug, future=True) + self.engine = sa_asyncio.create_async_engine( + url=settings.db.dsn, + pool_size=settings.db.pool_size, + pool_pre_ping=settings.db.pool_pre_ping, + echo=settings.db.echo, + future=True, + ) self.async_session = sa_asyncio.async_sessionmaker( - self.engine, class_=sa_asyncio.AsyncSession, expire_on_commit=False + bind=self.engine, + autocommit=settings.db.auto_commit, + autoflush=settings.db.auto_flush, + expire_on_commit=settings.db.expire_on_commit, + class_=sa_asyncio.AsyncSession, )