Files
akmon/doc_zhipao/SUPADB_RPC_OPTIMIZATION.md
2026-01-20 08:04:15 +08:00

133 lines
3.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Supadb 组件 RPC 支持优化
## 问题描述
之前的 supadb 组件在使用 RPC 调用时仍然要求 `collection` 参数不能为空,这是不合理的,因为:
1. RPC 函数可能不需要操作特定的数据表
2. RPC 函数可以是任意的数据库函数,不一定与某个表相关
3. 强制要求 collection 限制了 RPC 的使用场景
## 解决方案
### 1. 智能参数验证
```typescript
// 检查是否为 RPC 调用
const isRpcCall = props.rpc != null && props.rpc.length > 0;
// 只有在非 RPC 调用时才检查 collection
if (!isRpcCall && (props.collection == null || props.collection.trim() == '')) {
error.value = 'collection/table 不能为空';
loading.value = false;
return;
}
// RPC 调用时检查 rpc 参数
if (isRpcCall && (props.rpc == null || props.rpc.trim() == '')) {
error.value = 'rpc 函数名不能为空';
loading.value = false;
return;
}
```
### 2. 新增 params 属性
```typescript
// RPC 参数,用于传递给 RPC 函数的额外参数
params: {
type: UTSJSONObject,
default: () => ({})
}
```
### 3. 参数合并优先级
RPC 调用时,参数合并顺序:
1. **props.params** - 基础参数
2. **props.filter** - 过滤条件(可能覆盖同名参数)
3. **selectOptions** - 分页和排序参数
```typescript
// 首先添加props.params中的参数
if (props.params != null) {
// 添加 params 中的所有参数
}
// 然后添加filter中的参数可能会覆盖params中的同名参数
if (props.filter != null) {
// 添加 filter 中的所有参数
}
// 最后添加分页和排序参数
if (selectOptions.limit != null) rpcParams.set('limit', selectOptions.limit);
if (selectOptions.order != null) rpcParams.set('order', selectOptions.order);
// ...
```
## 使用示例
### 1. 纯 RPC 调用(无需 collection
```vue
<supadb
rpc="get_analytics_summary"
:params="{ teacher_id: 'abc123' }"
@process-data="handleData"
/>
```
### 2. RPC 调用 + 过滤条件
```vue
<supadb
rpc="get_student_performance"
:params="{ teacher_id: 'abc123' }"
:filter="{ start_date: '2023-01-01', end_date: '2023-12-31' }"
@process-data="handleData"
/>
```
### 3. 传统表查询(仍需 collection
```vue
<supadb
collection="ak_training_records"
:filter="{ status: 'completed' }"
@process-data="handleData"
/>
```
## 优势
### ✅ **灵活性提升**
- RPC 调用不再强制要求 collection 参数
- 支持任意 PostgreSQL 函数调用
- 参数传递更加灵活
### ✅ **向后兼容**
- 现有的表查询功能完全不受影响
- 现有的 RPC 调用代码无需修改
### ✅ **更好的参数管理**
- 新增 `params` 属性用于基础参数
- `filter` 属性专注于过滤条件
- 参数合并优先级清晰
### ✅ **错误处理改进**
- 针对不同使用场景提供准确的错误信息
- RPC 调用和表查询分别验证相关参数
## 影响的文件
- `h:\blews\akmon\components\supadb\supadb.uvue` - 主要组件文件
## 测试建议
1. **RPC 调用测试**
- 测试不带 collection 的纯 RPC 调用
- 测试带参数的 RPC 调用
- 测试参数合并优先级
2. **表查询测试**
- 确保现有的表查询功能正常
- 验证 collection 参数验证仍然有效
3. **错误处理测试**
- 测试缺少 rpc 参数时的错误提示
- 测试缺少 collection 参数时的错误提示
这个优化使得 supadb 组件更加灵活,能够更好地支持各种数据库操作场景。