API 参考¶
本页面提供 FastAPI-Easy 的完整 API 参考文档。
核心模块¶
CRUDRouter¶
自动生成 CRUD 路由的核心类。
CRUDRouter
¶
Bases: APIRouter
CRUD Router for automatic API generation
Generates CRUD routes automatically with support for filtering, sorting, pagination, and more.
__init__
¶
__init__(
schema: Type[BaseModel],
adapter: Optional[ORMAdapter] = None,
config: Optional[CRUDConfig] = None,
prefix: Optional[str] = None,
tags: Optional[List[str]] = None,
id_type: Type = int,
create_schema: Optional[Type[BaseModel]] = None,
update_schema: Optional[Type[BaseModel]] = None,
**kwargs: Any
)
Initialize CRUD Router
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
schema
|
Type[BaseModel]
|
Pydantic schema for the resource (used for reading) |
必需 |
adapter
|
Optional[ORMAdapter]
|
ORM adapter instance |
None
|
config
|
Optional[CRUDConfig]
|
CRUD configuration |
None
|
prefix
|
Optional[str]
|
API prefix (default: lowercase schema name) |
None
|
tags
|
Optional[List[str]]
|
OpenAPI tags |
None
|
id_type
|
Type
|
Type of the ID field (default: int) |
int
|
create_schema
|
Optional[Type[BaseModel]]
|
Schema for creation (default: same as schema) |
None
|
update_schema
|
Optional[Type[BaseModel]]
|
Schema for updates (default: same as schema) |
None
|
**kwargs
|
Any
|
Additional arguments passed to APIRouter |
{}
|
get_config
¶
update_config
¶
Update configuration
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
**kwargs
|
Any
|
Configuration fields to update |
{}
|
适配器 (Adapters)¶
SQLAlchemy 适配器¶
SQLAlchemyAdapter
¶
Bases: BaseORMAdapter
SQLAlchemy async ORM adapter
Supports SQLAlchemy 2.0+ with async/await.
__init__
¶
Initialize SQLAlchemy adapter
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
model
|
Type[DeclarativeBase]
|
SQLAlchemy ORM model class |
必需 |
session_factory
|
Async session factory |
必需 | |
pk_field
|
str
|
Primary key field name |
'id'
|
get_all
async
¶
Get all items with filtering, sorting, and pagination
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
filters
|
Dict[str, Any]
|
Filter conditions |
必需 |
sorts
|
Dict[str, Any]
|
Sort conditions |
必需 |
pagination
|
Dict[str, Any]
|
Pagination info (skip, limit) |
必需 |
返回:
| 类型 | 描述 |
|---|---|
List[Any]
|
List of items |
get_one
async
¶
Get single item by id
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
id
|
Any
|
Item id |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Optional[Any]
|
Item or None |
create
async
¶
Create new item
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
data
|
Dict[str, Any]
|
Item data |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
Created item |
引发:
| 类型 | 描述 |
|---|---|
ConflictError
|
If item already exists (unique constraint violation) |
AppError
|
For other database errors |
update
async
¶
Update item
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
id
|
Any
|
Item id |
必需 |
data
|
Dict[str, Any]
|
Updated data |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
Updated item |
引发:
| 类型 | 描述 |
|---|---|
ConflictError
|
If unique constraint violation |
AppError
|
For other database errors |
delete_one
async
¶
Delete single item
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
id
|
Any
|
Item id |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
Deleted item |
引发:
| 类型 | 描述 |
|---|---|
AppError
|
For database errors |
delete_all
async
¶
Delete all items
返回:
| 类型 | 描述 |
|---|---|
List[Any]
|
List of deleted items |
引发:
| 类型 | 描述 |
|---|---|
AppError
|
For database errors |
count
async
¶
Count items
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
filters
|
Dict[str, Any]
|
Filter conditions |
必需 |
返回:
| 类型 | 描述 |
|---|---|
int
|
Total count |
Tortoise 适配器¶
TortoiseAdapter
¶
Bases: BaseORMAdapter
Tortoise ORM adapter
Supports Tortoise ORM with async/await.
__init__
¶
Initialize Tortoise adapter
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
model
|
Type[Model]
|
Tortoise ORM model class |
必需 |
session_factory
|
Not used for Tortoise (included for interface compatibility) |
None
|
|
pk_field
|
str
|
Primary key field name |
'id'
|
get_all
async
¶
Get all items with filtering, sorting, and pagination
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
filters
|
Dict[str, Any]
|
Filter conditions |
必需 |
sorts
|
Dict[str, Any]
|
Sort conditions |
必需 |
pagination
|
Dict[str, Any]
|
Pagination info (skip, limit) |
必需 |
返回:
| 类型 | 描述 |
|---|---|
List[Any]
|
List of items |
get_one
async
¶
Get single item by id
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
id
|
Any
|
Item id |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Optional[Any]
|
Item or None |
create
async
¶
Create new item
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
data
|
Dict[str, Any]
|
Item data |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
Created item |
引发:
| 类型 | 描述 |
|---|---|
ConflictError
|
If item already exists (unique constraint violation) |
AppError
|
For other database errors |
update
async
¶
Update item
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
id
|
Any
|
Item id |
必需 |
data
|
Dict[str, Any]
|
Updated data |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
Updated item |
引发:
| 类型 | 描述 |
|---|---|
ConflictError
|
If unique constraint violation |
AppError
|
For other database errors |
delete_one
async
¶
Delete single item
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
id
|
Any
|
Item id |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
Deleted item |
引发:
| 类型 | 描述 |
|---|---|
AppError
|
For database errors |
delete_all
async
¶
Delete all items
返回:
| 类型 | 描述 |
|---|---|
List[Any]
|
List of deleted items |
引发:
| 类型 | 描述 |
|---|---|
AppError
|
For database errors |
count
async
¶
Count items
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
filters
|
Dict[str, Any]
|
Filter conditions |
必需 |
返回:
| 类型 | 描述 |
|---|---|
int
|
Total count |
配置¶
CRUD 配置¶
CRUDConfig
dataclass
¶
CRUD Router Configuration
Centralized configuration for all CRUD operations.
validate
¶
Validate configuration
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
If configuration is invalid |
使用说明¶
本 API 参考文档自动从代码的 docstring 生成。如果某些类或方法缺少文档,说明源代码中尚未添加文档字符串。
更多详细的使用指南,请参考: