Skip to content

Commit 2872aa1

Browse files
committed
添加对公式、汇总和自动编号字段的支持,并更新相关字段配置和测试用例
1 parent 9313613 commit 2872aa1

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

docs/METADATA.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ fields:
121121
| `select` | Selection from a list. | `options`, `multiple` |
122122
| `lookup` | Reference to another object. | `reference_to`, `multiple` |
123123
| `master_detail` | Strong ownership relationship. | `reference_to` (Required) |
124+
| `formula` | Read-only calculated field. | `expression`, `data_type` |
125+
| `summary` | Roll-up summary of child records. | `summary_object`, `summary_type`, `summary_field`, `summary_filters` |
126+
| `auto_number` | Auto-incrementing unique identifier. | `auto_number_format` |
124127
| `object` | JSON object structure. | |
125128
| `grid` | Array of objects/rows. | |
126129

packages/core/src/metadata.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ export type FieldType =
1818
| 'currency'
1919
| 'percent'
2020
| 'boolean'
21-
| 'lookup'
21+
| 'lookup'
2222
| 'master_detail'
2323
| 'password'
24+
| 'formula'
25+
| 'summary'
26+
| 'auto_number'
2427
| 'object'
2528
| 'grid';
2629

@@ -79,10 +82,30 @@ export interface FieldConfig {
7982
// Relationship properties
8083
/**
8184
* The API name of the target object.
82-
* Required when type is `lookup` or `master_detail`.
85+
* Required when type is `lookup` or `master_detail` or `summary`.
8386
*/
8487
reference_to?: string;
8588

89+
// Formula properties
90+
/** The expression for formula fields. */
91+
expression?: string;
92+
/** The return data type for formula or summary fields. */
93+
data_type?: 'text' | 'boolean' | 'date' | 'datetime' | 'number' | 'currency' | 'percent';
94+
95+
// Summary properties
96+
/** The child object to summarize. */
97+
summary_object?: string;
98+
/** The type of summary calculation. */
99+
summary_type?: 'count' | 'sum' | 'min' | 'max' | 'avg';
100+
/** The field on the child object to aggregate. */
101+
summary_field?: string;
102+
/** Filters to apply to child records before summarizing. */
103+
summary_filters?: any[] | string;
104+
105+
// Auto Number properties
106+
/** The format pattern for auto number fields (e.g. 'INV-{YYYY}-{0000}'). */
107+
auto_number_format?: string;
108+
86109
// UI properties (kept for compatibility, though ObjectQL is a query engine)
87110
/** Implementation hint: Whether this field should be indexed for search. */
88111
searchable?: boolean;

packages/driver-knex/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ export class KnexDriver implements Driver {
277277
case 'json':
278278
case 'object':
279279
case 'array': col = table.json(name); break;
280+
case 'summary': col = table.float(name); break; // Stored calculation result
281+
case 'auto_number': col = table.string(name); break; // Generated string
282+
case 'formula': return; // Virtual field, do not create column
280283
default: col = table.string(name);
281284
}
282285
}

packages/driver-knex/test/schema.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,26 @@ describe('KnexDriver Schema Sync (SQLite)', () => {
161161
const res = await driver.find('percent_test', {});
162162
expect(res[0].completion).toBe(0.85);
163163
});
164+
165+
it('should handle special fields (formula, summary, auto_number)', async () => {
166+
const objects = [{
167+
name: 'special_fields_test',
168+
fields: {
169+
// Formula should NOT create a column
170+
total: { type: 'formula', expression: 'price * qty', data_type: 'number' } as any,
171+
// Summary should create a numeric column
172+
child_count: { type: 'summary', summary_object: 'child_obj', summary_type: 'count' } as any,
173+
// Auto Number should create a string column
174+
invoice_no: { type: 'auto_number', auto_number_format: 'INV-{0000}' } as any
175+
}
176+
}];
177+
178+
await driver.init(objects);
179+
180+
const columns = await knexInstance('special_fields_test').columnInfo();
181+
182+
expect(columns).not.toHaveProperty('total');
183+
expect(columns).toHaveProperty('child_count');
184+
expect(columns).toHaveProperty('invoice_no');
185+
});
164186
});

packages/metadata/src/types.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ export type FieldType =
99
| 'currency'
1010
| 'percent'
1111
| 'boolean'
12-
| 'lookup'
12+
| 'lookup'
1313
| 'master_detail'
1414
| 'password'
15+
| 'formula'
16+
| 'summary'
17+
| 'auto_number'
1518
| 'object'
1619
| 'grid';
1720

@@ -31,6 +34,20 @@ export interface FieldConfig {
3134
scale?: number;
3235
precision?: number;
3336
reference_to?: string;
37+
38+
// Formula
39+
expression?: string;
40+
data_type?: 'text' | 'boolean' | 'date' | 'datetime' | 'number' | 'currency' | 'percent';
41+
42+
// Summary
43+
summary_object?: string;
44+
summary_type?: 'count' | 'sum' | 'min' | 'max' | 'avg';
45+
summary_field?: string;
46+
summary_filters?: any[] | string;
47+
48+
// Auto Number
49+
auto_number_format?: string;
50+
3451
searchable?: boolean;
3552
sortable?: boolean;
3653
index?: boolean;

0 commit comments

Comments
 (0)