Skip to content

Commit 38eede2

Browse files
committed
feat: 优化对象能力模型,增加描述和行业标准功能,增强API权限控制
1 parent 879f51b commit 38eede2

File tree

1 file changed

+94
-41
lines changed

1 file changed

+94
-41
lines changed
Lines changed: 94 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,60 @@
11
import { z } from 'zod';
22
import { FieldSchema } from './field.zod';
3+
import { ValidationRuleSchema } from './validation.zod';
34

45
/**
56
* Capability Flags
67
* Defines what system features are enabled for this object.
8+
*
9+
* Optimized based on industry standards (Salesforce, ServiceNow):
10+
* - Added `activities` (Tasks/Events)
11+
* - Added `mru` (Recent Items)
12+
* - Added `feeds` (Social/Chatter)
13+
* - Grouped API permissions
714
*/
815
export const ObjectCapabilities = z.object({
916
/** Enable history tracking (Audit Trail) */
10-
trackHistory: z.boolean().default(false),
17+
trackHistory: z.boolean().default(false).describe('Enable field history tracking for audit compliance'),
1118

1219
/** Enable global search indexing */
13-
searchable: z.boolean().default(true),
20+
searchable: z.boolean().default(true).describe('Index records for global search'),
1421

1522
/** Enable REST/GraphQL API access */
16-
apiEnabled: z.boolean().default(true),
23+
apiEnabled: z.boolean().default(true).describe('Expose object via automatic APIs'),
1724

1825
/**
1926
* API Supported Operations
20-
* Explicitly whitelist allowed operations.
21-
* If not defined, all standard operations are allowed (if apiEnabled=true).
27+
* Granular control over API exposure.
2228
*/
2329
apiMethods: z.array(z.enum([
24-
'get', 'list',
25-
'create', 'update', 'delete',
26-
'upsert',
27-
'bulkCreate', 'bulkUpdate', 'bulkDelete', 'bulkUpsert',
28-
'aggregate', // count, sum, group by
29-
'history', // audit trail access
30-
'search', // full text search
31-
'restore', // undelete from trash
32-
'purge', // hard delete
33-
'import', // data import
34-
'export', // data export
35-
])).optional().describe('Allowed API operations'),
30+
'get', 'list', // Read
31+
'create', 'update', 'delete', // Write
32+
'upsert', // Idempotent Write
33+
'bulk', // Batch operations
34+
'aggregate', // Analytics (count, sum)
35+
'history', // Audit access
36+
'search', // Search access
37+
'restore', 'purge', // Trash management
38+
'import', 'export', // Data portability
39+
])).optional().describe('Whitelist of allowed API operations'),
3640

37-
/** Enable attachments/files */
38-
files: z.boolean().default(false),
41+
/** Enable standard attachments/files engine */
42+
files: z.boolean().default(false).describe('Enable file attachments and document management'),
3943

40-
/** Enable discussions/chatter */
41-
feedEnabled: z.boolean().default(false),
44+
/** Enable social collaboration (Comments, Mentions, Feeds) */
45+
feeds: z.boolean().default(false).describe('Enable social feed, comments, and mentions (Chatter-like)'),
4246

43-
/** Enable Recycle Bin mechanics */
44-
trash: z.boolean().default(true),
47+
/** Enable standard Activity suite (Tasks, Calendars, Events) */
48+
activities: z.boolean().default(false).describe('Enable standard tasks and events tracking'),
49+
50+
/** Enable Recycle Bin / Soft Delete */
51+
trash: z.boolean().default(true).describe('Enable soft-delete with restore capability'),
52+
53+
/** Enable "Recently Viewed" tracking */
54+
mru: z.boolean().default(true).describe('Track Most Recently Used (MRU) list for users'),
55+
56+
/** Allow cloning records */
57+
clone: z.boolean().default(true).describe('Allow record deep cloning'),
4558
});
4659

4760
/**
@@ -51,38 +64,76 @@ export const IndexSchema = z.object({
5164
name: z.string().optional().describe('Index name'),
5265
fields: z.array(z.string()).describe('Fields included in the index'),
5366
unique: z.boolean().optional().describe('Whether the index is unique'),
67+
type: z.enum(['btree', 'hash', 'gin', 'gist']).optional().describe('Index type (default: btree)'),
68+
});
69+
70+
/**
71+
* Search Configuration
72+
* Defines how this object behaves in search results.
73+
*/
74+
export const SearchConfigSchema = z.object({
75+
fields: z.array(z.string()).describe('Fields to index for full-text search weighting'),
76+
displayFields: z.array(z.string()).optional().describe('Fields to display in search result cards'),
77+
filters: z.array(z.string()).optional().describe('Default filters for search results'),
5478
});
5579

