Last updated:
0 purchases
FastAPI Quick CRUD is a tool designed to streamline the development of RESTful APIs using FastAPI and SQLAlchemy. It automatically generates CRUD (Create, Read, Update, Delete) routes based on SQLAlchemy models, allowing developers to quickly build fully functional APIs without writing repetitive boilerplate code. The tool supports both synchronous and asynchronous SQLAlchemy setups and enables operations such as creating, updating, deleting, and querying records, including handling complex relationships, pagination, and upsert functionality.
FIND_ONE
, FIND_MANY
, UPDATE_ONE
, UPDATE_MANY
, PATCH_ONE
, PATCH_MANY
, CREATE_ONE
, DELETE_ONE
, and UPSERT_ONE
(PostgreSQL only).sqlacodegen
(for generating SQLAlchemy models from existing databases)Installation
bash
Copy code
pip install fastapi-quickcrud
bash
Copy code
pip install psycopg2 asyncpg
Usage
Setting Up a FastAPI Application:
crud_router_builder
to generate CRUD routes for each of your models.Example:
python
Copy code
from fastapi import FastAPI from sqlalchemy import Column, Integer, String, ForeignKey from sqlalchemy.orm import relationship, sessionmaker from fastapi_quickcrud import crud_router_builder Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String) email = Column(String) app = FastAPI() crud_route = crud_router_builder(db_model=User, prefix="/users", tags=["User"], async_mode=True) app.include_router(crud_route)
Using Relationships:
Example:
python
Copy code
class Account(Base): __tablename__ = "account" id = Column(Integer, primary_key=True) blog_post = relationship("BlogPost", back_populates="account") class BlogPost(Base): __tablename__ = "blog_post" id = Column(Integer, primary_key=True) account_id = Column(Integer, ForeignKey("account.id")) account = relationship("Account", back_populates="blog_post")
Handling Pagination and Filtering:
offset
, limit
, or custom fields for filtering.Upsert Functionality:
Example:
python
Copy code
from fastapi_quickcrud import CrudMethods friend_model_set = sqlalchemy_to_pydantic(db_model=Friend, crud_methods=[CrudMethods.UPSERT_ONE, CrudMethods.UPSERT_MANY])
Running the Application:
bash
Copy code
uvicorn app:app --reload
Accessing the API Docs:
http://127.0.0.1:8000/docs
to view the auto-generated API documentation.Example of Generated CRUD Methods:
The generated CRUD methods allow you to perform operations such as:
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.