async-sqlalchemy-patterns
Async SQLAlchemy 2.0+ database patterns for FastAPI including session management, connection pooling, Alembic migrations, relationship loading strategies, and query optimization. Use when implementing database models, configuring async sessions, setting up migrations, optimizing queries, managing relationships, or when user mentions SQLAlchemy, async database, ORM, Alembic, database performance, or connection pooling.
What this skill does
# Async SQLAlchemy Patterns
**Purpose:** Implement production-ready async SQLAlchemy 2.0+ patterns in FastAPI with proper session management, migrations, and performance optimization.
**Activation Triggers:**
- Database model implementation
- Async session configuration
- Alembic migration setup
- Query performance optimization
- Relationship loading issues
- Connection pool configuration
- Transaction management
- Database schema migrations
**Key Resources:**
- `scripts/setup-alembic.sh` - Initialize Alembic with async support
- `scripts/generate-migration.sh` - Create migrations from model changes
- `templates/base_model.py` - Base model with common patterns
- `templates/session_manager.py` - Async session factory and dependency
- `templates/alembic.ini` - Alembic configuration for async
- `examples/user_model.py` - Complete model with relationships
- `examples/async_context_examples.py` - Session usage patterns
## Core Patterns
### 1. Async Engine and Session Setup
**Database Configuration:**
```python
# app/core/database.py
from sqlalchemy.ext.asyncio import (
AsyncSession,
create_async_engine,
async_sessionmaker
)
from sqlalchemy.orm import declarative_base
from app.core.config import settings
# Create async engine
engine = create_async_engine(
settings.DATABASE_URL,
echo=settings.DEBUG,
pool_pre_ping=True, # Verify connections before using
pool_size=5, # Base number of connections
max_overflow=10, # Additional connections when pool is full
pool_recycle=3600, # Recycle connections after 1 hour
pool_timeout=30, # Wait 30s for available connection
)
# Create async session factory
AsyncSessionLocal = async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False, # Keep objects usable after commit
autoflush=False, # Manual flush control
autocommit=False, # Explicit commits only
)
Base = declarative_base()
```
**Dependency Injection Pattern:**
```python
# app/core/deps.py
from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.database import AsyncSessionLocal
async def get_db() -> AsyncGenerator[AsyncSession, None]:
"""
FastAPI dependency for database sessions.
Automatically handles cleanup.
"""
async with AsyncSessionLocal() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()
```
### 2. Base Model Pattern
Use template from `templates/base_model.py` with:
- UUID primary keys by default
- Automatic created_at/updated_at timestamps
- Soft delete support
- Common query methods
**Essential Mixins:**
```python
from datetime import datetime
from sqlalchemy import DateTime, Boolean, func
from sqlalchemy.orm import Mapped, mapped_column
class TimestampMixin:
"""Auto-managed timestamps"""
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
nullable=False
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
onupdate=func.now(),
nullable=False
)
class SoftDeleteMixin:
"""Soft delete support"""
is_deleted: Mapped[bool] = mapped_column(
Boolean,
default=False,
nullable=False
)
deleted_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True),
nullable=True
)
```
### 3. Relationship Loading Strategies
**Lazy Loading (Default - N+1 Problem Risk):**
```python
# Bad: Causes N+1 queries
users = await session.execute(select(User))
for user in users.scalars():
print(user.posts) # Separate query per user!
```
**Eager Loading (Recommended):**
```python
from sqlalchemy.orm import selectinload, joinedload
# selectinload: Separate query, good for one-to-many
stmt = select(User).options(selectinload(User.posts))
result = await session.execute(stmt)
users = result.scalars().all()
# joinedload: SQL JOIN, good for many-to-one
stmt = select(Post).options(joinedload(Post.author))
result = await session.execute(stmt)
posts = result.scalars().unique().all() # unique() required with joins!
# subqueryload: Subquery loading
stmt = select(User).options(subqueryload(User.posts))
```
**Relationship Configuration:**
```python
from sqlalchemy.orm import relationship
class User(Base):
__tablename__ = "users"
# One-to-many with cascade
posts: Mapped[list["Post"]] = relationship(
back_populates="author",
cascade="all, delete-orphan",
lazy="selectin" # Default eager loading
)
# Many-to-many
roles: Mapped[list["Role"]] = relationship(
secondary="user_roles",
back_populates="users",
lazy="selectin"
)
```
### 4. Query Patterns
**Basic CRUD:**
```python
from sqlalchemy import select, update, delete
from sqlalchemy.ext.asyncio import AsyncSession
# Create
async def create_user(session: AsyncSession, user_data: dict):
user = User(**user_data)
session.add(user)
await session.commit()
await session.refresh(user)
return user
# Read with filters
async def get_users(session: AsyncSession, skip: int = 0, limit: int = 100):
stmt = (
select(User)
.where(User.is_deleted == False)
.offset(skip)
.limit(limit)
.order_by(User.created_at.desc())
)
result = await session.execute(stmt)
return result.scalars().all()
# Update
async def update_user(session: AsyncSession, user_id: int, updates: dict):
stmt = (
update(User)
.where(User.id == user_id)
.values(**updates)
.returning(User)
)
result = await session.execute(stmt)
await session.commit()
return result.scalar_one()
# Delete
async def delete_user(session: AsyncSession, user_id: int):
stmt = delete(User).where(User.id == user_id)
await session.execute(stmt)
await session.commit()
```
**Complex Queries:**
```python
from sqlalchemy import func, and_, or_
# Aggregations
stmt = (
select(User.id, func.count(Post.id))
.join(Post)
.group_by(User.id)
.having(func.count(Post.id) > 5)
)
# Subqueries
subq = (
select(func.avg(Post.views))
.where(Post.user_id == User.id)
.scalar_subquery()
)
stmt = select(User).where(User.id.in_(
select(Post.user_id).where(Post.views > subq)
))
# Window functions
from sqlalchemy import over
stmt = select(
Post,
func.row_number().over(
partition_by=Post.user_id,
order_by=Post.created_at.desc()
).label('row_num')
).where(over.row_num <= 10)
```
### 5. Transaction Management
**Nested Transactions:**
```python
async def transfer_funds(
session: AsyncSession,
from_account: int,
to_account: int,
amount: float
):
async with session.begin_nested(): # Savepoint
# Debit
stmt = (
update(Account)
.where(Account.id == from_account)
.where(Account.balance >= amount)
.values(balance=Account.balance - amount)
)
result = await session.execute(stmt)
if result.rowcount == 0:
raise ValueError("Insufficient funds")
# Credit
stmt = (
update(Account)
.where(Account.id == to_account)
.values(balance=Account.balance + amount)
)
await session.execute(stmt)
await session.commit()
```
**Manual Transaction Control:**
```python
async def batch_operation(session: AsyncSession, items: list):
try:
for item in items:
session.add(item)
if len(session.new) >= 100:
await session.flush() # Flush but don't commit
await session.commit()
except Exception as e:
await session.rollback()
raise
```
### 6. Alembic Migration SetuRelated in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.