Skip to main content

Offline-First Backend Implementation

Dokumentasi lengkap implementasi backend untuk offline-first architecture di MStore Backend.

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

Response:

2. Resolve Conflict

Response:

πŸ§ͺ 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

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