|
|
@@ -92,6 +92,7 @@ export default yourEntityRoutes;
|
|
|
| `searchFields` | `string[]` | 搜索字段数组,用于关键词搜索 | 否 |
|
|
|
| `relations` | `string[]` | 关联查询配置,指定需要关联查询的关系 | 否 |
|
|
|
| `middleware` | `any[]` | 应用于所有CRUD路由的中间件数组 | 否 |
|
|
|
+| `relationFields` | `RelationFieldOptions` | 多对多关联字段配置,支持通过ID数组操作关联关系 | 否 |
|
|
|
|
|
|
### 2.3 生成的路由
|
|
|
|
|
|
@@ -170,6 +171,96 @@ export const UpdateYourEntityDto = z.object({
|
|
|
});
|
|
|
```
|
|
|
|
|
|
+### 2.5 多对多关联字段配置
|
|
|
+
|
|
|
+#### 2.5.1 关联字段配置 (RelationFieldOptions)
|
|
|
+
|
|
|
+新增 `relationFields` 配置选项,用于处理多对多关联字段:
|
|
|
+
|
|
|
+```typescript
|
|
|
+import { createCrudRoutes } from '@/server/utils/generic-crud.routes';
|
|
|
+import { PolicyNews, PolicyNewsSchema, CreatePolicyNewsDto, UpdatePolicyNewsDto } from '@/server/modules/silver-users/policy-news.entity';
|
|
|
+import { File } from '@/server/modules/files/file.entity';
|
|
|
+import { authMiddleware } from '@/server/middleware/auth.middleware';
|
|
|
+
|
|
|
+const policyNewsRoutes = createCrudRoutes({
|
|
|
+ entity: PolicyNews,
|
|
|
+ createSchema: CreatePolicyNewsDto,
|
|
|
+ updateSchema: UpdatePolicyNewsDto,
|
|
|
+ getSchema: PolicyNewsSchema,
|
|
|
+ listSchema: PolicyNewsSchema,
|
|
|
+ relations: ['files'],
|
|
|
+ middleware: [authMiddleware],
|
|
|
+ relationFields: {
|
|
|
+ fileIds: {
|
|
|
+ relationName: 'files', // 实体中的关联属性名
|
|
|
+ targetEntity: File, // 关联的目标实体类
|
|
|
+ joinTableName: 'policy_news_files' // 可选:中间表名
|
|
|
+ }
|
|
|
+ }
|
|
|
+});
|
|
|
+```
|
|
|
+
|
|
|
+#### 2.5.2 RelationFieldOptions 类型定义
|
|
|
+
|
|
|
+```typescript
|
|
|
+interface RelationFieldOptions {
|
|
|
+ [fieldName: string]: {
|
|
|
+ relationName: string; // 实体中的关联属性名
|
|
|
+ targetEntity: new () => any; // 关联的目标实体类
|
|
|
+ joinTableName?: string; // 中间表名(可选)
|
|
|
+ };
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+#### 2.5.3 使用示例
|
|
|
+
|
|
|
+**实体定义:**
|
|
|
+```typescript
|
|
|
+@Entity('policy_news')
|
|
|
+export class PolicyNews {
|
|
|
+ @PrimaryGeneratedColumn({ unsigned: true })
|
|
|
+ id!: number;
|
|
|
+
|
|
|
+ @Column({ name: 'news_title', type: 'varchar', length: 255 })
|
|
|
+ newsTitle!: string;
|
|
|
+
|
|
|
+ // 关联文件的多对多关系
|
|
|
+ @ManyToMany(() => File)
|
|
|
+ @JoinTable({
|
|
|
+ name: 'policy_news_files',
|
|
|
+ joinColumn: { name: 'policy_news_id', referencedColumnName: 'id' },
|
|
|
+ inverseJoinColumn: { name: 'file_id', referencedColumnName: 'id' }
|
|
|
+ })
|
|
|
+ files?: File[];
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+**请求格式:**
|
|
|
+```typescript
|
|
|
+// 创建政策资讯
|
|
|
+POST /api/v1/policy-news
|
|
|
+{
|
|
|
+ "newsTitle": "新政策解读",
|
|
|
+ "fileIds": [1, 2, 3] // 关联文件ID数组
|
|
|
+}
|
|
|
+
|
|
|
+// 更新政策资讯
|
|
|
+PUT /api/v1/policy-news/1
|
|
|
+{
|
|
|
+ "newsTitle": "更新后的政策解读",
|
|
|
+ "fileIds": [2, 4, 5] // 更新后的文件ID数组
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+#### 2.5.4 特性说明
|
|
|
+
|
|
|
+- **自动处理**:系统会自动处理多对多关联的中间表操作
|
|
|
+- **事务安全**:所有关联操作都在事务中执行
|
|
|
+- **空数组支持**:传空数组 `[]` 表示清除所有关联
|
|
|
+- **向后兼容**:不影响现有实体的使用方式
|
|
|
+- **类型安全**:支持完整的TypeScript类型检查
|
|
|
+
|
|
|
## 4. 高级用法
|
|
|
|
|
|
### 4.1 自定义中间件
|