Skip to main content

fastapi plugin

Project description

FastAPIX

FastAPIX 为 FastAPI 提供的基于 SQLAlchemy ORM 快速构建数据库操作的插件.

安装

pip3 install fastapix-py
# or
pip3 install fastapix_py-<version>-py3-none-any.whl

fastapi

from fastapi import FastAPI

app = FastAPI()

离线挂载 openapi

from fastapix import offline

offline.register_offline_openapi(app)

修改异常返回结果

from fastapix import handlers

handlers.register_exception_handlers(app)

ORM模型

from uuid import UUID, uuid4

from fastapix.crud import SQLModel, Field

# 继承 SQLModel
class Category(SQLModel, table=True):
    id: UUID = Field(default_factory=uuid4, primary_key=True, nullable=False, create=False, update=False)
    name: str = Field(..., title='名称', max_length=100, index=True, nullable=False, unique=True, update=False)
    description: str | None = Field(None, title='描述', max_length=255)
    test_list: list[str] | None = Field(None, title='test list')
    test_dict: dict[str, str] | None = Field(None, title='test dict')

创建连接

AsyncDatabase

from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine

database_url = 'sqlite+aiosqlite:///test.db'
engine: AsyncEngine = create_async_engine(database_url)

Database

from sqlalchemy.engine import create_engine, Engine

database_url = 'sqlite+aiosqlite:///test.db'
engine: Engine = create_engine(database_url)

注册

from fastapix.crud import SQLAlchemyCrud, EngineDatabase

database = EngineDatabase(engine)

cate_router = SQLAlchemyCrud(Category, database).router_manager()

# 挂载中间件
app.add_middleware(database.asgi_middleware)
# 挂载路由
app.include_router(cate_router.create_object_router())
app.include_router(cate_router.read_object_router())
app.include_router(cate_router.update_object_router())
app.include_router(cate_router.delete_object_router())

OpenAPI Docs

http://localhost:8000/docs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

fastapix_py-0.5.0-py3-none-any.whl (2.5 MB view details)

Uploaded Python 3

File details

Details for the file fastapix_py-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: fastapix_py-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 2.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for fastapix_py-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 732cf74df1b359cd2d67c460e7ef265978a990c745b100e3c22deeed157f6ad1
MD5 cd1930d10016cc18837e6a275e627da6
BLAKE2b-256 6262485916b4e0973570805c781947e045958fb81c2ca07a72a03c04bb4e8507

See more details on using hashes here.

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