FastApi CRUD router for SQLAlchemy
Project description
Source Code: https://github.com/drtnn/facrud-router
Requirements
Python 3.9+
facrud-router stands on the shoulders of giants:
Installation
$ pip install facrud-router
---> 100%
Example
Create it
- Create a file
main.pywith:
import uuid
from dataclasses import dataclass
from dataclasses import field
import uvicorn
from facrud_router import ModelCRUDRouter
from fastapi import FastAPI
from pydantic import BaseModel, Field
from sqlalchemy import Column, String
from sqlalchemy.dialects.postgresql import UUID
from app.api.deps import get_session
from app.api.deps import authentication_scheme
app = FastAPI(
title="FastAPI CURD Router Demo",
version="0.1.2",
description="FastAPI CRUD Router for SQLAlchemy",
openapi_url="/openapi.json",
docs_url="/",
)
@dataclass
class User:
__sa_dataclass_metadata_key__ = "sa"
__tablename__ = "user"
id: uuid.UUID = field(
init=False,
default_factory=uuid.uuid4,
metadata={"sa": Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)},
)
username: str = field(metadata={"sa": Column(String(255), nullable=False, index=True)})
full_name: str = field(metadata={"sa": Column(String(255), nullable=False)})
class UserRequestSchema(BaseModel):
username: str = Field(title="Username", max_length=255)
full_name: str = Field(title="User Full Name", max_length=255)
class UserResponseSchema(BaseModel):
id: uuid.UUID = Field(title="User Id")
username: str = Field(title="Username", max_length=255)
full_name: str = Field(title="User Full Name", max_length=255)
router = ModelCRUDRouter(
prefix="user",
model=User,
identifier_type=uuid.UUID,
get_session=get_session,
get_authentication=authentication_scheme,
request_schema=UserRequestSchema,
response_schema=UserResponseSchema
)
app.include_router(router.api_router)
if __name__ == "__main__":
uvicorn.run(app)
Run it
Run the server with:
$ uvicorn main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [28720]
INFO: Started server process [28722]
INFO: Waiting for application startup.
INFO: Application startup complete.
Check it
You already created an API that:
- GET
/user/{user_id}retrieves User object by UUID. - GET
/userreturns list of User objects. - POST
/usercreates User object. - DELETE
/user/{user_id}deletes User object by UUID. - PUT
/user/{user_id}updates User object by UUID. - PATCH
/user/{user_id}partial updates User object by UUID.
Interactive API docs
Now go to http://127.0.0.1:8000.
You will see the automatic interactive API documentation (provided by Swagger UI):
License
This project is licensed under the terms of the Apache License 2.0.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 facrud_router-0.1.2.tar.gz.
File metadata
- Download URL: facrud_router-0.1.2.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.11.0 Darwin/22.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f80e32370f0e1bf4fc957b57dd02c277312a7ced0eb002f1161b33715f3e19b8
|
|
| MD5 |
9ca9e94947ddbadacf555644101024d1
|
|
| BLAKE2b-256 |
e07539b2c291bba306bbb17fb930d5e71501383cdcd8823f6cc538b991150f08
|
File details
Details for the file facrud_router-0.1.2-py3-none-any.whl.
File metadata
- Download URL: facrud_router-0.1.2-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.11.0 Darwin/22.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12558a44d51f9f99ed764ef9309a049ca4c5e09135d4d4bfbc15de4063d086ac
|
|
| MD5 |
7eeb9bfc8783c57262696632d70ca9c3
|
|
| BLAKE2b-256 |
dc4296478792b471d4a131b4432c45c0f76adcec67e8635150d5ee51c01d6ef5
|