/scaffolding:database-optimizationfrom scaffolding
Guides schema design, index strategy, migration safety, and query analysis for relational databases. Useful when designing tables, writing migrations, or diagnosing slow queries.
Ships in scaffolding. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
| Form | Use When |
|---|---|
| 1NF | Always (atomic values) |
| 2NF | Most tables |
| 3NF | Transactional data |
| Denormalized | Read-heavy, reporting |
| Type | Use Case |
|---|---|
| B-Tree | Default, range queries |
| Hash | Exact match only |
| GIN (Postgres) | Full-text, JSONB, arrays |
| Partial | Subset of rows |
| Composite | Multi-column queries |
Index type names vary by engine (e.g. GIN/GiST/BRIN are Postgres-specific; MySQL/SQLite expose a different set). Treat engine-specific rows as examples.
| Issue | Symptom | Solution |
|---|---|---|
| Missing index | Sequential scan | Add index |
| N+1 queries | Many similar queries | Eager loading |
| Over-fetching | SELECT * | Select specific columns |
| No pagination | Large result sets | Add LIMIT/OFFSET |
| Cartesian join | Exploding rows | Fix JOIN conditions |
npm run build -- --analyze - Bundle analysislighthouse - Performance auditpy-spy - CPU profilingmemory_profiler - Memory analysisEXPLAIN ANALYZE - Query analysis| Setting | Development | Production |
|---|---|---|
| Min connections | 1 | 5 |
| Max connections | 5 | 20-50 |
| Idle timeout | 30s | 300s |
| Max lifetime | 1800s | 3600s |
Pool size formula: (cores * 2) + disk_spindles. Always use pooling in production.
| Element | Convention | Example |
|---|---|---|
| Tables | snake_case, plural | users, order_items |
| Columns | snake_case | first_name, created_at |
| Primary Key | id | id |
| Foreign Key | {table}_id | user_id, order_id |
| Indexes | ix_{table}_{columns} | ix_users_email |
Illustrative — this is one team's SQLAlchemy/Postgres setup shown as a concrete example. Substitute your ORM, driver, and schema conventions. The schema-design, indexing, and query-analysis guidance above is the engine-agnostic, reusable part.
<your-database-module>.py)from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase
class Base(DeclarativeBase):
pass
engine = create_async_engine(
DATABASE_URL, # postgresql+asyncpg://...
echo=False, future=True,
pool_size=5, max_overflow=10,
pool_recycle=3600, # Recycle connections after 1 hour
pool_pre_ping=True, # Detect stale connections
)
async_session_maker = async_sessionmaker(
engine, class_=AsyncSession, expire_on_commit=False
)
Session dependency (commit-on-success, rollback-on-error):
async def get_db() -> AsyncGenerator[AsyncSession, None]:
async with async_session_maker() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
All models inherit from the shared declarative Base and follow these patterns:
| Convention | Pattern | Example |
|---|---|---|
| Primary key | String(36), UUID as string | id: Mapped[str] = mapped_column(String(36), primary_key=True) |
| Timestamps | DateTime(timezone=True) + utc_now | created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now) |
| Foreign keys | Explicit ondelete policy | ForeignKey("projects.id", ondelete="CASCADE") |
| Nullable FK | ondelete="SET NULL" | ForeignKey("users.id", ondelete="SET NULL") |
| Indexes | On FKs and query columns | index=True on project_id, created_at, github_id |
| Type hints | Mapped[] with mapped_column | SQLAlchemy 2.0 declarative style |
| Relationships | TYPE_CHECKING guard for imports | Avoids circular imports between modules |
| Table | Model | Key Fields |
|---|---|---|
projects | Project | id, path (unique), name, created_at |
task_refs | TaskRef | id, project_id (FK), conversation_id, session_id, created_by (FK) |
users | User | id (uuid4), github_id (unique), github_login, avatar_url |
user_projects | UserProject | user_id (FK), project_id (FK), role, UniqueConstraint |
# Parent side - cascade delete orphans
tasks: Mapped[list["TaskRef"]] = relationship(
"TaskRef", back_populates="project", cascade="all, delete-orphan"
)
# Child side
project: Mapped["Project"] = relationship("Project", back_populates="tasks")
psycopg2 (strips +asyncpg from URL)pg_advisory_lock(1573678) prevents concurrent migrationsmain.py validates ORM vs DB schema on startup, logs warnings{hash}_{description}.py with upgrade() and downgrade()env.py: Required for autogenerate supportupgrade() and downgrade() functionsWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
SQL, PostgreSQL, MySQL, SQLite
npx claudepluginhub komluk/scaffolding --plugin scaffolding39 skills in this plugin
/agent-commsSendMessage recipient validation and worktreePath safety (CWE-59). TRIGGER when: validating a SendMessage `to:` recipient against the agent whitelist, or a worktreePath from another agent before acting on it. SKIP: routing topology (agent Comms Protocol); git ops (use git-operations); worktree recovery (use worktree-management)./agent-memoryRepo-local file memory under .scaffolding/ (shared/agent/conversation tiers). TRIGGER when: reading or writing .scaffolding/ memory files, or choosing which file tier an insight belongs in. SKIP: vector memory / MCP search and store (use semantic-memory-mcp); distilling conversations (use distill)./api-designRESTful API design standards: resource naming, HTTP methods, status codes, pagination, versioning. TRIGGER when: designing new API endpoints, defining error response shapes, or adding pagination/filtering. SKIP: implementing FastAPI route code (use python-patterns); error-handling internals (use error-handling)./context-engineeringOptimize Claude Code context-window usage for accuracy and cost. TRIGGER when: hitting context limits, structuring prompts for an agent, or trimming what gets injected into a task. SKIP: persisting knowledge across sessions (use agent-memory); semantic recall tuning (use semantic-memory-mcp)./distillKnowledge distillation: candidate extraction, confidence scoring, tier routing. Invoked by /learn and the memory-ingest Stop hook, not loaded proactively by agents. TRIGGER when: /learn or the Stop hook asks to distill, or scoring a candidate. SKIP: file memory writes (use agent-memory); vector store (use semantic-memory-mcp)./docker-templatesDocker multi-stage build templates, image security, and docker-compose patterns. TRIGGER when: writing a Dockerfile, composing services, or hardening a container image. SKIP: CI pipeline definitions (use github-actions-template); runtime monitoring (use monitoring-observability).Other plugins' skills for the same job