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.orm import declarative_base
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)
Base = declarative_base()

# Initialize the storage
storage = SQLAlchemyStorage(SessionLocal, Base)

Parameters

The SQLAlchemyStorage constructor accepts the following parameters:

def __init__(
    self,
    session: sessionmaker[AsyncSession],  # Async database session
    base: Any,                            # 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.1.post2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file aiogram_sqlalchemy_storage-0.1.1.post2.tar.gz.

File metadata

File hashes

Hashes for aiogram_sqlalchemy_storage-0.1.1.post2.tar.gz
Algorithm Hash digest
SHA256 09296fed8aaf499db5ff9ea31d02a6c94e5467e2f8a7c9eebe5f49d0f79a5de9
MD5 7894e05821dd2f20d870835fc07db40a
BLAKE2b-256 df7a2e9fc6db4db3609821337d10bc33c12f002437f563b7f0d1264395e687a6

See more details on using hashes here.

File details

Details for the file aiogram_sqlalchemy_storage-0.1.1.post2-py3-none-any.whl.

File metadata

File hashes

Hashes for aiogram_sqlalchemy_storage-0.1.1.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 22a67c1dddb1c4e41672c6038a2b2e5e159d024eb344b16205dbfbcef9a9050d
MD5 fb130b473f4b2bf64572dd4afb77f58b
BLAKE2b-256 eea7561b80b7d3e15b0f61288b15a30852f24f0e726f0e272d189bab7ecec9cf

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