Auto generate FastAPI endpoint by SqlAlchemy model for data access
Project description
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"],
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlalchemy_fastapi_endpoint_factory-0.0.4.tar.gz.
File metadata
- Download URL: sqlalchemy_fastapi_endpoint_factory-0.0.4.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b61a5bea4a61a0a7ebe1dcca6d89e4e5f93f46cb959b25a2c1b7669a0665a759
|
|
| MD5 |
0ea906644c8426bc408c84f64e36ccd5
|
|
| BLAKE2b-256 |
5bdc8a022537def8eba3db25e7aca1990a881459b90fd7ff91fc3463a8d7bda4
|
File details
Details for the file sqlalchemy_fastapi_endpoint_factory-0.0.4-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_fastapi_endpoint_factory-0.0.4-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3a963bf663a05d69e871fe6f7a46dd9968e8a0b461e800f5d53214d266148b1
|
|
| MD5 |
15717888067cfa8c93d93733f2f144f0
|
|
| BLAKE2b-256 |
26045f0c14f5c4c7d3673fc49ca2a3d4081fc42572276df8c94d12884e67784b
|