Fast Api Quick Crud | GitLocker.com Product

Fast API Quick CRUD

Last updated:

0 purchases

Fast API Quick CRUD Image
Fast API Quick CRUD Images
Add to Cart

Description:

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.

Features:

  • CRUD Route Generation: Automatically generate FastAPI CRUD routes for operations such as:
    • Get one, Get many
    • Create, Update, Patch, Delete one/many
    • Upsert one/many (PostgreSQL only)
    • Post-Redirect-Get
  • Support for Relationships: Supports foreign key relationships and auto-generates routes for related tables (Foreign Tree).
  • Async and Sync Modes: Fully compatible with both synchronous and asynchronous SQLAlchemy setups.
  • Pagination: Automatically supports pagination for "Get Many" endpoints, allowing for offset/limit queries.
  • Flexible Query Parameters: Supports filtering and limiting results using path parameters (primary key) and query parameters.
  • SQLAlchemy Model Support: Works with both declarative class definitions and imperative tables.
  • Comprehensive CRUD Methods: Includes methods like FIND_ONE, FIND_MANY, UPDATE_ONE, UPDATE_MANY, PATCH_ONE, PATCH_MANY, CREATE_ONE, DELETE_ONE, and UPSERT_ONE (PostgreSQL only).
  • Automatic Pydantic Model Generation: Converts SQLAlchemy models to Pydantic models for seamless API responses.

Requirements:

  • Python Version: Python 3.7 or higher
  • Dependencies:
    • FastAPI <= 0.68.2
    • Pydantic <= 1.8.2
    • SQLAlchemy <= 1.4.30
    • Starlette == 0.14.2
    • Psycopg2 (for PostgreSQL)
    • Asyncpg (for asynchronous PostgreSQL connections)
  • Database: PostgreSQL (recommended for upsert functionality)
  • Optional:
    • sqlacodegen (for generating SQLAlchemy models from existing databases)

Instructions:

Installation

  1. Install the FastAPI Quick CRUD package:
     

    bash

    Copy code

    pip install fastapi-quickcrud

  2. (Optional) Install PostgreSQL libraries for database connection:
     

    bash

    Copy code

    pip install psycopg2 asyncpg

Usage

  1. Setting Up a FastAPI Application:

    • Create your FastAPI app and SQLAlchemy models.
    • Use crud_router_builder to generate CRUD routes for each of your models.
    • Include these routes in your FastAPI application.

    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)

  2. Using Relationships:

    • The tool automatically handles relationships between tables (foreign keys), and generates CRUD routes that include related data.

    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")

  3. Handling Pagination and Filtering:

    • For "Get Many" endpoints, use query parameters like offset, limit, or custom fields for filtering.
  4. Upsert Functionality:

    • Upsert operations (Insert or Update) are supported for PostgreSQL databases. This ensures data is inserted or updated based on a conflict with unique columns.

    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])

  5. Running the Application:

    • Run your FastAPI application using Uvicorn:
     

    bash

    Copy code

    uvicorn app:app --reload

  6. Accessing the API Docs:

    • Once the app is running, navigate to 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:

  • Get One: Fetch a single record by primary key.
  • Get Many: Fetch multiple records with support for filtering via query parameters.
  • Update One: Update a specific record by its primary key.
  • Create One: Insert a new record.
  • Delete One: Delete a specific record by its primary key.

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product: (if this is empty don't purchase this product)

Customer Reviews

There are no reviews.