|
| 1 | +# Building a Micro-CRMSystem |
| 2 | + |
| 3 | +> **Prerequisites**: Completed [Build Your First App](./task-manager). |
| 4 | +
|
| 5 | +In this tutorial, we will explore **Relationships**, **Validation**, and **Hooks** by building a minimal CRM (Customer Relationship Management) system. |
| 6 | + |
| 7 | +## 1. Define the Objects |
| 8 | + |
| 9 | +We need two objects: `account` (Companies) and `contact` (People). |
| 10 | + |
| 11 | +Create `account.object.yml`: |
| 12 | +```yaml |
| 13 | +name: account |
| 14 | +label: Account |
| 15 | +fields: |
| 16 | + name: |
| 17 | + type: text |
| 18 | + required: true |
| 19 | + searchable: true |
| 20 | + industry: |
| 21 | + type: select |
| 22 | + options: |
| 23 | + - technology |
| 24 | + - finance |
| 25 | + - retail |
| 26 | + annual_revenue: |
| 27 | + type: currency |
| 28 | + owner: |
| 29 | + type: lookup |
| 30 | + reference_to: users |
| 31 | +``` |
| 32 | +
|
| 33 | +Create `contact.object.yml`: |
| 34 | +```yaml |
| 35 | +name: contact |
| 36 | +label: Contact |
| 37 | +fields: |
| 38 | + first_name: |
| 39 | + type: text |
| 40 | + required: true |
| 41 | + last_name: |
| 42 | + type: text |
| 43 | + required: true |
| 44 | + email: |
| 45 | + type: text |
| 46 | + required: true |
| 47 | + account: |
| 48 | + type: lookup |
| 49 | + reference_to: account # <--- Relationship |
| 50 | + required: true |
| 51 | +``` |
| 52 | + |
| 53 | +## 2. Add Validation |
| 54 | + |
| 55 | +We want to ensure that `annual_revenue` is never negative. |
| 56 | + |
| 57 | +In `account.object.yml`, add a validation rule: |
| 58 | + |
| 59 | +```yaml |
| 60 | +validation_rules: |
| 61 | + positive_revenue: |
| 62 | + expression: "annual_revenue >= 0" |
| 63 | + message: "Annual revenue cannot be negative" |
| 64 | +``` |
| 65 | + |
| 66 | +> **Note**: ObjectQL supports expression-based validation directly in the metadata. |
| 67 | + |
| 68 | +## 3. Implement Business Logic (Hooks) |
| 69 | + |
| 70 | +We want to automatically set the `full_name` of a contact whenever it is created or updated. |
| 71 | + |
| 72 | +Create or update your server entry point (e.g., `index.ts`): |
| 73 | + |
| 74 | +```typescript |
| 75 | +// ... app initialization ... |
| 76 | +
|
| 77 | +// Register a trigger |
| 78 | +app.on('beforeCreate', 'contact', async ({ data }) => { |
| 79 | + if (data) { |
| 80 | + data.full_name = `${data.first_name} ${data.last_name}`; |
| 81 | + } |
| 82 | +}); |
| 83 | + |
| 84 | +app.on('beforeUpdate', 'contact', async ({ data }) => { |
| 85 | + if (data && (data.first_name || data.last_name)) { |
| 86 | + // Note: In a real app, you might need to fetch the existing first/last name |
| 87 | + // if only one is being updated, but for simplicity: |
| 88 | + data.full_name = `${data.first_name} ${data.last_name}`; |
| 89 | + } |
| 90 | +}); |
| 91 | +``` |
| 92 | + |
| 93 | +Don't forget to add the `full_name` field to `contact.object.yml`: |
| 94 | +```yaml |
| 95 | + full_name: |
| 96 | + type: text |
| 97 | + readonly: true # Prevent manual edits via API |
| 98 | +``` |
| 99 | +
|
| 100 | +## 4. Query with Relationships |
| 101 | +
|
| 102 | +Now start your server and try querying contacts with their account details included. |
| 103 | +
|
| 104 | +**Request:** |
| 105 | +```bash |
| 106 | +curl -G http://localhost:3000/api/data/contact \ |
| 107 | + --data-urlencode 'filters=[["email", "contains", "@example.com"]]' \ |
| 108 | + --data-urlencode 'expand=["account"]' |
| 109 | +``` |
| 110 | + |
| 111 | +**ObjectQL automatically resolves the lookup:** |
| 112 | + |
| 113 | +```json |
| 114 | +{ |
| 115 | + "value": [ |
| 116 | + { |
| 117 | + "_id": "contact_123", |
| 118 | + "first_name": "Alice", |
| 119 | + "full_name": "Alice Smith", |
| 120 | + "account": { |
| 121 | + "_id": "acc_456", |
| 122 | + "name": "Tech Corp", |
| 123 | + "industry": "technology" |
| 124 | + } |
| 125 | + } |
| 126 | + ] |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## Summary |
| 131 | + |
| 132 | +You have just built a relational data system with business logic in minutes. |
| 133 | +- **Relationships**: Defined via `reference_to`. |
| 134 | +- **Validation**: Declarative rules in YAML. |
| 135 | +- **Logic**: TypeScript hooks for dynamic behavior. |
0 commit comments