fastapi-repository-pattern

Build a type-safe generic CRUD repository for FastAPI with async SQLAlchemy 2.0.

Updated Jan 24, 2026
One-click install
npx skills add https://github.com/amaozhao/MySkills --skill fastapi-repository-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: fastapi-repository-pattern
Source: https://github.com/amaozhao/MySkills/tree/main/skills/fastapi-repository-pattern
Command: npx skills add https://github.com/amaozhao/MySkills --skill fastapi-repository-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a type-safe, reusable repository pattern to centralize CRUD operations, keeping database access logic out of services.

Core Features & Use Cases

  • Type-safe, generic CRUD operations across models
  • Async SQLAlchemy 2.0 integration for scalable APIs
  • Clear separation of data access from business logic, enabling easier testing

Quick Start

Instantiate the repository with a SQLAlchemy model and an AsyncSession, then leverage get, list, create, update, and delete in your FastAPI services.

Frequently Asked Questions about fastapi-repository-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement a type-safe CRUD repository in FastAPI?

Build a generic repository class integrating async SQLAlchemy 2.0 with FastAPI, instantiating it with a SQLAlchemy model and AsyncSession to handle type-safe CRUD operations across multiple models.

What is the repository pattern and why use it with FastAPI and SQLAlchemy?

The repository pattern centralizes CRUD operations into a reusable class, separating database access from business logic. In FastAPI, this ensures consistent data access and testable database logic.

Does this generic repository pattern support async SQLAlchemy 2.0?

Yes, the generic repository pattern supports async SQLAlchemy 2.0 integration, enabling scalable FastAPI APIs with type-safe methods for get, list, create, update, and delete operations on your models.

How do I separate database access logic from business logic in FastAPI?

Separate database access logic by moving CRUD operations into a generic repository class, keeping FastAPI services focused on business logic while the repository handles SQLAlchemy queries and data access.

What CRUD methods are available in a type-safe FastAPI repository?

A type-safe FastAPI repository provides methods for get, get_by, list, count, create, create_multi, update, delete, and exists, all operating on SQLAlchemy models and Pydantic schemas.

Can I use the same CRUD repository for multiple SQLAlchemy models in FastAPI?

Yes, the generic repository pattern applies to projects requiring consistent data access across multiple models by instantiating the repository class with different SQLAlchemy models and an AsyncSession.