5680
/**
5781
* Base Object Schema Definition
82+
*
83+
* The Blueprint of a Business Object.
84+
* Represents a table, a collection, or a virtual entity.
5885
*/
5986
const ObjectSchemaBase = z.object({
60-
/** Identify */
61-
name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Machine name (snake_case)'),
62-
label: z.string().optional().describe('Singular Label (e.g. "Account")'),
63-
pluralLabel: z.string().optional().describe('Plural Label (e.g. "Accounts")'),
64-
description: z.string().optional().describe('Internal description'),
65-
icon: z.string().optional().describe('Lucide icon name'),
87+
/**
88+
* Identity & Metadata
89+
*/
90+
name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Machine unique key (snake_case). Immutable.'),
91+
label: z.string().optional().describe('Human readable singular label (e.g. "Account")'),
92+
pluralLabel: z.string().optional().describe('Human readable plural label (e.g. "Accounts")'),
93+
description: z.string().optional().describe('Developer documentation / description'),
94+
icon: z.string().optional().describe('Icon name (Lucide/Material) for UI representation'),
95+
96+
/**
97+
* Taxonomy & Organization
98+
*/
99+
tags: z.array(z.string()).optional().describe('Categorization tags (e.g. "sales", "system", "reference")'),
100+
active: z.boolean().default(true).describe('Is the object active and usable'),
101+
isSystem: z.boolean().default(false).describe('Is system object (protected from deletion)'),
102+
abstract: z.boolean().default(false).describe('Is abstract base object (cannot be instantiated)'),
66103

67104
/**
68-
* Storage / Virtualization Config
69-
* Defines where the data actually lives.
105+
* Storage & Virtualization
70106
*/
71-
datasource: z.string().default('default').describe('Target Datasource ID (e.g. "postgres", "salesforce"). references sys_datasource.'),
107+
datasource: z.string().default('default').describe('Target Datasource ID. "default" is the primary DB.'),
72108
tableName: z.string().optional().describe('Physical table/collection name in the target datasource'),
73-
isSystem: z.boolean().default(false).describe('Is system object (protected)'),
74109

75-
/** Fields Definition */
76-
fields: z.record(FieldSchema).describe('Map of field definitions'),
110+
/**
111+
* Data Model
112+
*/
113+
fields: z.record(FieldSchema).describe('Field definitions map'),
114+
indexes: z.array(IndexSchema).optional().describe('Database performance indexes'),
77115

78-
/** Indexes */
79-
indexes: z.array(IndexSchema).optional().describe('Database indexes definition'),
116+
/**
117+
* Logic & Validation (Co-located)
118+
* Best Practice: Define rules close to data.
119+
*/
120+
validations: z.array(ValidationRuleSchema).optional().describe('Object-level validation rules'),
121+
122+
/**
123+
* Display & UI Hints (Data-Layer)
124+
*/
125+
titleFormat: z.string().optional().describe('Title expression (e.g. "{name} - {code}"). Overrides nameField.'),
126+
compactLayout: z.array(z.string()).optional().describe('Primary fields for hover/cards/lookups'),
80127

81-
/** Key Fields */
82-
nameField: z.string().optional().describe('Which field represents the record name/title (usually "name")'),
128+
/**
129+
* Search Engine Config
130+
*/
131+
search: SearchConfigSchema.optional().describe('Search engine configuration'),
83132

84-
/** Features & Capabilities */
85-
enable: ObjectCapabilities.optional().describe('Enabled system capabilities'),
133+
/**
134+
* System Capabilities
135+
*/
136+
enable: ObjectCapabilities.optional().describe('Enabled system features modules'),
86137
});
87138

88139
/**
@@ -93,3 +144,5 @@ export const ObjectSchema = Object.assign(ObjectSchemaBase, {
93144
});
94145

95146
export type ServiceObject = z.infer<typeof ObjectSchemaBase>;
147+
export type ObjectCapabilities = z.infer<typeof ObjectCapabilities>;
148+
export type ObjectIndex = z.infer<typeof IndexSchema>;

0 commit comments

Comments
 (0)