Skip to main content

Simple FastAPI/SQLAlchemy to super easy create (generic) routes, delcare db types and more.

Project description

FasterApis

just a pet project for own use.

as the name implies, this let's you create FastAPI's combined with SQLAlchemy even FASTER.

from __future__ import annotations

from typing import Optional

from fastapi.security.oauth2 import OAuth2PasswordBearer
from sqlalchemy.testing.schema import mapped_column

from fastapis.db import Base, Mapped, relationship, ForeignKey
from fastapis.web import BaseRouter
from fastapis.web import app
from fastapis.web.types import schema_model_conf, BaseModel


# database models

class DBUser(Base):
    __tablename__ = "db_user"

    username: Mapped[str]
    password: Mapped[Optional[str]]
    token: Mapped[DBToken] = relationship(uselist=False, lazy="selectin")


class DBToken(Base):
    __tablename__ = "db_token"

    value: Mapped[str]
    user_id: Mapped[DBUser] = mapped_column(ForeignKey("db_user.id"))


# fastapi (pydantic) models

class UserRead(BaseModel):
    model_config = schema_model_conf
    username: str
    password: Optional[str] = None
    token: Optional[TokenRead] = None


class UserCreate(BaseModel):
    username: str
    password: str


class TokenRead(BaseModel):
    model_config = schema_model_conf
    value: Optional[str]


# fastapi security

security = OAuth2PasswordBearer(tokenUrl="/token")


# manual route for tokens

@app.post("/token")
async def get_token():
    return dict(access_token="123", token_type="Bearer")


# dependency

async def some_dependency():
    yield "test"


# router for users
# creates all route handlers for CRUD

user_router = BaseRouter(
    db_model=DBUser,
    model_read=UserRead,
    model_create=UserCreate,
    model_patch=UserCreate,
    security=security,
    dependencies=[some_dependency]
)

# include the above router
app.include_router(user_router, prefix="/user", tags=["user"])

# done!

if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Result

output api

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

fastapis-0.12.tar.gz (37.8 kB view details)

Uploaded Source

Built Distribution

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

fastapis-0.12-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapis-0.12.tar.gz.

File metadata

  • Download URL: fastapis-0.12.tar.gz
  • Upload date:
  • Size: 37.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for fastapis-0.12.tar.gz
Algorithm Hash digest
SHA256 ff7eaa7dd8fb738e2d2c1b0036caf62314b432a3c3d9a0b2c1a3ed7f5c4dd049
MD5 ff59a7ed50a1b19c943c4ba3363fb731
BLAKE2b-256 1bcee18a9a64e41c048c85f71206a6a6806c9ff0cad71f86b82ad3273e3de362

See more details on using hashes here.

File details

Details for the file fastapis-0.12-py3-none-any.whl.

File metadata

  • Download URL: fastapis-0.12-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for fastapis-0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 5be8cb88aef4b1a921e271ebb748766473d65807488a49576f0689347f834b12
MD5 bfc548a70ca067ba084279458fc08d05
BLAKE2b-256 e343d41ea7cff19bd9fafe27a0c30a71ca8beb4b82d477b8a335099dddc7b0da

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