[WIP] Database rework

This commit is contained in:
Profitroll 2023-11-05 13:20:01 +00:00
parent 7a0025c0f9
commit 116a18eba1
Signed by: profitroll
GPG Key ID: C4935728BCCF3EBC
11 changed files with 46 additions and 12 deletions

View File

@ -6,7 +6,7 @@ from typing import Any, Dict, List, Union
from bson import ObjectId
from classes.importer.abstract import Importer
from modules.database import col_entries
from modules.database_api import col_entries
class ImporterCSV(Importer):

View File

@ -5,7 +5,7 @@ from bson import ObjectId
from ujson import loads
from classes.importer.abstract import Importer
from modules.database import col_entries
from modules.database_api import col_entries
class ImporterJSON(Importer):

View File

@ -6,7 +6,7 @@ from pytz import timezone as pytz_timezone
from pytz.tzinfo import BaseTzInfo, DstTzInfo
from classes.point import Point
from modules.database import col_locations
from modules.database_api import col_locations
@dataclass

View File

@ -7,7 +7,7 @@ from pyrogram.types import User
from classes.location import Location
from classes.pyrouser import PyroUser
from modules.database import col_locations
from modules.database_api import col_locations
from modules.reminder import remind

View File

@ -14,7 +14,14 @@
"password": null,
"host": "127.0.0.1",
"port": 27017,
"name": "garbagebot"
"name": "garbage_bot"
},
"database_api": {
"user": null,
"password": null,
"host": "127.0.0.1",
"port": 27017,
"name": "garbage_reminder"
},
"search": {
"radius": 0.1

View File

@ -1,4 +1,4 @@
"""Module that provides all database collections"""
"""Module that provides bot's database collections."""
from typing import Any, Mapping
@ -24,5 +24,3 @@ db_client = AsyncClient(con_string)
db: AsyncDatabase = db_client.get_database(name=db_config["name"])
col_users: AsyncCollection = db.get_collection("users")
col_entries: AsyncCollection = db.get_collection("entries")
col_locations: AsyncCollection = db.get_collection("locations")

29
modules/database_api.py Normal file
View File

@ -0,0 +1,29 @@
"""Module that provides API database collections.
It's possible to use REST API client instead, but
using MongoDB directly is MUCH faster this way."""
from typing import Any, Mapping
from async_pymongo import AsyncClient, AsyncCollection, AsyncDatabase
from libbot.sync import config_get
db_config: Mapping[str, Any] = config_get("database_api")
if db_config["user"] is not None and db_config["password"] is not None:
con_string = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
db_config["user"],
db_config["password"],
db_config["host"],
db_config["port"],
db_config["name"],
)
else:
con_string = "mongodb://{0}:{1}/{2}".format(
db_config["host"], db_config["port"], db_config["name"]
)
db_client = AsyncClient(con_string)
db: AsyncDatabase = db_client.get_database(name=db_config["name"])
col_entries: AsyncCollection = db.get_collection("entries")
col_locations: AsyncCollection = db.get_collection("locations")

View File

@ -7,7 +7,7 @@ from libbot.pyrogram.classes import PyroClient
from classes.enums import GarbageType
from classes.location import Location
from classes.pyrouser import PyroUser
from modules.database import col_entries, col_users
from modules.database_api import col_entries, col_users
from modules.utils import from_utc
logger = logging.getLogger(__name__)

View File

@ -6,7 +6,7 @@ from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove
from classes.location import Location
from classes.pyroclient import PyroClient
from modules.database import col_locations
from modules.database_api import col_locations
async def search_name(app: PyroClient, message: Message) -> Union[Location, None]:

View File

@ -6,7 +6,7 @@ from pyrogram.types import Message, ReplyKeyboardRemove
from classes.location import Location
from classes.pyroclient import PyroClient
from modules.database import col_locations
from modules.database_api import col_locations
from modules.search_name import search_name

View File

@ -6,7 +6,7 @@ from pyrogram.types import Message
from classes.garbage_entry import GarbageEntry
from classes.pyroclient import PyroClient
from modules import custom_filters
from modules.database import col_entries
from modules.database_api import col_entries
@PyroClient.on_message(