Skip to content

Latest commit

 

History

History
60 lines (40 loc) · 1.34 KB

File metadata and controls

60 lines (40 loc) · 1.34 KB

Quick Setup

This guide will help you quickly set up aiogram-sqlalchemy-storage for your project.

Installation

pip install aiogram-sqlalchemy-storage

Basic Usage

  1. Create SQLAlchemy engine and session:
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from sqlalchemy.orm import declarative_base
from sqlalchemy_storage import SQLAlchemyStorage

engine = create_async_engine("sqlite+aiosqlite:///database.db")
SessionLocal = async_sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False)
Base = declarative_base()
  1. Initialize storage for aiogram FSM:
storage = SQLAlchemyStorage(sessionmaker=SessionLocal, metadata=Base.metadata)
  1. Integrate with aiogram FSM:
from aiogram import Bot, Dispatcher
from aiogram.fsm.storage.base import BaseStorage

bot = Bot(token="YOUR_BOT_TOKEN")
dp = Dispatcher(storage=storage)
  1. Create tables in the database:

Before using the storage, make sure to create the required tables:

async def init_db():
	async with engine.begin() as conn:
		await conn.run_sync(Base.metadata.create_all)

# Run this once at startup
import asyncio
asyncio.run(init_db())

Now your FSM state and data will be stored in the database using SQLAlchemy.