|
| 1 | +# Formula Engine Tutorial |
| 2 | + |
| 3 | +This tutorial demonstrates the Formula Engine capabilities in ObjectQL. Formulas are read-only calculated fields that automatically derive their values from other fields, related records, or system variables. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install |
| 9 | +npm run dev |
| 10 | +``` |
| 11 | + |
| 12 | +## What You'll Learn |
| 13 | + |
| 14 | +1. Basic formula expressions (arithmetic, string concatenation) |
| 15 | +2. Conditional logic in formulas |
| 16 | +3. System variables ($today, $current_user) |
| 17 | +4. Complex business logic |
| 18 | +5. Error handling |
| 19 | + |
| 20 | +## Example Objects |
| 21 | + |
| 22 | +### 1. E-commerce Order |
| 23 | + |
| 24 | +Demonstrates: |
| 25 | +- Price calculations with discounts and tax |
| 26 | +- Stock status logic |
| 27 | +- Risk assessment |
| 28 | + |
| 29 | +### 2. CRM Contact |
| 30 | + |
| 31 | +Demonstrates: |
| 32 | +- String concatenation (full name) |
| 33 | +- Relationship traversal |
| 34 | +- User ownership checks |
| 35 | + |
| 36 | +### 3. Project Management |
| 37 | + |
| 38 | +Demonstrates: |
| 39 | +- Date calculations |
| 40 | +- Progress tracking |
| 41 | +- Health scores |
| 42 | + |
| 43 | +## Running the Examples |
| 44 | + |
| 45 | +```bash |
| 46 | +npm run dev |
| 47 | +``` |
| 48 | + |
| 49 | +This will: |
| 50 | +1. Initialize the ObjectQL engine |
| 51 | +2. Register objects with formulas |
| 52 | +3. Create sample records |
| 53 | +4. Query and display formula results |
| 54 | + |
| 55 | +## Key Concepts |
| 56 | + |
| 57 | +### Formula Field Definition |
| 58 | + |
| 59 | +```yaml |
| 60 | +full_name: |
| 61 | + type: formula |
| 62 | + formula: "first_name + ' ' + last_name" |
| 63 | + data_type: text |
| 64 | + label: Full Name |
| 65 | +``` |
| 66 | +
|
| 67 | +### System Variables |
| 68 | +
|
| 69 | +- `$today` - Current date |
| 70 | +- `$now` - Current timestamp |
| 71 | +- `$current_user.id` - Current user ID |
| 72 | +- `$year`, `$month`, `$day` - Date components |
| 73 | + |
| 74 | +### Supported Operations |
| 75 | + |
| 76 | +- Arithmetic: `+`, `-`, `*`, `/`, `%`, `**` |
| 77 | +- Comparison: `===`, `!==`, `>`, `<`, `>=`, `<=` |
| 78 | +- Logical: `&&`, `||`, `!` |
| 79 | +- Conditional: Ternary operator, if/else blocks |
| 80 | +- String methods: `.toUpperCase()`, `.toLowerCase()`, `.trim()`, etc. |
| 81 | +- Math functions: `Math.round()`, `Math.max()`, etc. |
| 82 | + |
| 83 | +## See Also |
| 84 | + |
| 85 | +- [Formula Specification](../../docs/spec/formula.md) |
| 86 | +- [Formulas & Rules Guide](../../docs/guide/formulas-and-rules.md) |
0 commit comments