migrated mongo storage from using deprecated motor to PyMongo

This commit is contained in:
kievzenit 2025-07-09 16:04:20 +03:00
parent 7a517f1eba
commit 1c0207e1d1

View file

@ -1,6 +1,6 @@
from typing import Any, Dict, Mapping, Optional, cast from typing import Any, Dict, Mapping, Optional, cast
from motor.motor_asyncio import AsyncIOMotorClient from pymongo import AsyncMongoClient
from aiogram.exceptions import DataNotDictLikeError from aiogram.exceptions import DataNotDictLikeError
from aiogram.fsm.state import State from aiogram.fsm.state import State
@ -15,18 +15,18 @@ from aiogram.fsm.storage.base import (
class MongoStorage(BaseStorage): class MongoStorage(BaseStorage):
""" """
MongoDB storage required :code:`motor` package installed (:code:`pip install motor`) MongoDB storage required :code:`PyMongo` package installed (:code:`pip install PyMongo`).
""" """
def __init__( def __init__(
self, self,
client: AsyncIOMotorClient, client: AsyncMongoClient,
key_builder: Optional[KeyBuilder] = None, key_builder: Optional[KeyBuilder] = None,
db_name: str = "aiogram_fsm", db_name: str = "aiogram_fsm",
collection_name: str = "states_and_data", collection_name: str = "states_and_data",
) -> None: ) -> None:
""" """
:param client: Instance of AsyncIOMotorClient :param client: Instance of AsyncMongoClient
:param key_builder: builder that helps to convert contextual key to string :param key_builder: builder that helps to convert contextual key to string
:param db_name: name of the MongoDB database for FSM :param db_name: name of the MongoDB database for FSM
:param collection_name: name of the collection for storing FSM states and data :param collection_name: name of the collection for storing FSM states and data
@ -46,13 +46,13 @@ class MongoStorage(BaseStorage):
Create an instance of :class:`MongoStorage` with specifying the connection string Create an instance of :class:`MongoStorage` with specifying the connection string
:param url: for example :code:`mongodb://user:password@host:port` :param url: for example :code:`mongodb://user:password@host:port`
:param connection_kwargs: see :code:`motor` docs :param connection_kwargs: see :code:`PyMongo` docs
:param kwargs: arguments to be passed to :class:`MongoStorage` :param kwargs: arguments to be passed to :class:`MongoStorage`
:return: an instance of :class:`MongoStorage` :return: an instance of :class:`MongoStorage`
""" """
if connection_kwargs is None: if connection_kwargs is None:
connection_kwargs = {} connection_kwargs = {}
client = AsyncIOMotorClient(url, **connection_kwargs) client = AsyncMongoClient(url, **connection_kwargs)
return cls(client=client, **kwargs) return cls(client=client, **kwargs)
async def close(self) -> None: async def close(self) -> None: