Skip to main content

A FastAPI plugin that quickly builds CRUD-API based on SQLAlchemy

Project description

img_1
FastAPIX

PyPI Python Platform License

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

V1.0 后,将去除对pydantic v1的兼容

安装

pip3 install fastapix-py

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 datetime import datetime
from typing import Annotated

from pydantic.functional_serializers import PlainSerializer
from fastapix.crud import SQLModel, Field
from fastapix.pkg.serializer import convert_datetime_to_chinese

# 自定义类型序列化函数  pydantic V2+
# from fastapix.crud.mixins import CreateTimeMixin 默认以采用 `DATETIME` 序列化
# pydantic V2+
DATETIME = Annotated[datetime, PlainSerializer(convert_datetime_to_chinese)]


# 继承 SQLModel
class Category1(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)

    create_time: DATETIME = Field(default_factory=datetime.now, title="Create Time", create=False, update=False)


class Category2(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)

    create_time: DATETIME = Field(default_factory=datetime.now, title="Create Time", create=False, update=False)
    category1_id: UUID = Field(..., title='关联Category1主键', foreign_key=Category1.id)
# 

DATETIME TIMESTAMP 已内置于 fastapix.crud.mixins, 如需其他自定义序列化方式,根据 pydantic 版本,采用不同方式定义

创建连接

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, DBSessionMiddleware

database = EngineDatabase(engine)
# 挂载中间件
app.add_middleware(database.asgi_middleware)
# or 
app.add_middleware(DBSessionMiddleware, db=database)

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

# 挂载路由
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

img_1

## 查询条件

img_2

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

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

fastapix_py-0.12.0-py3-none-any.whl (3.1 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapix_py-0.12.0-py3-none-any.whl
  • Upload date:
  • Size: 3.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for fastapix_py-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 897780caeb001b9aaea4586a991ed440e53b5526ca726ccae839eddedd4ec42a
MD5 61ecaa6fff1a9aed746f679dc0b3ca39
BLAKE2b-256 f197cabe1e083d2ea48dbd7aed6eb571721edc48c36694a65558581ef0f48c37

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