Architecture
The Tanqueo Backend API follows a layered architecture pattern with clear separation of concerns between routing, business logic, and data access.System Overview
Technology Stack
Core Technologies
| Layer | Technology | Version | Purpose |
|---|---|---|---|
| Runtime | Node.js | 18+ | Server runtime environment |
| Language | TypeScript | 5.9.3 | Type-safe development |
| Framework | Express.js | 5.2.1 | Web application framework |
| Database | PostgreSQL | via Supabase | Relational data storage |
| Auth | Supabase Auth | 2.89.0 | User authentication & JWT |
| File Upload | Multer | 2.0.2 | Multipart form data handling |
| CORS | cors | 2.8.5 | Cross-origin resource sharing |
Package Configuration
package.json
Application Structure
Directory Layout
Server Initialization
The Express application is initialized insrc/server.ts:
src/server.ts
Middleware Pattern
Middleware Execution Order
1. CORS Middleware
Configured to accept requests from the frontend URL:2. JSON Body Parser
Parses incoming JSON payloads:3. Request Logger
Logs all incoming requests:4. Authentication Middleware
Applied to protected routes:src/middleware/auth.middleware.ts
Route Organization
Route Definition Pattern
All routes follow a consistent pattern:src/routes/tanqueos.routes.ts
API Route Structure
| Base Path | Module | Authentication Required |
|---|---|---|
/api/auth | Authentication | No (login/refresh) |
/api/tanqueos | Fuel tracking | Yes |
/api/engrases | Maintenance | Yes |
/api/documentos | Documents | Yes |
/api/flota | Fleet management | Yes |
/api/mantenimiento | Maintenance plans | Yes |
/api/presupuestos | Budgets | Yes |
/api/catalogos | Master data | Yes |
/api/upload | File uploads | Yes |
/api/saldos-bombas | Pump balances | Yes |
/api/health | Health check | No |
Controller Pattern
Controller Structure
Controllers implement business logic and database interactions:Database Layer
Supabase Client Configuration
src/config/supabase.ts
Row Level Security (RLS)
The authenticated client (req.supabase) automatically applies PostgreSQL RLS policies:
- Users can only access data they’re authorized to see
- Database-level security enforcement
- No additional authorization logic needed in application code
Database Views
Complex queries use PostgreSQL views for performance:tanqueo_relaciones- Joins tanqueos with related tablesengrase_relaciones- Joins engrases with conductors, placas, areasdocumento_relaciones- Joins documents with vehicle data
Request/Response Flow
Error Handling
Consistent error handling across all controllers:Environment Configuration
Required environment variables:.env
Performance Considerations
Caching
Implemented in-memory caching for frequently accessed data:Pagination
All list endpoints support pagination:Database Indexing
Optimized queries rely on PostgreSQL indexes for:- Foreign key relationships
- Date range queries
- Text search fields
Deployment
The application is designed for deployment on platforms like:- Railway (automatic PORT environment variable)
- Heroku
- Render
- AWS / GCP / Azure with Node.js runtime