|
| 1 | +/** |
| 2 | + * Example: Using Namespaced Imports |
| 3 | + * |
| 4 | + * This example demonstrates how to use the new namespaced import style |
| 5 | + * to organize code and prevent naming conflicts. |
| 6 | + */ |
| 7 | + |
| 8 | +// ============================================================================ |
| 9 | +// STYLE 1: Flat Imports (Backward Compatible) |
| 10 | +// ============================================================================ |
| 11 | + |
| 12 | +import { Field, FieldType, ObjectSchema } from '@objectstack/spec'; |
| 13 | + |
| 14 | +// Use directly without namespace |
| 15 | +const field1: Field = { |
| 16 | + name: 'employee_name', |
| 17 | + label: 'Employee Name', |
| 18 | + type: 'text' as FieldType, |
| 19 | + required: true, |
| 20 | +}; |
| 21 | + |
| 22 | +// ============================================================================ |
| 23 | +// STYLE 2: Namespaced Imports (Recommended for New Code) |
| 24 | +// ============================================================================ |
| 25 | + |
| 26 | +import * as Data from '@objectstack/spec/data'; |
| 27 | +import * as UI from '@objectstack/spec/ui'; |
| 28 | +import * as System from '@objectstack/spec/system'; |
| 29 | +import * as AI from '@objectstack/spec/ai'; |
| 30 | + |
| 31 | +// Use with namespace prefix for clarity |
| 32 | +const field2: Data.Field = { |
| 33 | + name: 'task_title', |
| 34 | + label: 'Task Title', |
| 35 | + type: 'text' as Data.FieldType, |
| 36 | + required: true, |
| 37 | +}; |
| 38 | + |
| 39 | +// Define an object using namespaced types |
| 40 | +const taskObject: Data.ServiceObject = { |
| 41 | + name: 'project_task', |
| 42 | + label: 'Task', |
| 43 | + fields: { |
| 44 | + title: field2, |
| 45 | + description: { |
| 46 | + name: 'description', |
| 47 | + label: 'Description', |
| 48 | + type: 'textarea' as Data.FieldType, |
| 49 | + }, |
| 50 | + status: { |
| 51 | + name: 'status', |
| 52 | + label: 'Status', |
| 53 | + type: 'select' as Data.FieldType, |
| 54 | + options: [ |
| 55 | + { value: 'todo', label: 'To Do' }, |
| 56 | + { value: 'in_progress', label: 'In Progress' }, |
| 57 | + { value: 'done', label: 'Done' }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + assignee: { |
| 61 | + name: 'assignee', |
| 62 | + label: 'Assignee', |
| 63 | + type: 'lookup' as Data.FieldType, |
| 64 | + reference: 'system_user', |
| 65 | + }, |
| 66 | + }, |
| 67 | +}; |
| 68 | + |
| 69 | +// Validate using namespaced schema |
| 70 | +const result = Data.ObjectSchema.safeParse(taskObject); |
| 71 | + |
| 72 | +if (result.success) { |
| 73 | + console.log('✓ Object definition is valid'); |
| 74 | +} else { |
| 75 | + console.error('✗ Validation errors:', result.error); |
| 76 | +} |
| 77 | + |
| 78 | +// Define UI components using namespaced types |
| 79 | +const taskView: UI.View = { |
| 80 | + name: 'task_list', |
| 81 | + label: 'Task List', |
| 82 | + object: 'project_task', |
| 83 | + type: 'list', |
| 84 | + listView: { |
| 85 | + type: 'grid', |
| 86 | + columns: ['title', 'status', 'assignee'], |
| 87 | + defaultSort: [{ field: 'created_at', direction: 'desc' }], |
| 88 | + }, |
| 89 | +}; |
| 90 | + |
| 91 | +// Define system configuration using namespaced types |
| 92 | +const adminUser: System.User = { |
| 93 | + id: 'user_admin_001', |
| 94 | + email: 'admin@example.com', |
| 95 | + name: 'System Admin', |
| 96 | + emailVerified: new Date(), |
| 97 | + image: null, |
| 98 | + createdAt: new Date(), |
| 99 | + updatedAt: new Date(), |
| 100 | +}; |
| 101 | + |
| 102 | +// Define AI agent using namespaced types |
| 103 | +const salesAgent: AI.Agent = { |
| 104 | + name: 'sales_assistant', |
| 105 | + label: 'Sales Assistant', |
| 106 | + description: 'AI agent to help with sales tasks', |
| 107 | + model: { |
| 108 | + provider: 'openai' as AI.ModelProvider, |
| 109 | + name: 'gpt-4', |
| 110 | + }, |
| 111 | + tools: [], |
| 112 | + knowledge: [], |
| 113 | + instructions: 'You are a helpful sales assistant.', |
| 114 | +}; |
| 115 | + |
| 116 | +// ============================================================================ |
| 117 | +// BENEFITS OF NAMESPACED IMPORTS |
| 118 | +// ============================================================================ |
| 119 | + |
| 120 | +/** |
| 121 | + * 1. Clear Domain Boundaries |
| 122 | + * - Immediately obvious which protocol a type belongs to |
| 123 | + * - Easier to navigate and understand the codebase |
| 124 | + * |
| 125 | + * 2. No Naming Conflicts |
| 126 | + * - If Data.User and System.User both existed, no confusion |
| 127 | + * - Safe to add new types without worrying about conflicts |
| 128 | + * |
| 129 | + * 3. Better IDE Autocomplete |
| 130 | + * - Type "Data." and see all data protocol types |
| 131 | + * - Discover related types within a namespace |
| 132 | + * |
| 133 | + * 4. Self-Documenting Code |
| 134 | + * - Reading "System.User" is clearer than just "User" |
| 135 | + * - Helps new developers understand the architecture |
| 136 | + */ |
| 137 | + |
| 138 | +export { |
| 139 | + field1, |
| 140 | + field2, |
| 141 | + taskObject, |
| 142 | + taskView, |
| 143 | + adminUser, |
| 144 | + salesAgent, |
| 145 | + result, |
| 146 | +}; |
0 commit comments