Skip to main content

SQLAlchemy-based storage for aiogram FSM

Project description

SQLAlchemyStorage for aiogram FSM

Overview

SQLAlchemyStorage is a storage module for aiogram's finite state machine (FSM) using SQLAlchemy as the backend. It provides an efficient and flexible way to persist FSM state and data using an asynchronous database session.

Features

  • Asynchronous support with AsyncSession
  • Customizable table name for storing FSM data
  • Pluggable key-building strategy
  • JSON serialization customization

Installation

Use pip to install in your environment:

pip install aiogram-sqlalchemy-storage

Usage

Import and Setup

# db.py
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from sqlalchemy_storage import SQLAlchemyStorage

# Create an async engine
engine = create_async_engine("sqlite+aiosqlite:///database.db")
SessionLocal = async_sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False)

# Initialize the storage
storage = SQLAlchemyStorage(session=SessionLocal)

Parameters

The SQLAlchemyStorage constructor accepts the following parameters:

def __init__(
    self,
    session: sessionmaker[AsyncSession],  # Async database session
    base=None,                            # Optional declarative base
    table_name: Optional[str] = 'aiogram_fsm_data',  # Custom table name
    key_builder: Optional[KeyBuilder] = None,       # Custom key-building strategy
    json_dumps: _JsonDumps = json.dumps,            # Custom JSON serialization
    json_loads: _JsonLoads = json.loads,            # Custom JSON deserialization
):

Example Integration with aiogram

# bot.py
from db import storage
from aiogram import Bot, Dispatcher

bot = Bot(token="YOUR_BOT_TOKEN")
dp = Dispatcher(storage=storage)

Database Schema

By default, SQLAlchemyStorage creates a table named aiogram_fsm_data to store FSM-related data. You can customize this by passing a different table name during initialization.

Custom Key Builder

If you need a custom key-building strategy, pass an instance of KeyBuilder to the key_builder parameter.

Custom JSON Serialization

You can override the default JSON serialization and deserialization methods using the json_dumps and json_loads parameters.

License

MIT License

Contributions

Contributions are welcome! Feel free to submit issues or pull requests on the repository.


This module provides an efficient way to manage FSM data storage using SQLAlchemy with aiogram, ensuring scalability and flexibility in bot development.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiogram_sqlalchemy_storage-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiogram_sqlalchemy_storage-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_sqlalchemy_storage-0.1.0.tar.gz.

File metadata

File hashes

Hashes for aiogram_sqlalchemy_storage-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4948c0ae767af7df41c3c51bed84f540e4e4426f9bf8739672bfaade9c72c728
MD5 baac01ff213e32ab096cb9fe68764ec5
BLAKE2b-256 638c8aff490a032329ac240945fa55bf34a8c78814e278ab52a00715dc28625a

See more details on using hashes here.

File details

Details for the file aiogram_sqlalchemy_storage-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aiogram_sqlalchemy_storage-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a070be1a93f00933c87311d9a01d16b3221e85bcd2aa9b5978c08b3f2e2f1e68
MD5 9538e9ccd499d20719a83e874b7a040b
BLAKE2b-256 b3909d015fc3e737c2495e7011ac5709d64f5c5431eba0445a2398b7099626d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page