didier/database/engine.py

28 lines
760 B
Python
Raw Normal View History

from urllib.parse import quote_plus
from sqlalchemy.engine import URL
2022-07-11 20:23:38 +00:00
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
import settings
encoded_postgres_password = quote_plus(settings.POSTGRES_PASS)
2022-07-19 16:49:22 +00:00
2022-07-25 18:33:20 +00:00
# PostgreSQL engine
postgres_engine = create_async_engine(
2022-07-19 16:57:24 +00:00
URL.create(
drivername="postgresql+asyncpg",
2022-07-25 17:07:57 +00:00
username=settings.POSTGRES_USER,
password=encoded_postgres_password,
2022-07-25 17:07:57 +00:00
host=settings.POSTGRES_HOST,
port=settings.POSTGRES_PORT,
database=settings.POSTGRES_DB,
2022-07-19 16:57:24 +00:00
),
2022-06-28 22:14:44 +00:00
pool_pre_ping=True,
2022-06-30 13:20:54 +00:00
future=True,
2022-06-28 22:14:44 +00:00
)
2022-07-25 18:33:20 +00:00
DBSession = sessionmaker(
autocommit=False, autoflush=False, bind=postgres_engine, class_=AsyncSession, expire_on_commit=False
)