Skip to main content

Auto generate FastAPI endpoint by SqlAlchemy model for data access

Project description

Sqlalchemy Fastapi Endpoint Factory

Auto generate FastAPI endpoint by SqlAlchemy model for data access

Installation

pip install sqlalchemy-fastapi-endpoint-factory

Or

uv add --dev sqlalchemy-fastapi-endpoint-factory

Example

Install an example for a quick launch.

uv add fastapi sqlalchemy uvicorn
# main.py
from fastapi import FastAPI
from sqlalchemy import Integer, Column, String, insert

from sqlalchemy_fastapi_endpoint_factory.main import make_endpoint
from sqlalchemy.orm import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

Base = declarative_base()


class SimpleModel(Base):
    __tablename__ = "simple_model"

    id = Column(Integer, primary_key=True)
    username = Column(String)
    email = Column(String)


engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False})
connection = engine.connect()
ScopedSession = scoped_session(
    sessionmaker(autocommit=False, autoflush=False, bind=connection)
)
SimpleModel.__table__.create(bind=engine, checkfirst=True)

# Test data
TEST_DATA = [
    {"id": 1, "username": "user1", "email": "user1@example.com"},
    {"id": 2, "username": "user2", "email": "user2@example.com"},
    {"id": 3, "username": "user3", "email": "user3@example.com"},
    {"id": 4, "username": "admin", "email": "admin@example.com"},
    {"id": 5, "username": "test", "email": "test@example.com"},
]
with engine.begin() as conn:
    conn.execute(insert(SimpleModel), TEST_DATA)


def execute_query(query):
    with ScopedSession() as session:
        result = session.execute(query).mappings()

    return [dict(row) for row in result]


app = FastAPI()
app.add_api_route(
    "/simple/filter",
    endpoint=make_endpoint(SimpleModel, execute_query),
    methods=["POST"],
)
uvicorn main:app

Visit page in browser:

http://localhost:8000/docs

http://localhost:8000/redoc

img.png

img.png

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

sqlalchemy_fastapi_endpoint_factory-1.0.1.tar.gz (379.9 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file sqlalchemy_fastapi_endpoint_factory-1.0.1.tar.gz.

File metadata

File hashes

Hashes for sqlalchemy_fastapi_endpoint_factory-1.0.1.tar.gz
Algorithm Hash digest
SHA256 09f6274827c684d70b76e6fbae14060930c45ad28503eb8626fc88f06a5d7bdc
MD5 65a22abf5958f27482d286031f253c77
BLAKE2b-256 a3d25d6d3f5eb49a958427aa4b10c73ca62d38981c75815dde254178f2497a6b

See more details on using hashes here.

File details

Details for the file sqlalchemy_fastapi_endpoint_factory-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_fastapi_endpoint_factory-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 168dcb84bbf2a12b151e0eea79142aa8e4c46f437962706116ca2313a64e8656
MD5 0a52e9bbc1d703d7f0df6c83a3edc250
BLAKE2b-256 7069aaf05e899099b3dca8d5be67c30360f9b77ead6662411d140494fb71d6f2

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