Appearance
Data Persistence: Microtec POS
This document explains the data persistence strategy of Microtec POS, an offline-first application built with Flutter.
1. Local Database: ObjectBox
Microtec POS uses ObjectBox (a high-performance NoSQL database for Flutter/Dart) for all local data storage. This ensures high-speed operations and complete offline functionality.
Core Persistence Concepts
- Offline-First: All writes (sales, shifts, customer updates) occur locally first.
- Data Transfer Objects (DTOs): Reside in the
lib/infralayer. These objects (e.g.,OrderModelDto) are annotated with@Entity()for ObjectBox. - Interface Inversion: The
lib/infralayer implements repository interfaces defined inlib/domain.
Entity Relationships in ObjectBox
- ToOne/ToMany Relations: Used to model complex business entities. For example:
OrderModelDtohas aToMany<OrderItemDto>relation.OrderModelDtohas aToOne<UserDto>for tracking who opened/closed it.
- JSON Serialization: Some deeply nested or highly dynamic objects (like pricing logic or complex modifiers) are stored as JSON strings in ObjectBox properties for flexibility.
2. Repository Pattern Implementation
The persistence logic is encapsulated in repository implementations in lib/infra.
OrderModelDBOperationImpl
The primary implementation for database operations related to orders.
- Transactions: Uses
_store.runInTransactionto ensure atomicity when saving complex order trees (Order + Items + Payments). - Search & Filtering: Utilizes
QueryBuilderandCondition(viaOrderModelRepoDBQuery) to handle advanced filtering by date range, status, and custom search queries. - Pagination: Implemented via
offsetandlimitinQueryBuilderto maintain UI performance with thousands of transactions.
Key Data Stores
- Order Store: Tracks all transactions, returns, and reservations.
- Product Store: Local cache of the product catalog (synced from the cloud).
- Session Store: Manages shift data and cash drawer reconciliation.
- Counter Store: Maintains local invoice and order numbering to prevent gaps when offline.
3. Data Integrity & Maintenance
- Automatic Numbering: The
OrderCounterDtotracks the last used invoice number per device and business date, ensuring unique IDs across the entire branch. - Sync Status Tracking: Every entity has a
SyncStatusproperty (e.g.,notSynced,synced,error) used by the background synchronization manager. - Data Pruning: The system includes logic to remove old synced orders and sessions to manage local storage usage (found in
removeOldOrdersByUidS).
For behavioral logic and synchronization details, see SYNC_STRATEGY.md.