Skip to content

kasrakhaksar/dynamic-crud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Dynamic FastAPI CRUD Project

Overview

This project is a dynamic and fully reusable CRUD framework built with FastAPI, SQLAlchemy, and PostgreSQL. The goal of this project is to allow developers to quickly create CRUD APIs for any model with minimal effort. You only define the model's fields once, and the system automatically generates:

  • SQLAlchemy database models
  • Pydantic schemas for request/response validation
  • CRUD operations (create, read, update, delete)
  • FastAPI routers for endpoints

The architecture is designed to be extensible, so adding new fields or models requires minimal changes.


Features

  • Dynamic CRUD for multiple models
  • PostgreSQL integration
  • Fully type-annotated Pydantic models
  • Automatic schema generation from a single source of truth
  • Swagger UI available at /docs for API testing

Project Structure ( EXAMPLE MODELS (USER , PRODUCT) )



fastapi_project/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py            # FastAPI app entry point
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   └── config.py      # Configuration
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   β”œβ”€β”€ base.py        # SQLAlchemy Base
β”‚   β”‚   └── session.py     # DB session
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── user.py        # Dynamic User model
β”‚   β”‚   └── product.py     # Dynamic Product model
β”‚   β”‚   └── fields.py      # Source of truth for all model fields
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   └── dynamic.py     # Dynamic Pydantic schemas
β”‚   β”œβ”€β”€ crud/
β”‚   β”‚   β”œβ”€β”€ base.py        # Generic CRUD class
β”‚   β”‚   β”œβ”€β”€ user.py        # User CRUD
β”‚   β”‚   └── product.py     # Product CRUD
β”‚   └── api/
β”‚       β”œβ”€β”€ user.py        # User API endpoints
β”‚       └── product.py     # Product API endpoints
β”œβ”€β”€ requirements.txt       # Python dependencies
└── README.md

Installation

  1. Clone the repository:
git clone <repo_url>
cd fastapi_project
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate   # Linux / macOS
venv\Scripts\activate      # Windows
  1. Install dependencies:
pip install -r requirements.txt
  1. Configure PostgreSQL in app/core/config.py (or use .env variables if implemented).

  2. Run the application:

uvicorn app.main:app --reload
  1. Open your browser to http://127.0.0.1:8000/docs to explore the Swagger UI.

Usage

The project comes with two default models:

  • User: name, email
  • Product: name, description, price, color

CRUD endpoints are automatically available:

  • POST /users β†’ create a new user
  • GET /users β†’ get all users
  • GET /users/{id} β†’ get a single user
  • PUT /users/{id} β†’ update user
  • DELETE /users/{id} β†’ delete user

How to Add or Modify Fields

All model fields are defined in one place:

app/models/fields.py

Example for Product:

PRODUCT_FIELDS = {
    "name": {"type": str, "nullable": False},
    "description": {"type": str, "nullable": True},
    "price": {"type": float, "nullable": False},
    "color": {"type": str, "nullable": True},  # Add new fields here
}

Steps to add or change a field:

  1. Open app/models/fields.py.
  2. Add a new key for your field with its type and nullable property.
  3. Save the file.
  4. Restart the FastAPI server.

The new field will automatically be included in:

  • SQLAlchemy model
  • Pydantic schemas (Create, Read, Update)
  • CRUD operations
  • API endpoints

No need to touch CRUD classes or Router filesβ€”everything is fully dynamic.


How It Works

  1. Field Definition: fields.py contains the single source of truth for fields.
  2. Dynamic Models: models/user.py and models/product.py use type() to create SQLAlchemy models based on these fields.
  3. Dynamic Schemas: schemas/dynamic.py builds Pydantic models using Annotated and Field.
  4. Generic CRUD: crud/base.py handles all CRUD operations for any model.
  5. Routers: api/user.py and api/product.py use the dynamic CRUD and schemas to expose API endpoints.

This ensures that adding a new field or even a new model requires minimal code changes and everything works out of the box.

About

Dynamic FastAPI With Fully Reusable CRUD For Any Model And Easy Field Extensions πŸŽοΈπŸ’¨

Topics

Resources

Stars

Watchers

Forks

Contributors