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

Uploaded Source

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