2022-06-10 01:48:02 +02:00
|
|
|
from urllib.parse import quote_plus
|
|
|
|
|
2022-07-25 20:33:20 +02:00
|
|
|
import motor.motor_asyncio
|
2022-06-10 01:48:02 +02:00
|
|
|
from sqlalchemy.engine import URL
|
2022-07-11 22:23:38 +02:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
2022-06-10 01:48:02 +02:00
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
|
|
|
|
import settings
|
|
|
|
|
2022-07-25 21:08:06 +02:00
|
|
|
encoded_postgres_password = quote_plus(settings.POSTGRES_PASS)
|
2022-07-19 18:49:22 +02:00
|
|
|
|
2022-07-25 20:33:20 +02:00
|
|
|
# PostgreSQL engine
|
|
|
|
postgres_engine = create_async_engine(
|
2022-07-19 18:57:24 +02:00
|
|
|
URL.create(
|
|
|
|
drivername="postgresql+asyncpg",
|
2022-07-25 19:07:57 +02:00
|
|
|
username=settings.POSTGRES_USER,
|
2022-07-25 21:08:06 +02:00
|
|
|
password=encoded_postgres_password,
|
2022-07-25 19:07:57 +02:00
|
|
|
host=settings.POSTGRES_HOST,
|
|
|
|
port=settings.POSTGRES_PORT,
|
|
|
|
database=settings.POSTGRES_DB,
|
2022-07-19 18:57:24 +02:00
|
|
|
),
|
2022-06-29 00:14:44 +02:00
|
|
|
pool_pre_ping=True,
|
2022-06-30 15:20:54 +02:00
|
|
|
future=True,
|
2022-06-29 00:14:44 +02:00
|
|
|
)
|
2022-06-10 01:48:02 +02:00
|
|
|
|
2022-07-25 20:33:20 +02:00
|
|
|
DBSession = sessionmaker(
|
|
|
|
autocommit=False, autoflush=False, bind=postgres_engine, class_=AsyncSession, expire_on_commit=False
|
|
|
|
)
|
|
|
|
|
|
|
|
# MongoDB client
|
2022-07-27 22:02:59 +02:00
|
|
|
if not settings.TESTING:
|
|
|
|
encoded_mongo_username = quote_plus(settings.MONGO_USER)
|
|
|
|
encoded_mongo_password = quote_plus(settings.MONGO_PASS)
|
|
|
|
mongo_url = (
|
|
|
|
f"mongodb://{encoded_mongo_username}:{encoded_mongo_password}@{settings.MONGO_HOST}:{settings.MONGO_PORT}/"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# Require no authentication when testing
|
|
|
|
mongo_url = f"mongodb://{settings.MONGO_HOST}:{settings.MONGO_PORT}/"
|
|
|
|
|
2022-07-25 21:08:06 +02:00
|
|
|
mongo_client = motor.motor_asyncio.AsyncIOMotorClient(mongo_url)
|