Skip to content

Commit baf20c4

Browse files
committed
添加ObjectQL AI开发者文档,涵盖项目结构、元数据模式、查询语法、钩子编写和编码规范
1 parent 116573b commit baf20c4

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

docs/AI_CONTEXT.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# ObjectQL AI Developer Context
2+
3+
This document is optimized for AI coding assistants (GitHub Copilot, Cursor, etc.) to understand the ObjectQL framework context, constraints, and valid syntax.
4+
5+
## 1. Project Structure
6+
ObjectQL projects follow a modular structure.
7+
* **Definition Files**: `*.object.yml` defines the database schema and UI metadata.
8+
* **Logic Files**: `*.hook.ts` (triggers) and `*.action.ts` (custom API methods).
9+
* **Entry Point**: `index.ts` usually exports the module definition.
10+
11+
## 2. Metadata Schema (`.object.yml`)
12+
### 2.1 File Format
13+
```yaml
14+
name: project_task # Unique API Name (table name)
15+
label: Project Task # UI Label
16+
description: Tracks individual units of work.
17+
fields:
18+
name:
19+
type: text
20+
required: true
21+
status:
22+
type: select
23+
options: ["New", "In Progress", "Done"]
24+
project:
25+
type: lookup
26+
reference_to: project
27+
required: true
28+
```
29+
30+
### 2.2 Valid Field Types (Updated 2026)
31+
**Basic**: `text`, `textarea`, `markdown`, `html`, `number`, `currency`, `percent`, `boolean`, `password`
32+
**Format**: `email`, `phone`, `url`
33+
**Date**: `date`, `datetime`, `time`
34+
**Media**: `file`, `image`, `avatar` (supports `multiple: true`)
35+
**Geo**: `location`
36+
**Relational**: `lookup`, `master_detail` (supports `reference_to`)
37+
**Calculated**: `formula` (requires `expression`, `data_type`), `summary` (requires `summary_object`, `summary_type`, `summary_field`)
38+
**System**: `auto_number` (requires `auto_number_format`), `grid`, `object`
39+
40+
### 2.3 Key Attributes
41+
`required`, `unique`, `readonly`, `hidden`, `defaultValue`, `multiple`, `min`, `max`, `regex`
42+
43+
## 3. Query Syntax (JSON-based)
44+
ObjectQL uses a unified JSON query format, NOT SQL.
45+
46+
### 3.1 Fetching Data
47+
```typescript
48+
const tasks = await context.object('project_task').find({
49+
fields: ['name', 'status', 'project.name'], // Support dot notation for lookups
50+
filters: [
51+
['status', '=', 'In Progress'],
52+
'or',
53+
['priority', '=', 'High']
54+
],
55+
sort: [['created_at', 'desc']],
56+
skip: 0,
57+
limit: 20
58+
});
59+
```
60+
61+
### 3.2 Filtering Operators
62+
`=`, `!=`, `>`, `>=`, `<`, `<=`, `in`, `nin`, `contains`
63+
64+
## 4. Writing Hooks (`.hook.ts`)
65+
Hooks intercept database operations.
66+
**Triggers**: `beforeCreate`, `afterCreate`, `beforeUpdate`, `afterUpdate`, `beforeDelete`, `afterDelete`.
67+
68+
```typescript
69+
// project_task.hook.ts
70+
import { ObjectQLContext } from '@objectql/core';
71+
72+
export async function beforeCreate({ doc, ctx }: { doc: any, ctx: ObjectQLContext }) {
73+
if (doc.hours > 100) {
74+
throw new Error("Task duration too long");
75+
}
76+
doc.owner = ctx.userId; // Set default owner
77+
}
78+
```
79+
80+
## 5. Coding Conventions
81+
1. **Imports**: Always import types from `@objectql/core` or `@objectql/api`.
82+
2. **Async/Await**: All database operations are asynchronous.
83+
3. **No SQL**: Never write raw SQL. Use the Repository API (`find`, `create`, `update`, `delete`).
84+
4. **Validation**: Prefer schema-level validation (`min`, `regex`) inside `.object.yml` over code validation where possible.
85+

0 commit comments

Comments
 (0)