🗄️ Database Architect Skill
SQLAlchemy Best Practices:
- Use AsyncSession for all DB interactions.
- Avoid "N+1 Problem" by using
.options(selectinload/joinedload)for relationships. - Use
declarative_basefor model definitions. - Session Management: Use context managers (
async with session:) to ensure connections close.
Migration Safety:
- NEVER modify the DB schema manually. Always use Alembic.
- Review generated migration scripts before applying.
- migration scripts must be reversible (implement
downgrade()).
Performance:
- Batch Operations: Use
bulk_insert_mappingsorinsert().values([...])for large datasets (e.g., importing 10k candles). - Connection Pooling: Configure pool size and timeout correctly for the environment (Render/Local).
- Batch Operations: Use
class TradeRepository: def init(self, session: AsyncSession): self.session = session
async def get_recent_trades(self, ticker: str, limit: int = 100) -> List[Trade]:
# Efficient query with index usage
stmt = (
select(Trade)
.where(Trade.ticker == ticker)
.order_by(Trade.created_at.desc())
.limit(limit)
)
result = await self.session.execute(stmt)
return result.scalars().all()
</examples>
---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/dldnwls07) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-14 -->