Skip to main content

FastAPI Router that creates CRUD routes for SqlAlchemy models

Project description

fastapi-modelrouter

FastAPI Router that creates CRUD routes for SqlAlchemy models

Features

  • Generates direct from SqlAlchemy Models CRUD routes
  • no need to write Pydantic Basemodels
  • keep SqlAlchemy Models and routes in sync
  • Simple to Use
  • Supports composite primary keys
  • Supports queryparams

Installation

pip install fastapi-modelrouter

Basic Usage

from fastapi import FastAPI
from modelrouter import ModelRouter

# setup Sqlalchemy Database
from sqlalchemy import create_engine, Column, String, Integer
from sqlalchemy.orm import sessionmaker, declarative_base

sqlalchemy_database_url = "sqlite:///:memory"

engine = create_engine(
    sqlalchemy_database_url, connect_args={"check_same_thread": False}
)
session_local = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()


# Define Project Model
class Project(Base):
    __tablename__ = 'project'
    projectno = Column(String, primary_key=True)
    project = Column(String)
    state = Column(Integer, default=0)
    owner = Column(String)


Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)


# Database session function
def get_db():
    db = session_local()
    try:
        yield db
    finally:
        db.close()


# Create FastApi
app = FastAPI()

# Create and include Modelrouter 
app.include_router(ModelRouter(Project, get_db))

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

fastapi-modelrouter-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file fastapi-modelrouter-0.1.1.tar.gz.

File metadata

  • Download URL: fastapi-modelrouter-0.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for fastapi-modelrouter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 548fc7ed17995e93ae0e4781a292f636be840ffe65a7dcbb47ca3e6779f3928a
MD5 cc8381fb3cb73cf916321299f8a7fe6f
BLAKE2b-256 002e4a5539ae75e3ad90f000d9dc26ba345f27b779c7fb1477b4f60db153158d

See more details on using hashes here.

Supported by

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