Skip to main content

A FastAPI-based CRUD API generator for MySQL databases

Project description

smart-api-bridge

A flexible and dynamic CRUD (Create, Read, Update, Delete) operations library for FastAPI applications with MySQL database support.

Features

  • Dynamic CRUD operations for any MySQL table
  • Simple integration with FastAPI
  • Automatic pagination
  • Supports both hard delete and soft delete operations
  • Partial updates via the PATCH method
  • Configurable database connection
  • Secure authentication and authorization support

Installation

pip install smart-api-bridge

Quick Start

from fastapi import FastAPI
from api_bridge import APIBridge

app = FastAPI()

# Configure database connection
db_config = {
    "host": "localhost",
    "port": 3306,
    "database": "mydb",
    "user": "username",
    "password": "password@123",
}


# Initialize APIBridge
api_bridge = APIBridge(db_config)

# Create FastAPI app
app = FastAPI()
app.include_router(api_bridge.router)

# Run with uvicorn
# uvicorn main:app --reload

Available Endpoints

Method Endpoint Description
GET /api/test Test database connection
GET /api/{table_name} Fetch all records (supports pagination)
POST /api/{table_name} Insert a new record
PUT /api/{table_name}/{record_id} Update an existing record
PATCH /api/{table_name}/{record_id} Partially update a record
DELETE /api/{table_name}/{record_id} Soft delete a record
DELETE /api/{table_name}/{record_id}/hard Permanently delete a record

Example Applications

1. E-commerce Product Management System

Manage products, categories, inventory, and orders using dynamic CRUD operations:

import os
from fastapi import FastAPI
from api_bridge import APIBridge

app = FastAPI(title="E-commerce API")

# Load database config
db_config = {
    "host": os.getenv("DB_HOST", "localhost"),
    "port": int(os.getenv("DB_PORT", 3306)),
    "database": os.getenv("DB_NAME", "ecommerce"),
    "user": os.getenv("DB_USER", "user"),
    "password": os.getenv("DB_PASS", "password"),
}

# Initialize APIBridge
api_bridge = APIBridge(db_config)

app.include_router(api_bridge.router)

@app.get("/")
def read_root():
    return {"message": "E-commerce API ready"}

2. Content Management System (CMS)

Manage blog posts, pages, users, and media files:

from fastapi import FastAPI
from api_bridge import 
from fastapi.middleware.cors import CORSMiddleware
import os

app = FastAPI(title="CMS API")

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load database config
db_config = {
    "host": os.getenv("DB_HOST", "localhost"),
    "port": int(os.getenv("DB_PORT", 3306)),
    "database": os.getenv("DB_NAME", "cms"),
    "user": os.getenv("DB_USER", "user"),
    "password": os.getenv("DB_PASS", "password"),
}

# Initialize APIBridge
api_bridge = APIBridge(db_config)

app.include_router(api_bridge.router)

@app.get("/")
def read_root():
    return {"message": "CMS API ready"}

Workflow to Publish as a PyPI Package

  1. Prepare Your Package Structure:

    smart-api-bridge/
    ├── smart_api_bridge/
    │   ├── __init__.py
    │   ├── db.py
    │   ├── router.py
    ├── setup.py
    ├── README.md
    ├── LICENSE
    └── requirements.txt
    
  2. Test Your Package Locally:

    pip install -e .
    
  3. Build Your Package:

    pip install build
    python -m build
    
  4. Upload to PyPI Test:

    pip install twine
    twine upload --repository-url https://test.pypi.org/legacy/ dist/*
    
  5. Test Your Package from TestPyPI:

    pip install --index-url https://test.pypi.org/simple/ smart-api-bridge
    
  6. Upload to PyPI:

    twine upload dist/*
    

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smart_api_bridge-0.1.4.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smart_api_bridge-0.1.4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file smart_api_bridge-0.1.4.tar.gz.

File metadata

  • Download URL: smart_api_bridge-0.1.4.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for smart_api_bridge-0.1.4.tar.gz
Algorithm Hash digest
SHA256 b633ca74d90b3348da7adae6c8584204a18c2c52c8306f4f2af85447ce6b0104
MD5 6ad795deebfaf3b905f8c0754c31c901
BLAKE2b-256 f68677814f5ef8fbfac529efc2f156a6208d03319c1ef108cd52956a2ff20373

See more details on using hashes here.

File details

Details for the file smart_api_bridge-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for smart_api_bridge-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b3c4ba61ca11535dde8a8b4ef58bcb8f2cdcf2e7dd43ba2a16f92ba8c947263d
MD5 b3442466502455c8355752608179d3c1
BLAKE2b-256 037e7a27756499886b98b12f6767dec793b672b658680d5473ea6cb9370f9f20

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page