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.1.tar.gz
(25.1 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
File details
Details for the file fastapis-0.1.tar.gz.
File metadata
- Download URL: fastapis-0.1.tar.gz
- Upload date:
- Size: 25.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9622152040db98047a0622fc77c35212da2b06684aa1da0c9abc9843e10bce54
|
|
| MD5 |
84e7fd718ea636df9815aeca1c17445f
|
|
| BLAKE2b-256 |
e1b1fac1d63ee042e68665fb318b22ec8c471e56404af31d4ea9473c9581c219
|
File details
Details for the file fastapis-0.1-py3-none-any.whl.
File metadata
- Download URL: fastapis-0.1-py3-none-any.whl
- Upload date:
- Size: 2.9 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 |
9a19672d7978aea7e6f81b0ef13d1160dbd82cdefbc7362907061e0ec8b37903
|
|
| MD5 |
0200a2e9c80b84ba75a9a023423801ee
|
|
| BLAKE2b-256 |
94916566187c4196e5b6a833cedfe4e7964739312738e18f6325215071506894
|