Bot commit: Добавлен файл db.py
This commit is contained in:
parent
006ffee050
commit
74fc74519e
72
db.py
Normal file
72
db.py
Normal file
@ -0,0 +1,72 @@
|
||||
import os
|
||||
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
|
||||
import redis.asyncio as redis
|
||||
import asyncio
|
||||
from config import PROD, MONGO_PASSWORD, MONGODB_HOST, REDIS_HOST
|
||||
|
||||
# Create a new event loop for asyncio
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
|
||||
# ========================= RETRIEVE CONFIG =========================
|
||||
if not all([MONGO_PASSWORD, MONGODB_HOST, REDIS_HOST]):
|
||||
raise EnvironmentError("Missing required environment variables for MongoDB/Redis")
|
||||
|
||||
# ========================= DATABASE URI =========================
|
||||
MONGO_PORT = 27017 if PROD else 30002
|
||||
mongo_uri = f"mongodb://admin:{MONGO_PASSWORD}@{MONGODB_HOST}:{MONGO_PORT}/"
|
||||
|
||||
# ========================= MONGO CLIENT =========================
|
||||
mongo_client = AsyncIOMotorClient(mongo_uri)
|
||||
|
||||
# Function to get database collections lazily
|
||||
def get_collection(db_name, collection_name):
|
||||
return mongo_client[db_name][collection_name]
|
||||
|
||||
# Access collections when needed
|
||||
competitions_col = get_collection("competitions", "competitions")
|
||||
competition_request_col = get_collection("competitions", "request")
|
||||
users_collection = get_collection("auth", "users")
|
||||
users_token_collection = get_collection("auth", "token")
|
||||
notify_collection = get_collection("notify", "notify")
|
||||
posts_collection = get_collection("posts", "posts")
|
||||
news_collection = get_collection("news", "news")
|
||||
social_collection = get_collection("social", "social")
|
||||
vk_photos_collection = get_collection("vk", "photos")
|
||||
invitations_collection = get_collection("invitations", "invitations")
|
||||
delivery_collection = get_collection("delivery", "delivery")
|
||||
exhibition_collection = get_collection("exhibition", "exhibition")
|
||||
|
||||
# ========================= GRIDFS =========================
|
||||
bucket_competitions = AsyncIOMotorGridFSBucket(mongo_client["competitions"])
|
||||
gridfs_map = {
|
||||
"competition": bucket_competitions,
|
||||
"competitions": bucket_competitions,
|
||||
"users": AsyncIOMotorGridFSBucket(mongo_client["auth"]),
|
||||
"posts": AsyncIOMotorGridFSBucket(mongo_client["posts"]),
|
||||
"news": AsyncIOMotorGridFSBucket(mongo_client["news"]),
|
||||
"social": AsyncIOMotorGridFSBucket(mongo_client["social"]),
|
||||
"exhibition": AsyncIOMotorGridFSBucket(mongo_client["exhibition"]),
|
||||
}
|
||||
|
||||
|
||||
# ========================= REDIS CLIENT =========================
|
||||
REDIS_PORT = 6379 if PROD else 30003
|
||||
|
||||
redis_client = redis.Redis(
|
||||
host=REDIS_HOST,
|
||||
port=REDIS_PORT,
|
||||
db=0,
|
||||
decode_responses=True
|
||||
)
|
||||
|
||||
# Exporting collections and clients
|
||||
__all__ = [
|
||||
"competitions_col", "competition_request_col",
|
||||
"users_collection", "users_token_collection",
|
||||
"notify_collection", "social_collection",
|
||||
"posts_collection", "news_collection",
|
||||
"gridfs_map", "redis_client",
|
||||
"mongo_client", "invitations_collection",
|
||||
"delivery_collection", "exhibition_collection",
|
||||
]
|
||||
Loading…
Reference in New Issue
Block a user