mirror of
http://88.130.71.182:3000/BlitTech/badoHair_be.git
synced 2026-06-12 23:23:22 +00:00
Initial Commit
This commit is contained in:
42
app/database.py
Normal file
42
app/database.py
Normal file
@@ -0,0 +1,42 @@
|
||||
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
|
||||
Reference in New Issue
Block a user