database
Universal database operations skill for modern applications. Expert in SQLModel/SQLAlchemy patterns, async database operations, connection pooling, migrations, performance optimization, and multi-database support (PostgreSQL, MySQL, SQLite). Provides production-ready patterns for any database-driven application.
What this skill does
# Database Operations & Management
This skill provides comprehensive database patterns and best practices for 2025, focusing on async operations, performance optimization, and production-ready configurations that work across different database systems.
## When to Use This Skill
Use this skill when you need to:
- Design database schemas with SQLModel/SQLAlchemy
- Implement async database operations
- Optimize database performance
- Set up connection pooling
- Create and manage database migrations
- Handle complex relationships and queries
- Set up production database configurations
- Monitor and troubleshoot database issues
## Core Database Patterns
### 1. Async Connection Management
```python
# Database connection setup with connection pooling
import asyncio
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from sqlalchemy.pool import NullPool
from contextlib import asynccontextmanager
import os
# Environment-based configuration
DATABASE_CONFIG = {
"development": {
"url": "sqlite+aiosqlite:///./app.db",
"poolclass": NullPool,
"echo": True
},
"production": {
"url": os.getenv("DATABASE_URL", "postgresql+asyncpg://user:pass@localhost/db"),
"pool_size": 30,
"max_overflow": 40,
"pool_pre_ping": True,
"pool_recycle": 3600,
"echo": False
}
}
class DatabaseManager:
"""Universal database manager for async operations"""
def __init__(self, env: str = "development"):
config = DATABASE_CONFIG[env]
self.engine = create_async_engine(**config)
self.async_session = async_sessionmaker(
self.engine,
class_=AsyncSession,
expire_on_commit=False
)
@asynccontextmanager
async def get_session(self):
"""Get async database session with proper cleanup"""
async with self.async_session() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()
async def create_tables(self, metadata):
"""Create all tables from metadata"""
async with self.engine.begin() as conn:
await conn.run_sync(metadata.create_all)
async def close(self):
"""Close database connections"""
await self.engine.dispose()
# Singleton instance
db_manager = DatabaseManager(os.getenv("ENV", "development"))
```
### 2. Base Model with Best Practices
```python
# models/base.py
from sqlmodel import SQLModel, Field, DateTime, func
from typing import Optional
from datetime import datetime
from sqlalchemy import Column, DateTime as SQLDateTime, Index
class TimestampMixin(SQLModel):
"""Mixin for timestamp fields"""
created_at: datetime = Field(
default_factory=datetime.utcnow,
sa_column=Column(
SQLDateTime(timezone=True),
server_default=func.now(),
nullable=False
)
)
updated_at: Optional[datetime] = Field(
default=None,
sa_column=Column(
SQLDateTime(timezone=True),
onupdate=func.now(),
nullable=True
)
)
class SoftDeleteMixin(SQLModel):
"""Mixin for soft delete functionality"""
deleted_at: Optional[datetime] = None
is_deleted: bool = Field(default=False)
class BaseModel(SQLModel, TimestampMixin):
"""Base model with common fields and patterns"""
id: Optional[int] = Field(default=None, primary_key=True)
class Config:
# Enable Pydantic's strict mode
strict = True
# Validate defaults
validate_assignment = True
# Use enum values
use_enum_values = True
# Indexes for common queries
Index("idx_base_created_at", BaseModel.created_at)
```
### 3. Advanced Model Patterns
```python
# models/examples.py
from enum import Enum
from typing import Optional, List
from sqlalchemy import Column, String, Text, Boolean, Integer, Index, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship
from sqlmodel import SQLModel, Field, Session, select, update, delete
# Enums for type safety
class Status(str, Enum):
ACTIVE = "active"
INACTIVE = "inactive"
PENDING = "pending"
class Priority(str, Enum):
LOW = "low"
MEDIUM = "medium"
HIGH = "high"
CRITICAL = "critical"
class User(BaseModel, table=True):
"""User model with optimized fields and indexes"""
__tablename__ = "users"
email: str = Field(
sa_column=Column(String(255), unique=True, nullable=False, index=True)
)
username: str = Field(
sa_column=Column(String(100), unique=True, nullable=False, index=True)
)
full_name: Optional[str] = Field(default=None, max_length=200)
is_active: bool = Field(default=True, sa_column=Column(Boolean, default=True))
# Optimized text fields
bio: Optional[str] = Field(
default=None,
sa_column=Column(Text) # Use Text for longer content
)
# Relationships
tasks: List["Task"] = Relationship(back_populates="owner")
# Optimized queries
__table_args__ = (
Index("idx_user_active_email", "is_active", "email"),
Index("idx_user_created_at", "created_at"),
)
class Task(BaseModel, SoftDeleteMixin, table=True):
"""Task model with advanced patterns"""
__tablename__ = "tasks"
# Foreign key with index
owner_id: int = Field(
foreign_key="users.id",
sa_column=Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
)
# Optimized fields
title: str = Field(
sa_column=Column(String(200), nullable=False)
)
description: Optional[str] = Field(
default=None,
sa_column=Column(Text)
)
# Enum fields
status: Status = Field(default=Status.PENDING)
priority: Priority = Field(default=Priority.MEDIUM)
# JSON field for flexible metadata
metadata: dict = Field(
default_factory=dict,
sa_column=Column("metadata", JSON)
)
# Relationships
owner: User = Relationship(back_populates="tasks")
tags: List["TaskTag"] = Relationship(back_populates="task")
# Optimized queries
__table_args__ = (
Index("idx_task_owner_status", "owner_id", "status"),
Index("idx_task_priority_created", "priority", "created_at"),
Index("idx_task_deleted", "is_deleted"),
)
class Tag(BaseModel, table=True):
"""Tag model for many-to-many relationships"""
__tablename__ = "tags"
name: str = Field(
sa_column=Column(String(50), unique=True, nullable=False, index=True)
)
color: Optional[str] = Field(default=None, max_length=7) # Hex color code
class TaskTag(BaseModel, table=True):
"""Many-to-many relationship table"""
__tablename__ = "task_tags"
task_id: int = Field(foreign_key="tasks.id", primary_key=True)
tag_id: int = Field(foreign_key="tags.id", primary_key=True)
task: Task = Relationship()
tag: Tag = Relationship()
```
### 4. Repository Pattern for Clean Code
```python
# repositories/base.py
from abc import ABC, abstractmethod
from typing import TypeVar, Generic, List, Optional, Dict, Any
from sqlmodel import SQLModel, Session, select, update, delete, func
ModelType = TypeVar("ModelType", bound=SQLModel)
class BaseRepository(Generic[ModelType], ABC):
"""Base repository with common CRUD operations"""
def __init__(self, model: type[ModelType]):
self.model = model
async def create(self, db: Session, *, obj_in: Dict[str, Any]) -> ModelType:
"""Create a new record"""
db_obj = self.model(**obj_in)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
async def get(self, db: Session, id: Any) -> Optional[ModelType]:
"""Get a record by ID"""
statement = select(seRelated in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.