Skip to content

Commit 9009d5b

Browse files
authored
Merge pull request #43 from objectstack-ai/copilot/translate-english-doc-to-chinese
2 parents 04ad23d + e535b0e commit 9009d5b

30 files changed

+4779
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ This repository contains the documentation for:
1313
## Features
1414

1515
- 🌍 **Multi-language Support**:
16-
- Source: English (`content/docs`)
17-
- Target: Chinese (`content/docs-cn`) - *Auto-translated via AI*
16+
- Source: English (`content/docs/*.mdx`)
17+
- Target: Chinese (`content/docs/*.cn.mdx`) - *Auto-translated via AI using dot parser*
1818
- 📝 **MDX Content**: Interactive documentation with Type-safe components.
1919
- 🛠️ **Automated Workflows**:
2020
- AI Translation CLI (`packages/cli`)
Lines changed: 373 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,373 @@
1+
---
2+
title: AI 代码宝典
3+
description: AI 辅助 ObjectStack 开发的最佳实践
4+
---
5+
6+
# AI 代码宝典
7+
8+
本指南帮助开发者利用 AI 工具来提高 ObjectStack 开发效率。
9+
10+
## 为什么选择 AI 协作
11+
12+
ObjectStack 作为协议驱动的平台,涉及很多:
13+
- 架构定义
14+
- UI 协议配置
15+
- 重复的 CRUD 代码
16+
- API 文档生成
17+
18+
这些都非常适合 AI 协助,可以显著提高开发效率。
19+
20+
## 核心术语
21+
22+
与 AI 协作时使用一致的术语至关重要。以下是 ObjectStack 的核心术语:
23+
24+
### 数据层术语
25+
26+
- **Schema**:JSON 格式的数据模型定义,描述表结构
27+
- **Object**:业务对象,对应于数据库表
28+
- **Field**:对象字段,对应于数据库列
29+
- **Reference**:关系字段,用于建立对象之间的关联
30+
- **Virtual Field**:计算字段,不存储在数据库中
31+
32+
### 查询层术语
33+
34+
- **ObjectQL**:统一的跨数据库查询语言
35+
- **Query DSL**:查询域特定语言
36+
- **Filter**:查询过滤条件
37+
- **Aggregation**:聚合查询
38+
- **Driver**:数据库驱动程序,例如 `@objectql/driver-mysql`
39+
40+
### UI 层术语
41+
42+
- **ObjectUI**:JSON 驱动的 UI 协议
43+
- **Component**:表单、表格、卡片等组件
44+
- **Action**:交互操作,如 API 请求、页面导航等
45+
- **Renderer**:将 JSON 协议转换为实际 UI
46+
47+
### 平台层术语
48+
49+
- **ObjectOS**:运行时平台
50+
- **Plugin**:功能扩展
51+
- **Hook**:生命周期钩子,如 beforeInsert、afterUpdate
52+
- **RBAC**:基于角色的访问控制
53+
- **FLS**:字段级安全
54+
55+
## AI 辅助架构设计
56+
57+
### 提示模板
58+
59+
```
60+
我需要在 ObjectStack 中为[业务场景]创建一个 Schema。
61+
62+
业务需求:
63+
- [需求 1]
64+
- [需求 2]
65+
- [需求 3]
66+
67+
请生成符合 ObjectStack 规范的 JSON Schema,包括:
68+
1. 所有必要的字段定义
69+
2. 字段类型(text、number、date、reference 等)
70+
3. 字段约束(required、unique 等)
71+
4. 对象之间的关系定义
72+
```
73+
74+
### 示例对话
75+
76+
**用户**
77+
```
78+
我需要在 ObjectStack 中为电子商务订单管理系统创建一个 Schema。
79+
80+
业务需求:
81+
- 管理客户信息(名称、电话、邮箱)
82+
- 管理产品信息(名称、价格、库存)
83+
- 管理订单(订单号、客户、产品列表、总金额、状态)
84+
- 订单状态包括:待支付、已支付、已发货、已完成、已取消
85+
86+
请生成符合 ObjectStack 规范的 JSON Schema。
87+
```
88+
89+
**AI 响应**
90+
```json
91+
{
92+
"objects": {
93+
"customers": {
94+
"label": "Customer",
95+
"fields": {
96+
"name": { "type": "text", "label": "Name", "required": true },
97+
"phone": { "type": "text", "label": "Phone" },
98+
"email": { "type": "email", "label": "Email", "unique": true }
99+
}
100+
},
101+
"products": {
102+
"label": "Product",
103+
"fields": {
104+
"name": { "type": "text", "label": "Product Name", "required": true },
105+
"price": { "type": "number", "label": "Price", "required": true },
106+
"stock": { "type": "number", "label": "Stock", "default": 0 }
107+
}
108+
},
109+
"orders": {
110+
"label": "Order",
111+
"fields": {
112+
"order_no": { "type": "text", "label": "Order Number", "unique": true },
113+
"customer": { "type": "reference", "ref": "customers", "label": "Customer" },
114+
"items": { "type": "grid", "label": "Order Items", "fields": {
115+
"product": { "type": "reference", "ref": "products" },
116+
"quantity": { "type": "number", "default": 1 },
117+
"price": { "type": "number" }
118+
}},
119+
"total": { "type": "number", "label": "Total Amount" },
120+
"status": {
121+
"type": "select",
122+
"label": "Status",
123+
"options": ["pending", "paid", "shipped", "completed", "cancelled"],
124+
"default": "pending"
125+
}
126+
}
127+
}
128+
}
129+
}
130+
```
131+
132+
## AI 辅助 UI 协议生成
133+
134+
### 提示模板
135+
136+
```
137+
我需要为[对象名称]创建 ObjectUI 协议。
138+
139+
UI 需求:
140+
- [UI 需求 1]
141+
- [UI 需求 2]
142+
143+
请生成符合 ObjectStack ObjectUI 规范的 JSON 协议。
144+
```
145+
146+
### 示例对话
147+
148+
**用户**
149+
```
150+
我需要为订单对象创建列表页面和详情表单。
151+
152+
UI 需求:
153+
- 列表页面显示订单号、客户名称、总金额、状态
154+
- 支持按状态过滤
155+
- 点击订单显示详情
156+
- 详情表单可以编辑订单状态
157+
```
158+
159+
**AI 响应**
160+
```json
161+
{
162+
"pages": {
163+
"order_list": {
164+
"type": "page",
165+
"title": "Order List",
166+
"components": [
167+
{
168+
"type": "table",
169+
"object": "orders",
170+
"columns": [
171+
{ "field": "order_no", "label": "Order No" },
172+
{ "field": "customer.name", "label": "Customer" },
173+
{ "field": "total", "label": "Total" },
174+
{ "field": "status", "label": "Status" }
175+
],
176+
"filters": [
177+
{ "field": "status", "type": "select", "label": "Status" }
178+
],
179+
"actions": [
180+
{ "type": "navigate", "target": "order_detail", "label": "View" }
181+
]
182+
}
183+
]
184+
},
185+
"order_detail": {
186+
"type": "page",
187+
"title": "Order Detail",
188+
"components": [
189+
{
190+
"type": "form",
191+
"object": "orders",
192+
"fields": [
193+
{ "name": "order_no", "label": "Order No", "readonly": true },
194+
{ "name": "customer", "label": "Customer", "readonly": true },
195+
{ "name": "total", "label": "Total", "readonly": true },
196+
{ "name": "status", "label": "Status", "type": "select" }
197+
],
198+
"actions": [
199+
{ "type": "submit", "label": "Save" }
200+
]
201+
}
202+
]
203+
}
204+
}
205+
}
206+
```
207+
208+
## AI 辅助插件开发
209+
210+
### 提示模板
211+
212+
```
213+
我需要为 ObjectStack 开发一个服务端插件。
214+
215+
功能需求:
216+
- [需求描述]
217+
218+
请生成符合 ObjectStack 插件规范的代码,包括:
219+
1. 插件入口文件
220+
2. 必要的生命周期钩子
221+
3. 错误处理
222+
```
223+
224+
### 示例代码生成
225+
226+
**用户**
227+
```
228+
我需要开发一个插件,在订单创建后自动发送电子邮件通知。
229+
```
230+
231+
**AI 响应**
232+
```javascript
233+
// plugins/order-notification.js
234+
module.exports = {
235+
name: 'order-notification',
236+
237+
hooks: {
238+
afterInsert: async (context, object, record) => {
239+
if (object === 'orders') {
240+
const { customer, total, order_no } = record;
241+
242+
// Query customer email
243+
const customerData = await context.db.query({
244+
object: 'customers',
245+
filters: { _id: customer },
246+
fields: ['email', 'name']
247+
});
248+
249+
if (customerData && customerData[0]?.email) {
250+
await context.email.send({
251+
to: customerData[0].email,
252+
subject: `Order Confirmation - ${order_no}`,
253+
body: `
254+
Dear ${customerData[0].name},
255+
256+
Your order ${order_no} has been created successfully.
257+
Order amount: $${total}
258+
259+
Thank you for your purchase!
260+
`
261+
});
262+
}
263+
}
264+
}
265+
}
266+
};
267+
```
268+
269+
## AI 辅助文档生成
270+
271+
### 自动生成 API 文档
272+
273+
使用 TypeDoc 或 JSDoc 从代码注释自动生成文档:
274+
275+
```typescript
276+
/**
277+
* Query object data
278+
* @param {Object} options - Query options
279+
* @param {string} options.object - Object name
280+
* @param {Object} options.filters - Filter conditions
281+
* @param {Object} options.sort - Sort rules
282+
* @param {number} options.limit - Limit count
283+
* @returns {Promise<Array>} Query results
284+
*
285+
* @example
286+
* const users = await db.query({
287+
* object: 'users',
288+
* filters: { age: { $gt: 18 } },
289+
* sort: { created_at: 'desc' },
290+
* limit: 10
291+
* });
292+
*/
293+
async function query(options) {
294+
// Implementation code
295+
}
296+
```
297+
298+
然后运行:
299+
```bash
300+
npx typedoc --out docs/api src/
301+
```
302+
303+
## 最佳实践
304+
305+
### 1. 清晰的背景
306+
307+
与 AI 交流时,明确指定:
308+
- 使用 ObjectStack 概念
309+
- 要遵循的协议规范
310+
- 目标数据库类型(如有特殊要求)
311+
312+
### 2. 提供完整信息
313+
314+
包括:
315+
- 业务需求的详细描述
316+
- 数据模型之间的关系
317+
- UI 交互流
318+
- 特殊业务规则
319+
320+
### 3. 验证生成的代码
321+
322+
AI 生成的代码需要:
323+
- 验证 ObjectStack 符合性
324+
- 功能测试
325+
- 安全和性能审查
326+
327+
### 4. 迭代优化
328+
329+
如果结果不理想:
330+
- 提供更具体的需求
331+
- 给出示例或参考
332+
- 逐步分解需求
333+
334+
## 推荐工具
335+
336+
### 代码生成
337+
- **GitHub Copilot**:代码补全和生成
338+
- **ChatGPT / Claude**:架构设计和问题解决
339+
- **Cursor**:AI 辅助编程 IDE
340+
341+
### 文档生成
342+
- **TypeDoc**:TypeScript API 文档
343+
- **JSDoc**:JavaScript API 文档
344+
- **Swagger/OpenAPI**:REST API 文档
345+
346+
### 测试协助
347+
- **AI 生成的测试用例**:覆盖边界情况
348+
- **AI 代码审查**:发现潜在问题
349+
350+
## 考虑事项
351+
352+
### ⚠️ AI 限制
353+
354+
- AI 可能生成不符合最新规范的代码
355+
- 需要手动验证业务逻辑正确性
356+
- 复杂的架构设计仍需要人类决策
357+
358+
### ✅ AI 优势
359+
360+
- 快速生成重复代码
361+
- 提供多个实现选项
362+
- 协助文档编写
363+
- 代码重构建议
364+
365+
## 总结
366+
367+
AI 是 ObjectStack 开发的强大助手,但不是替代品。最佳实践:
368+
369+
1. **使用 AI 加速**:架构定义、UI 协议、重复代码
370+
2. **手动审查**:业务逻辑、安全、性能优化
371+
3. **持续学习**:理解 AI 的能力和局限,适当使用
372+
373+
通过正确使用 AI 工具,开发效率可以提高 3-5 倍,使开发者能够更多地专注于核心业务逻辑。

0 commit comments

Comments
 (0)