← ClaudeAtlas

sqlalchemy-2-0listed

Modern async ORM with type-safe models and efficient queries
aiskillstore/marketplace · ★ 329 · API & Backend · score 82
Install: claude install-skill aiskillstore/marketplace
# SQLAlchemy 2.0+ Skill ## Quick Start ### Basic Setup ```python from sqlalchemy.ext.asyncio import AsyncAttrs, async_sessionmaker, create_async_engine, AsyncSession from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column import asyncio # Base class for models class Base(AsyncAttrs, DeclarativeBase): pass # Async engine engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/db") # Session factory async_session = async_sessionmaker(engine, expire_on_commit=False) # Example model class User(Base): __tablename__ = "users" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column(String(50)) email: Mapped[str] = mapped_column(String(100)) ``` ### Basic CRUD Operations ```python async def create_user(name: str, email: str) -> User: async with async_session() as session: async with session.begin(): user = User(name=name, email=email) session.add(user) await session.flush() # Get the ID return user async def get_user(user_id: int) -> User | None: async with async_session() as session: result = await session.execute(select(User).where(User.id == user_id)) return result.scalar_one_or_none() async def update_user_email(user_id: int, new_email: str) -> bool: async with async_session() as session: result = await session.execute( update(User).where(User.id == user_id).values(email=new_email) )