ワンクリックで
yweb-orm
YWeb ORM 使用规范。在编写数据库模型定义、CRUD 操作、查询过滤、分页、软删除、事务管理、批量操作、关系定义等数据层代码时使用。基于 SQLAlchemy 的 Active Record 模式。
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
YWeb ORM 使用规范。在编写数据库模型定义、CRUD 操作、查询过滤、分页、软删除、事务管理、批量操作、关系定义等数据层代码时使用。基于 SQLAlchemy 的 Active Record 模式。
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
Enforce form validation before submit. Validate first, return early on failure, and only then run the async submit logic inside try/catch so validation errors aren’t swallowed and misreported.
强制遵循Element Plus开发规范:使用CSS变量而非硬编码值、不修改组件自带样式、不使用内联样式、使用标准CSS类和scoped样式。在涉及Element Plus样式修改时必须调用此技能。
一站式 UI/UX 优化专家,自动执行完整的设计优化流程。Invoke when user wants comprehensive UI/UX improvements, full page optimization, or 'make this look professional'.
Set up and run end-to-end (E2E) tests for web applications using Playwright. Invoke when user wants to create E2E tests, set up testing framework, or run automated browser tests.
强制遵循Y-SSO前端开发规范:Vue组件规范、CSS变量使用、BEM命名、导入顺序、文件结构等。仅在修改 frontend/src/ 目录下文件时调用。
YWeb DDD 分层架构与 API 设计规范。在创建或修改 API 路由、Service 层、领域模型、DTO 时使用。涵盖瘦 API 原则、服务层拆分、Model 设计、DTO 转换、响应格式等。
| name | yweb-orm |
| description | YWeb ORM 使用规范。在编写数据库模型定义、CRUD 操作、查询过滤、分页、软删除、事务管理、批量操作、关系定义等数据层代码时使用。基于 SQLAlchemy 的 Active Record 模式。 |
YWeb ORM 基于 SQLAlchemy,采用 Active Record 模式:
BaseModel(自动获得 id/name/code/时间戳/软删除等字段)CoreModel(仅 id + 时间戳,更轻量)init_database() 一行初始化Model.query 进行链式查询comment:mapped_column(String(255), comment="字段说明")Mapped[str] 类型注解 + mapped_column()__tablename__BaseModel 自带软删除支持,查询自动过滤已删除记录@transactional 装饰器管理事务边界编码时根据需要阅读对应文档:
| 文档路径 | 说明 |
|---|---|
yweb-core/docs/03_orm_guide.md | ORM 基础指南(初始化、模型定义、CRUD、分页、软删除) |
yweb-core/docs/orm_docs/README.md | ORM 完整功能文档索引 |
| 文档路径 | 说明 |
|---|---|
yweb-core/docs/orm_docs/01_overview.md | 概述与架构设计 |
yweb-core/docs/orm_docs/02_model_definition.md | 模型定义(BaseModel、CoreModel、字段) |
yweb-core/docs/orm_docs/03_crud_operations.md | CRUD 操作(save/add/update/delete) |
yweb-core/docs/orm_docs/03_relationships.md | 关系定义(一对一/一对多/多对多/自关联) |
yweb-core/docs/orm_docs/04_query_and_filter.md | 查询与过滤(Query 对象、链式调用) |
yweb-core/docs/orm_docs/05_pagination.md | 分页查询(Page 对象) |
yweb-core/docs/orm_docs/06_bulk_operations.md | 批量操作 |
| 文档路径 | 说明 |
|---|---|
yweb-core/docs/orm_docs/07_soft_delete.md | 软删除机制 |
yweb-core/docs/orm_docs/08_cascade_soft_delete.md | 级联软删除 |
yweb-core/docs/orm_docs/09_version_control.md | 乐观锁与版本控制 |
yweb-core/docs/orm_docs/10_history.md | 历史记录 |
yweb-core/docs/orm_docs/11_transaction.md | 事务管理 |
yweb-core/docs/orm_docs/12_db_session.md | 数据库会话管理 |
yweb-core/docs/orm_docs/16_transaction_manager.md | 高级事务管理器(嵌套事务、Savepoints) |
| 文档路径 | 说明 |
|---|---|
yweb-core/docs/orm_docs/13_serialization.md | 数据序列化(to_dict) |
yweb-core/docs/orm_docs/14_schema_validation.md | Schema 与验证 |
yweb-core/docs/orm_docs/15_fastapi_integration.md | FastAPI 集成 |
| 文档路径 | 说明 |
|---|---|
yweb-core/docs/orm_docs/17-tree-structure-guide.md | 树形结构混入 |
yweb-core/docs/orm_docs/18-sortable-mixin-guide.md | 排序混入 |
yweb-core/docs/orm_docs/19-state-machine-guide.md | 状态机混入 |
yweb-core/docs/orm_docs/20-taggable-mixin-guide.md | 标签混入 |
| 文档路径 | 说明 |
|---|---|
yweb-core/docs/orm_commit_behavior_outside_transaction.md | 事务外提交行为 |
yweb-core/docs/orm_commit_suppression_mechanism.md | 提交抑制机制 |
ORM 基于同步 SQLAlchemy Session,在 async def 中直接调用会阻塞事件循环,框架会抛出 SynchronousOnlyOperation。
| 场景 | 写法 | 安全 |
|---|---|---|
| 纯 DB 操作路由 | def get_users(): | ✅ FastAPI 自动放线程池 |
| 混合异步 + DB | async def + await run_db(...) | ✅ 手动放线程池 |
async def 直接调 ORM | async def + User.query.all() | ❌ 禁止 |
| 启动/lifespan | with allow_sync(): | ✅ 临时豁免 |
# ✅ 推荐:纯 DB 路由用 def
@router.get("/users")
def get_users():
return User.query.all()
# ✅ 混合场景用 run_db
from yweb.orm import run_db
@router.get("/users")
async def get_users():
users = await run_db(User.get_all)
extra = await some_async_call()
return {"users": users, "extra": extra}
# ❌ 禁止:async def 直接调 ORM
@router.get("/users")
async def get_users():
return User.query.all() # → SynchronousOnlyOperation!
详见 yweb-core/docs/orm_docs/15_fastapi_integration.md 和 yweb-core/yweb/orm/async_safety.py。
02_model_definition.md03_crud_operations.md11_transaction.md 和 16_transaction_manager.md03_orm_guide.md 基础指南开始