Migrate to async_pymongo

This commit is contained in:
Profitroll 2023-08-14 15:16:30 +02:00
parent faccb22bc0
commit d8f8e49f31
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
4 changed files with 17 additions and 25 deletions

View File

@ -18,17 +18,19 @@ class PyroClient(PyroClient):
* `PyroUser`: PyroUser object
"""
if (
col_users.find_one({"id": user.id if isinstance(user, User) else user})
await col_users.find_one(
{"id": user.id if isinstance(user, User) else user}
)
is None
):
col_users.insert_one(
await col_users.insert_one(
{
"id": user.id if isinstance(user, User) else user,
"locale": user.language_code if isinstance(user, User) else None,
}
)
db_record = col_users.find_one(
db_record = await col_users.find_one(
{"id": user.id if isinstance(user, User) else user}
)

View File

@ -1,15 +1,17 @@
from dataclasses import dataclass
from typing import Union
from attrs import define
from bson import ObjectId
from modules.database import col_users
@define
@dataclass
class PyroUser:
"""Dataclass of DB entry of a user"""
__slots__ = ("_id", "id", "locale")
_id: ObjectId
id: int
locale: Union[str, None]
@ -20,4 +22,4 @@ class PyroUser:
### Args:
* locale (`str`): New locale to be set
"""
col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})
await col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})

View File

@ -1,11 +1,9 @@
"""Module that provides all database columns"""
"""Module that provides all database collections"""
from pymongo import MongoClient
from ujson import loads
from async_pymongo import AsyncClient
from libbot.sync import config_get
with open("config.json", "r", encoding="utf-8") as f:
db_config = loads(f.read())["database"]
f.close()
db_config = config_get("database")
if db_config["user"] is not None and db_config["password"] is not None:
con_string = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
@ -20,13 +18,7 @@ else:
db_config["host"], db_config["port"], db_config["name"]
)
db_client = MongoClient(con_string)
db_client = AsyncClient(con_string)
db = db_client.get_database(name=db_config["name"])
collections = db.list_collection_names()
for collection in ["users"]:
if collection not in collections:
db.create_collection(collection)
col_users = db.get_collection("users")

View File

@ -1,13 +1,9 @@
aiofiles~=23.2.1
apscheduler~=3.10.1
attrs~=23.1.0
black~=23.7.0
apscheduler~=3.10.3
convopyro==0.5
pykeyboard==0.1.5
pymongo==4.4.1
pyrogram==2.0.106
tgcrypto==1.2.5
ujson==5.8.0
uvloop==0.17.0
--extra-index-url https://git.end-play.xyz/api/packages/profitroll/pypi/simple
async_pymongo==0.1.4
libbot[speed,pyrogram]==2.0.1