Architecture Document Runtime - osama1998H/Moca GitHub Wiki
Document Runtime
Document interface, DynamicDoc, 16-event lifecycle, naming engine, and validation.
The Document Runtime is responsible for creating, reading, updating, and deleting documents. Every document operation passes through a lifecycle engine that fires hooks at each stage.
Document Interface
All documents implement the Document interface. The default implementation is DynamicDoc -- a map-backed document that adapts to any MetaType at runtime.
16-Event Lifecycle
Document operations fire events in a defined order:
Insert
BeforeInsert -> Validate -> BeforeSave -> (DB INSERT) -> AfterInsert -> AfterSave
Update
BeforeUpdate -> Validate -> BeforeSave -> (DB UPDATE) -> AfterUpdate -> AfterSave
Delete
BeforeDelete -> (DB DELETE) -> AfterDelete -> OnTrash
Submit / Cancel / Amend
BeforeSubmit -> OnSubmit -> AfterSubmit
BeforeCancel -> OnCancel -> AfterCancel
Naming Engine
Six strategies for generating document name (primary key):
| Strategy | Example | Use Case |
|---|---|---|
| AutoIncrement | 1, 2, 3 |
Simple sequential IDs |
| Pattern | INV-2025-00001 |
Formatted sequences with prefixes |
| ByField | [email protected] |
Use a field value as the name |
| ByHash | a1b2c3d4 |
Short hash of field values |
| UUID | 550e8400-e29b... |
Globally unique identifiers |
| Custom | (hook-defined) | App-defined naming logic |
Validation
Before any write, the runtime validates:
- Required fields are present
- Type coercion (string to int, etc.)
- Regex patterns
- Min/max constraints
- Unique field checks
- Link field target existence