|
8 | 8 |
|
9 | 9 | 1. **Zod First:** ALL definitions must start with a **Zod Schema**. We need runtime validation for the CLI and JSON Schema generation for the IDE. |
10 | 10 | 2. **Type Derivation:** TypeScript interfaces must be inferred from Zod (`z.infer<typeof X>`). |
11 | | -3. **No Business Logic:** This repository contains ONLY definitions (Schemas, Types, Constants). No database connections, no UI rendering code. |
| 11 | +3. **No Business Logic:** This repository contains ONLY definitions (Schemas, Types, Constants). |
| 12 | +4. **Naming Convention:** |
| 13 | + * **Configuration Keys (TS Props):** `camelCase` (e.g., `maxLength`, `referenceFilters`). |
| 14 | + * **Machine Names (Data Values):** `snake_case` (e.g., `name: 'first_name'`, `object: 'project_task'`). |
12 | 15 |
|
13 | 16 | --- |
14 | 17 |
|
15 | 18 | ## 📘 1. The Metamodel Standards (Knowledge Base) |
16 | 19 |
|
17 | | -When implementing specific protocols, you must strictly adhere to these structural requirements: |
| 20 | +### **A. DATA PROTOCOL (`src/data/*.zod.ts`)** |
| 21 | +*Core Business Logic & Data Model* |
18 | 22 |
|
19 | | -### **A. Protocol: FIELD (`src/zod/meta/field.zod.ts`)** |
| 23 | +* **Field (`src/data/field.zod.ts`)**: |
| 24 | + * **Type Enum**: `text`, `textarea`, `number`, `boolean`, `select`, `lookup`, `formula`, ... |
| 25 | + * **Props**: `name` (snake_case), `label`, `type`, `multiple` (Array support), `reference` (Target Object). |
| 26 | +* **Object (`src/data/object.zod.ts`)**: |
| 27 | + * **Props**: `name` (snake_case), `label`, `fields` (Map), `enable` (Capabilities: `trackHistory`, `apiEnabled`). |
| 28 | +* **Logic**: `validation.zod.ts` (Rules), `permission.zod.ts` (ACL), `workflow.zod.ts` (Automation). |
20 | 29 |
|
21 | | -The atomic unit of data. |
| 30 | +### **B. UI PROTOCOL (`src/ui/*.zod.ts`)** |
| 31 | +*Presentation & Interaction* |
22 | 32 |
|
23 | | -* **Enum `FieldType`:** |
24 | | -* Basic: `text`, `textarea`, `markdown`, `html`, `password`, `email`. |
25 | | -* Number: `number`, `currency`, `percent`. |
26 | | -* Date: `date`, `datetime`, `time`. |
27 | | -* Logic: `boolean`. |
28 | | -* Choice: `select`, `multiselect` (Requires `options: {label, value}[]`). |
29 | | -* Relational: `lookup`, `master_detail` (Requires `reference`: target object name). |
30 | | -* Calculated: `formula`, `summary` (Requires `expression`). |
31 | | -* Media: `image`, `file`, `avatar`. |
32 | | -* System: `id`, `owner`, `created_at`, `updated_at`. |
| 33 | +* **View (`src/ui/view.zod.ts`)**: |
| 34 | + * **ListView**: `grid`, `kanban`, `calendar`, `gantt`. |
| 35 | + * **FormView**: `simple`, `tabbed`, `wizard`. |
| 36 | +* **App (`src/ui/app.zod.ts`)**: |
| 37 | + * **Navigation**: Structured Menu Tree (`ObjectNavItem`, `DashboardNavItem`). |
| 38 | + * **Branding**: Logo, Colors. |
| 39 | +* **Dashboard (`src/ui/dashboard.zod.ts`)**: Grid layout widgets. |
| 40 | +* **Action (`src/ui/action.zod.ts`)**: Buttons, URL jumps, Screen Flows. |
33 | 41 |
|
| 42 | +### **C. SYSTEM PROTOCOL (`src/system/*.zod.ts`)** |
| 43 | +*Runtime Configuration* |
34 | 44 |
|
35 | | -* **Standard Props:** `name` (required), `label`, `type`, `required`, `defaultValue`, `description` (tooltip), `hidden`, `readonly`. |
36 | | - |
37 | | -### **B. Protocol: ENTITY (`src/zod/meta/entity.zod.ts`)** |
38 | | - |
39 | | -Represents a business object or database table. |
40 | | - |
41 | | -* **Props:** |
42 | | -* `name`: Machine name (snake_case, e.g., `project_task`). |
43 | | -* `label`: Human name (e.g., "Project Task"). |
44 | | -* `description`: Documentation. |
45 | | -* `icon`: Lucide icon name. |
46 | | -* `datasource`: String (default: `'default'`). |
47 | | -* `dbName`: Optional physical table name override. |
48 | | -* `fields`: A Record/Map of `FieldSchema`. |
49 | | -* `indexes`: Definition of database indexes. |
50 | | - |
51 | | - |
52 | | - |
53 | | -### **C. Protocol: VIEW/LAYOUT (`src/zod/meta/view.zod.ts`)** |
54 | | - |
55 | | -Defines how the entity is presented in ObjectUI. |
56 | | - |
57 | | -* **ListView:** `columns` (field names), `sort`, `filter`, `searchable_fields`. |
58 | | -* **FormView:** |
59 | | -* `layout`: `'simple' | 'tabbed' | 'wizard'`. |
60 | | -* `groups`: Array of `{ label: string, columns: 1|2, fields: string[] }`. |
61 | | - |
62 | | - |
63 | | - |
64 | | -### **D. Protocol: MANIFEST (`src/zod/bundle/manifest.zod.ts`)** |
65 | | - |
66 | | -The `objectstack.config.ts` definition for Plugins/Apps. |
67 | | - |
68 | | -* **Props:** |
69 | | -* `id`: Reverse domain (e.g., `com.objectstack.crm`). |
70 | | -* `version`: SemVer string. |
71 | | -* `type`: `'app' | 'plugin' | 'driver'`. |
72 | | -* `permissions`: Array of permission strings (e.g., `['entity.read.customer']`). |
73 | | -* `menus`: Recursive structure `{ label, path, icon, children[] }`. |
74 | | -* `entities`: Glob patterns (e.g., `['./src/schemas/*.gql']`). |
75 | | - |
76 | | - |
77 | | - |
78 | | -### **E. Protocol: RUNTIME (`src/types/runtime/*.ts`)** |
79 | | - |
80 | | -*Note: These are pure TS interfaces, usually not Zod.* |
81 | | - |
82 | | -* **Plugin:** `onInstall`, `onEnable`, `onDisable`. |
83 | | -* **Context:** `PluginContext` exposing `ql` (ObjectQL) and `os` (ObjectOS). |
| 45 | +* **Manifest (`src/system/manifest.zod.ts`)**: Package definition (`objectstack.config.ts`). |
| 46 | +* **Translation (`src/system/translation.zod.ts`)**: Internationalization (i18n). |
84 | 47 |
|
85 | 48 | --- |
86 | 49 |
|
87 | 50 | ## 🛠️ 2. Coding Patterns |
88 | 51 |
|
89 | | -### **Zod Implementation Pattern** |
90 | | - |
91 | | -Always use `z.describe()` to ensure the generated JSON Schema has documentation. |
| 52 | +### **Naming Convention Example** |
92 | 53 |
|
93 | 54 | ```typescript |
94 | | -import { z } from 'zod'; |
95 | | - |
96 | | -// 1. Define Sub-schemas |
97 | | -const SelectOption = z.object({ |
98 | | - label: z.string(), |
99 | | - value: z.string() |
100 | | -}); |
| 55 | +export const FieldSchema = z.object({ |
| 56 | + // CONFIGURATION KEY -> camelCase |
| 57 | + maxLength: z.number().optional(), |
| 58 | + defaultValue: z.any().optional(), |
101 | 59 |
|
102 | | -// 2. Define Main Schema |
103 | | -export const MySchema = z.object({ |
104 | | - /** The unique machine name */ |
105 | | - name: z.string().regex(/^[a-z_]+$/).describe("Machine name (snake_case)"), |
106 | | - |
107 | | - /** Configuration options */ |
108 | | - options: z.array(SelectOption).optional() |
| 60 | + // SYSTEM IENTIFIER RULES -> snake_case |
| 61 | + name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Machine name (snake_case)'), |
109 | 62 | }); |
110 | | - |
111 | | -// 3. Export Type |
112 | | -export type MyType = z.infer<typeof MySchema>; |
113 | | - |
114 | 63 | ``` |
115 | 64 |
|
116 | | -### **File Structure** |
| 65 | +### **Directory Structure** |
117 | 66 |
|
118 | | -* `src/zod/meta/`: Metamodel schemas (Field, Entity, View). |
119 | | -* `src/zod/bundle/`: Packaging schemas (Manifest). |
120 | | -* `src/types/runtime/`: Runtime-only interfaces. |
121 | | -* `src/constants/`: Shared constants (e.g., reserved field names). |
| 67 | +* `packages/spec/src/data/`: ObjectQL (Object, Field, Validation). |
| 68 | +* `packages/spec/src/ui/`: ObjectUI (App, View, Action). |
| 69 | +* `packages/spec/src/system/`: ObjectOS (Manifest, Config). |
122 | 70 |
|
123 | 71 | --- |
124 | 72 |
|
125 | 73 | ## 🤖 3. Interaction Shortcuts |
126 | 74 |
|
127 | | -When the user gives these commands, execute the corresponding task: |
128 | | - |
129 | | -* **"Create Field Protocol"** → Implement `src/zod/meta/field.zod.ts` covering all FieldTypes and their specific props (options, reference). |
130 | | -* **"Create Entity Protocol"** → Implement `src/zod/meta/entity.zod.ts` importing the Field schema. |
131 | | -* **"Create View Protocol"** → Implement `src/zod/meta/view.zod.ts` for List and Form layouts. |
132 | | -* **"Create Manifest Protocol"** → Implement `src/zod/bundle/manifest.zod.ts`. |
133 | | -* **"Create Build Script"** → Write `scripts/build-schema.ts` to convert Zod schemas to `manifest.schema.json`. |
| 75 | +* **"Create Field Protocol"** → Implement `src/data/field.zod.ts`. |
| 76 | +* **"Create Object Protocol"** → Implement `src/data/object.zod.ts`. |
| 77 | +* **"Create UI Protocol"** → Implement `src/ui/view.zod.ts`. |
| 78 | +* **"Create App Protocol"** → Implement `src/ui/app.zod.ts`. |
0 commit comments