mirror of
http://88.130.71.182:3000/BlitTech/badoHair_be.git
synced 2026-06-13 08:34:29 +00:00
43 lines
1013 B
Python
43 lines
1013 B
Python
import asyncpg
|
|
from app.config import get_settings
|
|
|
|
_pool: asyncpg.Pool | None = None
|
|
|
|
|
|
async def get_pool() -> asyncpg.Pool:
|
|
global _pool
|
|
if _pool is None:
|
|
settings = get_settings()
|
|
_pool = await asyncpg.create_pool(
|
|
settings.DATABASE_URL,
|
|
min_size=2,
|
|
max_size=10,
|
|
command_timeout=60,
|
|
init=_init_connection,
|
|
)
|
|
return _pool
|
|
|
|
|
|
async def _init_connection(conn: asyncpg.Connection):
|
|
await conn.set_type_codec(
|
|
"jsonb",
|
|
encoder=lambda v: __import__("json").dumps(v),
|
|
decoder=lambda v: __import__("json").loads(v),
|
|
schema="pg_catalog",
|
|
format="text",
|
|
)
|
|
await conn.set_type_codec(
|
|
"json",
|
|
encoder=lambda v: __import__("json").dumps(v),
|
|
decoder=lambda v: __import__("json").loads(v),
|
|
schema="pg_catalog",
|
|
format="text",
|
|
)
|
|
|
|
|
|
async def close_pool():
|
|
global _pool
|
|
if _pool:
|
|
await _pool.close()
|
|
_pool = None
|