|
| 1 | +# Export Organization Guide |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +The `@objectstack/spec` package exports many schemas and types. As the API surface grows, there's a risk of: |
| 6 | +1. **Naming conflicts**: Different protocols might want to use similar names |
| 7 | +2. **Poor discoverability**: With 200+ exports in a flat namespace, it's hard to find what you need |
| 8 | +3. **Unclear domain boundaries**: Not obvious which exports belong to which protocol |
| 9 | + |
| 10 | +## Solution: Dual Export Strategy |
| 11 | + |
| 12 | +We now support **both flat and namespaced imports** to give developers flexibility while preventing future naming conflicts. |
| 13 | + |
| 14 | +### 1. Flat Exports (Backward Compatible) |
| 15 | + |
| 16 | +All existing imports continue to work: |
| 17 | + |
| 18 | +```typescript |
| 19 | +import { Field, User, App, Agent } from '@objectstack/spec'; |
| 20 | +``` |
| 21 | + |
| 22 | +**When to use:** |
| 23 | +- Migrating existing code |
| 24 | +- Importing a few specific types |
| 25 | +- Quick prototyping |
| 26 | + |
| 27 | +### 2. Namespaced Exports (Recommended) |
| 28 | + |
| 29 | +Import by protocol domain for better organization: |
| 30 | + |
| 31 | +```typescript |
| 32 | +import * as Data from '@objectstack/spec/data'; |
| 33 | +import * as UI from '@objectstack/spec/ui'; |
| 34 | +import * as System from '@objectstack/spec/system'; |
| 35 | +import * as AI from '@objectstack/spec/ai'; |
| 36 | +import * as API from '@objectstack/spec/api'; |
| 37 | + |
| 38 | +const field: Data.Field = { /* ... */ }; |
| 39 | +const user: System.User = { /* ... */ }; |
| 40 | +``` |
| 41 | + |
| 42 | +**When to use:** |
| 43 | +- New code |
| 44 | +- Working with many types from the same protocol |
| 45 | +- Want to avoid any risk of naming conflicts |
| 46 | +- Want clear domain boundaries in your code |
| 47 | + |
| 48 | +## Protocol Domains |
| 49 | + |
| 50 | +### `@objectstack/spec/data` - Data Protocol |
| 51 | +Core business logic and data modeling: |
| 52 | +- `Object`, `Field`, `FieldType` |
| 53 | +- `Query`, `Filter`, `Sort` |
| 54 | +- `Validation`, `Permission`, `Sharing` |
| 55 | +- `Flow`, `Workflow`, `Trigger` |
| 56 | +- `Dataset`, `Mapping` |
| 57 | + |
| 58 | +### `@objectstack/spec/ui` - UI Protocol |
| 59 | +Presentation and interaction: |
| 60 | +- `App`, `View`, `Page` |
| 61 | +- `Dashboard`, `Report`, `Widget` |
| 62 | +- `Action`, `Theme` |
| 63 | + |
| 64 | +### `@objectstack/spec/system` - System Protocol |
| 65 | +Runtime configuration and security: |
| 66 | +- `Manifest`, `Datasource`, `Driver` |
| 67 | +- `User`, `Account`, `Session` |
| 68 | +- `Organization`, `Role`, `Permission` |
| 69 | +- `Auth`, `Policy`, `Territory` |
| 70 | +- `Webhook`, `License`, `Translation` |
| 71 | +- `Plugin` |
| 72 | + |
| 73 | +### `@objectstack/spec/ai` - AI Protocol |
| 74 | +AI/ML capabilities: |
| 75 | +- `Agent`, `AITool`, `AIKnowledge` |
| 76 | +- `ModelRegistry`, `ModelProvider` |
| 77 | +- `RAGPipeline`, `VectorStore` |
| 78 | +- `NLQRequest`, `QueryIntent` |
| 79 | + |
| 80 | +### `@objectstack/spec/api` - API Protocol |
| 81 | +API contracts and envelopes: |
| 82 | +- `CreateRequest`, `UpdateRequest` |
| 83 | +- `ApiError`, `BaseResponse` |
| 84 | +- `ExportRequest`, `BulkRequest` |
| 85 | + |
| 86 | +## Migration Guide |
| 87 | + |
| 88 | +### For Library Maintainers |
| 89 | + |
| 90 | +You don't need to change anything! All existing exports remain available. However, you may want to adopt namespaced imports in new code: |
| 91 | + |
| 92 | +**Before:** |
| 93 | +```typescript |
| 94 | +import { Field, User, App } from '@objectstack/spec'; |
| 95 | +``` |
| 96 | + |
| 97 | +**After (optional):** |
| 98 | +```typescript |
| 99 | +import * as Data from '@objectstack/spec/data'; |
| 100 | +import * as System from '@objectstack/spec/system'; |
| 101 | +import * as UI from '@objectstack/spec/ui'; |
| 102 | + |
| 103 | +const field: Data.Field = { /* ... */ }; |
| 104 | +const user: System.User = { /* ... */ }; |
| 105 | +const app: UI.App = { /* ... */ }; |
| 106 | +``` |
| 107 | + |
| 108 | +### For Application Developers |
| 109 | + |
| 110 | +Choose the style that fits your needs: |
| 111 | + |
| 112 | +```typescript |
| 113 | +// Option 1: Flat imports (quick and simple) |
| 114 | +import { ObjectSchema, Field } from '@objectstack/spec'; |
| 115 | + |
| 116 | +// Option 2: Namespaced imports (organized and safe) |
| 117 | +import * as Data from '@objectstack/spec/data'; |
| 118 | +const result = Data.ObjectSchema.parse(config); |
| 119 | + |
| 120 | +// Option 3: Mixed approach |
| 121 | +import { Field } from '@objectstack/spec'; |
| 122 | +import * as System from '@objectstack/spec/system'; |
| 123 | +``` |
| 124 | + |
| 125 | +## Implementation Details |
| 126 | + |
| 127 | +### Package.json Exports |
| 128 | + |
| 129 | +The `package.json` now includes export mappings: |
| 130 | + |
| 131 | +```json |
| 132 | +{ |
| 133 | + "exports": { |
| 134 | + ".": "./dist/index.js", |
| 135 | + "./data": "./dist/data/index.js", |
| 136 | + "./ui": "./dist/ui/index.js", |
| 137 | + "./system": "./dist/system/index.js", |
| 138 | + "./ai": "./dist/ai/index.js", |
| 139 | + "./api": "./dist/api/index.js" |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### Barrel Files |
| 145 | + |
| 146 | +Each protocol domain has an `index.ts` barrel file that re-exports all schemas and types from that domain: |
| 147 | + |
| 148 | +- `src/data/index.ts` - Data Protocol |
| 149 | +- `src/ui/index.ts` - UI Protocol |
| 150 | +- `src/system/index.ts` - System Protocol |
| 151 | +- `src/ai/index.ts` - AI Protocol |
| 152 | +- `src/api/index.ts` - API Protocol |
| 153 | + |
| 154 | +The root `src/index.ts` continues to re-export everything for backward compatibility. |
| 155 | + |
| 156 | +## Benefits |
| 157 | + |
| 158 | +1. **Zero Breaking Changes**: All existing code continues to work |
| 159 | +2. **Prevents Conflicts**: Clear namespace boundaries prevent naming collisions |
| 160 | +3. **Better IDE Support**: Autocomplete shows all types in a namespace |
| 161 | +4. **Self-Documenting**: Code clearly shows which protocol is being used |
| 162 | +5. **Scalable**: Can easily add new protocols without conflict risk |
| 163 | + |
| 164 | +## Future Considerations |
| 165 | + |
| 166 | +As the API grows, we can: |
| 167 | +1. Add sub-namespaces (e.g., `@objectstack/spec/data/query` for query-specific types) |
| 168 | +2. Mark certain flat exports as deprecated if conflicts arise |
| 169 | +3. Add convenience exports for commonly-used combinations |
| 170 | + |
| 171 | +## Questions? |
| 172 | + |
| 173 | +See the main README for usage examples, or check the TypeScript definitions in your IDE for available exports in each namespace. |
0 commit comments