Offline-First Backend Implementation
Dokumentasi lengkap implementasi backend untuk offline-first architecture di MStore Backend.π― Quick Links
Database Schema
Atlas HCL schema untuk transactions & conflicts
Service Layer
Business logic & batch sync implementation
Repository Layer
Data access layer dengan GORM
API Endpoints
REST API untuk batch sync & conflict resolution
π Database Schema
Transactions Table (Modified)
Location:third_party/migrations/atlas/schema/05_transactional.my.hcl
Offline Conflicts Table
Location:third_party/migrations/atlas/schema/09_offline_first.my.hcl
ποΈ Architecture
π¦ Go Models
Transaction Model
File:internal/models/transaction.go
Conflict Record Model
File:internal/domains/pos/offline/offline_repository.go
π§ Service Layer
Batch Sync Flow
Duplicate Detection
π Repository Layer
Save Conflict
Get Conflict
π API Endpoints
1. Batch Sync
2. Resolve Conflict
π§ͺ Testing
Unit Test Example
π‘ Best Practices
DO β
- Use database transactions untuk atomic operations
- Implement proper error handling dengan context
- Log semua sync operations untuk audit
- Use unique index pada
offline_reference - Validate input data sebelum processing
- Return detailed error messages
DONβT β
- Jangan skip duplicate detection
- Jangan ignore conflicts
- Jangan hardcode device IDs
- Jangan lupa rollback on error
- Jangan expose sensitive data di logs
π Related Documentation
Flutter Implementation
Complete Flutter + Isar DB guide
Transaction Flow
Transaction state machine
Database Schema
Complete database schema
API Reference
Full API documentation
π Troubleshooting
Duplicate Key Error
Symptom:Duplicate entry for key 'idx_transactions_offline_reference'
Solution: Conflict detection working correctly. Check conflict table.
Transaction Rollback
Symptom: Some transactions synced, some not Solution: Ensure all operations in single DB transaction.JSON Marshal Error
Symptom:json: unsupported type
Solution: Ensure all struct fields are exportable (capitalized).
Implementation Complete! Backend sudah 100% ready untuk production. Test dengan REST Client di
api/pos/offline-sync.http