-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#22: feat: add clients for MongoDB database and collection
- Loading branch information
1 parent
fe5065d
commit d486fb6
Showing
3 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from motor.core import AgnosticCollection, AgnosticDatabase | ||
from pymongo import results | ||
|
||
|
||
class MongoDatabaseClient: | ||
"""Клиент для работы с базой данной из MongoDB.""" | ||
|
||
collection_cache: dict[tuple, "MongoCollectionClient"] = {} | ||
|
||
def __init__(self, db_client: AgnosticDatabase) -> None: | ||
assert isinstance(db_client, AgnosticDatabase) | ||
self._client = db_client | ||
|
||
def __getattr__(self, name: str) -> "MongoCollectionClient": | ||
if name.startswith("_"): | ||
raise AttributeError(f"Cannot access private collection <{name}>") | ||
return self[name] | ||
|
||
def __getitem__(self, name: str) -> "MongoCollectionClient": | ||
mongo_client = self._client.__getattr__(name) | ||
return self._get_collection_client(name, mongo_client) | ||
|
||
def _get_collection_client(self, name: str, mongo_client: AgnosticCollection) -> "MongoCollectionClient": | ||
cache_key = ("MongoCollectionClient", name, self.__module__) | ||
cached_client = self.collection_cache.get(cache_key) | ||
if cached_client: | ||
return cached_client | ||
new_client = MongoCollectionClient(collection_client=mongo_client) | ||
return new_client | ||
|
||
|
||
class MongoCollectionClient: | ||
"""Клиент для работы с коллекций документов MongoDB.""" | ||
|
||
def __init__(self, collection_client: AgnosticCollection) -> None: | ||
assert isinstance(collection_client, AgnosticCollection) | ||
self._client = collection_client | ||
|
||
async def insert_one(self, document: dict) -> results.InsertOneResult: | ||
"""Добавление одного документа в коллекцию.""" | ||
result = await self._client.insert_one(document) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters