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
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.11.tar.gz
(37.6 kB
view details)
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
fastapis-0.11-py3-none-any.whl
(16.3 kB
view details)
File details
Details for the file fastapis-0.11.tar.gz.
File metadata
- Download URL: fastapis-0.11.tar.gz
- Upload date:
- Size: 37.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c8f46635b3ab4b575be8d1110cedabf95e2128a94b754dd891388ce8227500c
|
|
| MD5 |
3e1807f579d74646c919a0467de3be70
|
|
| BLAKE2b-256 |
46f11aaa9609ba44be800e0e70797320201d33c12aef480f4367cf2663a4791f
|
File details
Details for the file fastapis-0.11-py3-none-any.whl.
File metadata
- Download URL: fastapis-0.11-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
995cc8436968feb11fc22c10283ce34bec731f1a205f4873280a091aaabeb84e
|
|
| MD5 |
5c19a1f7d4d1f80edef982b2f9a8224e
|
|
| BLAKE2b-256 |
0c5ed146df785496d4aef90ad9555e0c6ff0195f75f728a76d1867370c9773aa
|