2
0
yourname 5 сар өмнө
parent
commit
a7de211035

+ 24 - 1
.roo/rules/10-entity.md

@@ -1,10 +1,33 @@
 # 数据库实体规范
 
+## 常见不规范问题
+  1. nullable 字段用了? 而不是!, 类型没加 null
+    错误示例:
+      ```ts
+      @Column({
+        name: 'description',
+        type: 'text',
+        nullable: true,
+        comment: '容器配置描述'
+      })
+      description?: string;
+      ```
+    正确示例:
+      ```ts
+      @Column({
+        name: 'description',
+        type: 'text',
+        nullable: true,
+        comment: '容器配置描述'
+      })
+      description!: string | null;
+      ```
+
 ## 1. 实体基础结构
 
 ```typescript
 import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
-import { z } from 'zod';
+import { z } from '@hono/zod-openapi'
 
 @Entity('table_name') // 使用小写下划线命名表名
 export class EntityName {

+ 51 - 0
.roo/rules/11-entity-creation.md

@@ -0,0 +1,51 @@
+# 新实体创建流程规范
+
+## 完整开发流程
+
+1. **创建实体**
+   - 位置: `src/server/modules/[模块名]/[实体名].entity.ts`
+   - 参考已有实体文件如`user.entity.ts`
+   - 注意: 必须包含Zod Schema定义
+
+2. **创建Service**
+   - 位置: `src/server/modules/[模块名]/[实体名].service.ts`
+   - 通过构造函数注入DataSource
+   - 使用实体Schema进行输入输出验证
+
+3. **创建API路由**
+   - 目录结构:
+     ```
+     src/server/api/[实体名]/
+     ├── get.ts       # 列表
+     ├── post.ts      # 创建
+     ├── [id]/
+     │   ├── get.ts   # 详情
+     │   ├── put.ts   # 更新
+     │   └── delete.ts # 删除
+     └── index.ts     # 路由聚合
+     ```
+   - 必须使用实体Schema作为请求/响应Schema
+   - 参考`users`模块的实现
+
+4. **注册路由**
+   - 在`src/server/api.ts`中添加路由注册
+
+5. **创建客户端API**
+   - 在`src/client/api.ts`中添加客户端定义
+
+6. **前端调用**
+   - 在页面组件(如`pages_users.tsx`)中:
+     - 使用`InferResponseType`提取响应类型
+     - 使用`InferRequestType`提取请求类型
+     - 示例:
+       ```typescript
+       type EntityResponse = InferResponseType<typeof entityClient.$get, 200>;
+       type CreateRequest = InferRequestType<typeof entityClient.$post>['json'];
+       ```
+
+## 注意事项
+
+1. 实体Schema必须在实体文件中定义,路由中直接引用,不要重复定义
+2. 前端表格/表单字段必须与实体定义保持一致
+3. 确保所有API调用都有正确的类型推断
+4. 参考现有模块实现保持风格一致