SQLAlchemyStorage
is a storage backend for aiogram
's finite state machine (FSM) using SQLAlchemy. It provides an efficient and flexible way to persist FSM state and data using an asynchronous database session.
Use pip
to install in your environment:
pip install aiogram-sqlalchemy-storage
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from sqlalchemy.orm import declarative_base
from sqlalchemy_storage import SQLAlchemyStorage
from aiogram import Bot, Dispatcher
# Setup database
engine = create_async_engine("sqlite+aiosqlite:///database.db")
SessionLocal = async_sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False)
Base = declarative_base()
# Initialize storage
storage = SQLAlchemyStorage(sessionmaker=SessionLocal, metadata=Base.metadata)
# Setup bot
bot = Bot(token="YOUR_BOT_TOKEN")
dp = Dispatcher(storage=storage)
- Asynchronous support with
AsyncSession
- Customizable table name for storing FSM data
- Pluggable key-building strategy
- JSON serialization customization
For detailed documentation, configuration options, and advanced usage, see:
MIT License
Contributions are welcome! Feel free to submit issues or pull requests on the repository.