ποΈ Backend Architecture
The backend is built on AdonisJS, a robust Node.js web framework that follows the MVC (Model-View-Controller) pattern. We rigorously apply the Service-Repository pattern to keep controllers thin and logic testable.
Directory Structureβ
app/
βββ Controllers/ # Request Handling (Input Validation, HTTP Responses)
βββ Models/ # Lucid ORM Definitions (Database Schema)
βββ Services/ # Business Logic (The core intelligence)
βββ Validators/ # Data validation rules
βββ Websockets/ # Real-time event handlers
βββ Tasks/ # Planned tasks (Cron jobs)
Core Design Patternsβ
1. Service Layerβ
We do not put business logic in Controllers. Instead, we use dedicated Services.
CameraService: Manages camera provisioning, connection status checks, and configuration updates.AlertService: Handles the lifecycle of an alert (Trigger -> Notify Users -> Acknowledge -> Archive).StatsService: Aggregates time-series data for dashboard graphs (e.g., "Activity Over Time").
2. Real-Time Communicationβ
We use Websockets to obtain sub-second latency for critical alerts.
| Channel | Event | Payload | Description |
|---|---|---|---|
cameras | status:update | { id, status } | Camera going Online/offline |
alerts | new:alert | { type, severity } | Critical user notification |
stream | frame | Binary Blob | Live preview (MJPEG over socket) |
Database Schema (MySQL)β
The database is normalized to ensure data integrity.
- Users
1:NStables - Stables
1:NBoxes - Boxes
1:1Cameras - Cameras
1:NAlerts
Note: We use Lucid ORM Migrations to manage schema changes versioning. Always run
node ace migration:runafter pulling updates.