๐ Understanding Optical (Directus) Extensions — Full Detailed Guide With Real Examples
Optical = Directus + Custom Extensions + Flows + Integrations
Extensions เคाเคฐ เคธ्เคคเคฐों เคชเคฐ เคाเคฎ เคเคฐเคคे เคนैं:
-
Hooks → Events listener (เคเคฌ data create/update เคนो)
-
Endpoints → Custom API routes
-
Modules → Custom UI (Admin panel เคฎें custom page)
-
Interfaces / Displays → Custom form fields / data displays
| Event | Meaning |
|---|---|
items.create.before |
New item insert เคนोเคจे เคธे เคชเคนเคฒे |
items.create.after |
Insert เคนोเคจे เคे เคฌाเคฆ |
items.update.after |
Record update เคे เคฌाเคฆ |
items.delete.after |
Delete เคे เคฌाเคฆ |
auth.login.after |
เคिเคธी user เคจे login เคिเคฏा |
files.upload |
เคिเคธी asset เคो upload เคिเคฏा |
What this does:
-
Any update in
user_profile -
Makes a new row inside
PRJ_User_Audit -
Stores timestamp + changes
✔️ Example 2: Auto-Approval Logic (BCA Worker POC)
If worker confidence_score > 80 → auto approve
module.exports = ({ services }) => {
const { ItemsService } = services;
return {
'items.create.after': async (ctx) => {
if (ctx.collection !== 'Worker_Profile') return;
const score = ctx.payload.confidence_score;
const service = new ItemsService('Worker_Profile', {
schema: ctx.schema,
accountability: ctx.accountability
});
if (score >= 80) {
await service.updateOne(ctx.key, {
Status: "Approved"
});
}
}
};
};
Use cases: lightweight API server inside Optical.
-
Passport OCR API
-
MOM WPOL Simulation API
-
BCA Worker Test Result Aggregation API
-
ICA ACK API
-
Health check endpoints
-
Bulk fetch APIs
✔️ Real Example: MOM Work Permit Generator Endpoint
๐ข 3. MODULES — Custom Screens in Admin Panel
Modules = Optical เคे Admin Panel เคฎें new menu + custom UI.
Use cases:
-
BCA Monitoring Dashboard
-
Firm-based Test Score summary
-
Assessor Allocation UI
-
Worker Passport OCR Tools
-
CSO Tools
-
PRJ Tools dashboard
-
MOM simulation dashboard
This automatically creates:
๐ A new menu item in left navigation
๐ Clicking it shows your custom Vue UI (Dashboard.vue)
✔️ Example BCA Dashboard Module
Dashboard.vue contains:
-
Total workers
-
Pass/Fail rate
-
Arrival rate
-
Test Participation
-
Confidence Score (firm-based)
-
Random Audit candidates
This is exactly what we did in BCA demo.
๐ก 4. INTERFACES — Custom Input Field Components
Interface = เคจเคฏी form input type.
Use cases:
-
ICA date selector with YYYYMMDD
-
Passport MRZ scanner input
-
FIN validator input
-
Trade specialization picker
-
Firm-selector popup
-
Auto complete from external API
Structure:
extensions/interfaces/<interface-name>/src/
Example:
export default {
id: 'fin-input',
name: 'FIN Input (Validator)',
type: 'string',
icon: 'badge',
component: () => import('./fin-input.vue')
}
๐ 5. DISPLAYS — Custom Display Component (Read-only)
Display = List view เคฏा detail view เคฎें custom look
Examples:
-
Status badge (green/red/orange)
-
Confidence Score color-coded
-
Passport expiry warning (red)
-
Worker Age calculation
Location:
๐ง How All Extensions Work Together (Real PRJ/BCA Example)
Here’s a real scenario combining all extension types:
Scenario: Passport Upload → Extract → Validate → Update CMS
Step 1 — User uploads passport
(Directus event fires)
Step 2 — Hook files.upload
→ Sends passport to OCR API
→ Gets extracted values
→ Updates User Profile
Step 3 — Endpoint /passport-ocr/run
→ Can be used manually for reprocessing
Step 4 — Module “Passport Tools”
→ Admins can trigger extraction
→ View extracted results
→ Correct values manually
Step 5 — Display “Expiry Warning”
→ If passport_expiry < 1 month → show RED ⚠️
๐ Folder Summary (Everything Together)
extensions/
│
├── hooks/
│ ├── prjourney-user-audit/
│ ├── passport-auto-extract/
│
├── endpoints/
│ ├── passport-ocr/
│ ├── mom-wp-generator/
│ ├── test-result-api/
│
├── modules/
│ ├── bca-dashboard/
│ ├── prj-tools/
│
├── interfaces/
│ ├── fin-input/
│ ├── date-yyyymmdd/
│
└── displays/
├── status-badge/
├── expiry-warning/
| Requirement | Use | Why |
|---|---|---|
| Auto update database fields | Hook | Data event triggered |
| Create custom API | Endpoint | Public/Private programmatic access |
| Create custom UI page | Module | New admin panel screen |
| Create custom form input | Interface | For editors/data entry |
| Display custom-colored values | Display | For lists & details |
Comments
Post a Comment