27 lines
564 B
Python
27 lines
564 B
Python
import asyncpg
|
|
|
|
from app.core.config import get_settings
|
|
|
|
pool: asyncpg.Pool | None = None
|
|
|
|
|
|
async def init_pool() -> asyncpg.Pool:
|
|
global pool
|
|
if pool is None:
|
|
settings = get_settings()
|
|
pool = await asyncpg.create_pool(settings.database_dsn, min_size=2, max_size=20)
|
|
return pool
|
|
|
|
|
|
async def close_pool() -> None:
|
|
global pool
|
|
if pool is not None:
|
|
await pool.close()
|
|
pool = None
|
|
|
|
|
|
def get_pool() -> asyncpg.Pool:
|
|
if pool is None:
|
|
raise RuntimeError("Database pool is not initialized")
|
|
return pool
|