18 KiB
Hive CE Database Schema Documentation
Overview
This document describes the complete Hive CE database schema for the Retail POS application. The database uses Hive CE (Community Edition) for offline-first local storage with type-safe adapters.
Database Structure
Hive Boxes
The application uses 5 main Hive boxes:
| Box Name | Type | Purpose |
|---|---|---|
products |
Box<ProductModel> |
Store all product data |
categories |
Box<CategoryModel> |
Store all category data |
cart |
Box<CartItemModel> |
Store current cart items |
transactions |
Box<TransactionModel> |
Store completed transactions |
settings |
Box<AppSettingsModel> |
Store app settings |
Type ID Assignments (0-223)
| Model | Type ID | Description |
|---|---|---|
ProductModel |
0 | Product entity |
CategoryModel |
1 | Category entity |
CartItemModel |
2 | Shopping cart item |
TransactionModel |
3 | Completed transaction |
AppSettingsModel |
4 | Application settings |
Model Schemas
1. ProductModel (typeId: 0)
File: lib/features/products/data/models/product_model.dart
Fields
| Field | Type | HiveField | Description | Required |
|---|---|---|---|---|
id |
String | 0 | Unique product identifier (UUID) | Yes |
name |
String | 1 | Product name | Yes |
description |
String | 2 | Product description | Yes |
price |
double | 3 | Product price (USD) | Yes |
imageUrl |
String? | 4 | Product image URL | No |
categoryId |
String | 5 | Associated category ID | Yes |
stockQuantity |
int | 6 | Current stock quantity | Yes |
isAvailable |
bool | 7 | Availability status | Yes |
createdAt |
DateTime | 8 | Creation timestamp | Yes |
updatedAt |
DateTime | 9 | Last update timestamp | Yes |
Methods
copyWith()- Create a copy with updated fieldstoJson()- Convert to JSON mapfromJson()- Create from JSON maptoString()- String representation- Equality operators (==, hashCode)
Example
ProductModel(
id: 'prod_123',
name: 'Wireless Headphones',
description: 'Premium noise-cancelling wireless headphones',
price: 299.99,
imageUrl: 'https://example.com/image.jpg',
categoryId: 'cat_electronics',
stockQuantity: 25,
isAvailable: true,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
)
2. CategoryModel (typeId: 1)
File: lib/features/categories/data/models/category_model.dart
Fields
| Field | Type | HiveField | Description | Required |
|---|---|---|---|---|
id |
String | 0 | Unique category identifier | Yes |
name |
String | 1 | Category name | Yes |
description |
String? | 2 | Category description | No |
iconPath |
String? | 3 | Icon path or Material icon name | No |
color |
String? | 4 | Hex color string (e.g., '#2196F3') | No |
productCount |
int | 5 | Number of products in category | Yes |
createdAt |
DateTime | 6 | Creation timestamp | Yes |
Methods
copyWith()- Create a copy with updated fieldstoJson()- Convert to JSON mapfromJson()- Create from JSON maptoString()- String representation- Equality operators (==, hashCode)
Example
CategoryModel(
id: 'cat_electronics',
name: 'Electronics',
description: 'Electronic devices and accessories',
iconPath: 'devices',
color: '#2196F3',
productCount: 15,
createdAt: DateTime.now(),
)
3. CartItemModel (typeId: 2)
File: lib/features/home/data/models/cart_item_model.dart
Fields
| Field | Type | HiveField | Description | Required |
|---|---|---|---|---|
productId |
String | 0 | Product identifier | Yes |
productName |
String | 1 | Product name (cached) | Yes |
price |
double | 2 | Price at time of adding | Yes |
quantity |
int | 3 | Quantity of items | Yes |
imageUrl |
String? | 4 | Product image URL (cached) | No |
addedAt |
DateTime | 5 | Timestamp when added | Yes |
Computed Properties
lineTotal- Calculated asprice * quantity
Methods
copyWith()- Create a copy with updated fieldstoJson()- Convert to JSON mapfromJson()- Create from JSON maptoString()- String representation- Equality operators (==, hashCode)
Example
CartItemModel(
productId: 'prod_123',
productName: 'Wireless Headphones',
price: 299.99,
quantity: 2,
imageUrl: 'https://example.com/image.jpg',
addedAt: DateTime.now(),
)
// lineTotal = 599.98
4. TransactionModel (typeId: 3)
File: lib/features/home/data/models/transaction_model.dart
Fields
| Field | Type | HiveField | Description | Required |
|---|---|---|---|---|
id |
String | 0 | Unique transaction identifier | Yes |
items |
List | 1 | List of cart items | Yes |
subtotal |
double | 2 | Subtotal (before tax/discount) | Yes |
tax |
double | 3 | Tax amount | Yes |
discount |
double | 4 | Discount amount | Yes |
total |
double | 5 | Total amount | Yes |
completedAt |
DateTime | 6 | Transaction completion time | Yes |
paymentMethod |
String | 7 | Payment method used | Yes |
Computed Properties
totalItems- Total number of items in transaction
Methods
copyWith()- Create a copy with updated fieldstoJson()- Convert to JSON mapfromJson()- Create from JSON maptoString()- String representation- Equality operators (==, hashCode)
Example
TransactionModel(
id: 'txn_123',
items: [cartItem1, cartItem2],
subtotal: 599.98,
tax: 60.00,
discount: 0.00,
total: 659.98,
completedAt: DateTime.now(),
paymentMethod: 'cash',
)
5. AppSettingsModel (typeId: 4)
File: lib/features/settings/data/models/app_settings_model.dart
Fields
| Field | Type | HiveField | Description | Required |
|---|---|---|---|---|
themeModeString |
String | 0 | Theme mode ('light', 'dark', 'system') | Yes |
language |
String | 1 | Language code (e.g., 'en', 'es') | Yes |
currency |
String | 2 | Currency code (e.g., 'USD', 'EUR') | Yes |
taxRate |
double | 3 | Tax rate as decimal (e.g., 0.10 = 10%) | Yes |
storeName |
String | 4 | Store name | Yes |
enableSync |
bool | 5 | Enable automatic sync | Yes |
lastSyncAt |
DateTime? | 6 | Last sync timestamp | No |
Computed Properties
themeMode- ReturnsThemeModeenum from string
Factory Constructors
fromThemeMode()- Create from ThemeMode enumdefaultSettings()- Create with default values
Methods
copyWith()- Create a copy with updated fieldstoJson()- Convert to JSON mapfromJson()- Create from JSON maptoString()- String representation
Example
AppSettingsModel(
themeModeString: 'system',
language: 'en',
currency: 'USD',
taxRate: 0.08,
storeName: 'My Retail Store',
enableSync: true,
lastSyncAt: DateTime.now(),
)
Data Sources
ProductLocalDataSource
File: lib/features/products/data/datasources/product_local_datasource_hive.dart
Methods
getAllProducts()- Get all productsgetProductById(id)- Get product by IDgetProductsByCategory(categoryId)- Get products by categorysaveProducts(products)- Bulk save productssaveProduct(product)- Save single productupdateProduct(product)- Update productdeleteProduct(id)- Delete productdeleteAllProducts()- Clear all productsproductExists(id)- Check if product existsgetAvailableProducts()- Get available products onlygetLowStockProducts(threshold)- Get low stock productssearchProducts(query)- Search products by name/description
CategoryLocalDataSource
File: lib/features/categories/data/datasources/category_local_datasource_hive.dart
Methods
getAllCategories()- Get all categoriesgetCategoryById(id)- Get category by IDsaveCategories(categories)- Bulk save categoriessaveCategory(category)- Save single categoryupdateCategory(category)- Update categorydeleteCategory(id)- Delete categorydeleteAllCategories()- Clear all categoriescategoryExists(id)- Check if category existsupdateProductCount(categoryId, count)- Update product count
CartLocalDataSource
File: lib/features/home/data/datasources/cart_local_datasource.dart
Cart Methods
getCartItems()- Get all cart itemsgetCartItem(productId)- Get specific cart itemaddToCart(item)- Add item to cartupdateCartItem(item)- Update cart itemremoveFromCart(productId)- Remove from cartclearCart()- Clear entire cartisInCart(productId)- Check if item is in cartgetCartTotal()- Calculate cart totalgetCartItemCount()- Get total item count
Transaction Methods
saveTransaction(transaction)- Save completed transactiongetAllTransactions()- Get all transactionsgetTransaction(id)- Get transaction by IDgetTransactionsByDateRange(start, end)- Get transactions by dategetTodayTransactions()- Get today's transactionsgetTotalSales(start, end)- Calculate total salesgetTodaySales()- Calculate today's salesdeleteTransaction(id)- Delete transactionclearAllTransactions()- Clear all transactionsgetTransactionCount()- Get transaction count
SettingsLocalDataSource
File: lib/features/settings/data/datasources/settings_local_datasource_hive.dart
Methods
getSettings()- Get current settingssaveSettings(settings)- Save settingsupdateThemeMode(mode)- Update theme modeupdateLanguage(language)- Update languageupdateLastSyncTime(time)- Update last sync timeupdateCurrency(currency)- Update currencyupdateTaxRate(taxRate)- Update tax rateupdateStoreName(storeName)- Update store nametoggleSync(enable)- Toggle sync on/offresetSettings()- Reset to defaults
Database Initialization
HiveDatabase
File: lib/core/database/hive_database.dart
Singleton class managing all Hive boxes and operations.
Methods
init()- Initialize Hive and open all boxesgetBox<T>(boxName)- Get a specific boxclearAllData()- Clear all data (except settings)clearCart()- Clear cart onlycompactAll()- Compact all boxes (optimize storage)closeAll()- Close all boxesdeleteAll()- Delete all boxes (complete reset)getStatistics()- Get database statisticsisInitialized- Check initialization status
Properties
productsBox- Direct access to products boxcategoriesBox- Direct access to categories boxcartBox- Direct access to cart boxtransactionsBox- Direct access to transactions boxsettingsBox- Direct access to settings box
DatabaseInitializer
File: lib/core/database/database_initializer.dart
Handles database initialization and seeding.
Methods
initialize(seedIfEmpty)- Initialize database and seed if emptyseedDatabase(forceReseed)- Seed database with sample dataresetDatabase()- Clear and reseed databasegetDatabaseStats()- Get database statisticscompactDatabase()- Compact database
Sample Data
Generated Categories (5)
- Electronics - Blue (#2196F3)
- Icon:
devices
- Icon:
- Home Appliances - Green (#4CAF50)
- Icon:
kitchen
- Icon:
- Sports & Fitness - Orange (#FF9800)
- Icon:
fitness_center
- Icon:
- Fashion - Pink (#E91E63)
- Icon:
checkroom
- Icon:
- Books & Media - Purple (#9C27B0)
- Icon:
book
- Icon:
Generated Products (10)
- Wireless Headphones - $299.99 (Electronics)
- Smart Watch - $199.99 (Electronics)
- Laptop Stand - $39.99 (Electronics)
- Coffee Maker - $79.99 (Appliances)
- Blender - $59.99 (Appliances)
- Yoga Mat - $29.99 (Sports)
- Running Shoes - $89.99 (Sports)
- Water Bottle - $24.99 (Sports)
- Leather Backpack - $129.99 (Fashion)
- Sunglasses - $49.99 (Fashion)
Usage Examples
Initialize Database
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final database = HiveDatabase.instance;
final initializer = DatabaseInitializer(database);
await initializer.initialize(seedIfEmpty: true);
runApp(MyApp());
}
Query Products
final dataSource = ProductLocalDataSourceHive(HiveDatabase.instance);
// Get all products
final products = await dataSource.getAllProducts();
// Get by category
final electronics = await dataSource.getProductsByCategory('cat_electronics');
// Search products
final results = await dataSource.searchProducts('headphones');
// Get low stock
final lowStock = await dataSource.getLowStockProducts(10);
Manage Cart
final dataSource = CartLocalDataSource(HiveDatabase.instance);
// Add to cart
await dataSource.addToCart(CartItemModel(
productId: 'prod_123',
productName: 'Wireless Headphones',
price: 299.99,
quantity: 1,
addedAt: DateTime.now(),
));
// Get cart total
final total = await dataSource.getCartTotal();
// Clear cart
await dataSource.clearCart();
Save Transaction
final dataSource = CartLocalDataSource(HiveDatabase.instance);
await dataSource.saveTransaction(TransactionModel(
id: Uuid().v4(),
items: cartItems,
subtotal: 599.98,
tax: 48.00,
discount: 0.0,
total: 647.98,
completedAt: DateTime.now(),
paymentMethod: 'cash',
));
Update Settings
final dataSource = SettingsLocalDataSourceHive(HiveDatabase.instance);
// Update theme
await dataSource.updateThemeMode(ThemeMode.dark);
// Update tax rate
await dataSource.updateTaxRate(0.10); // 10%
// Get settings
final settings = await dataSource.getSettings();
Code Generation
To generate Hive type adapters after modifying models:
flutter pub run build_runner build --delete-conflicting-outputs
File Locations
lib/
core/
constants/
storage_constants.dart # Box names and type IDs
database/
hive_database.dart # Main database class
database_initializer.dart # Initialization logic
seed_data.dart # Sample data generator
features/
products/data/
models/product_model.dart # Product model + adapter
datasources/
product_local_datasource.dart # Interface
product_local_datasource_hive.dart # Hive implementation
categories/data/
models/category_model.dart # Category model + adapter
datasources/
category_local_datasource.dart # Interface
category_local_datasource_hive.dart # Hive implementation
home/data/
models/
cart_item_model.dart # Cart item model + adapter
transaction_model.dart # Transaction model + adapter
datasources/
cart_local_datasource.dart # Cart & transaction operations
settings/data/
models/app_settings_model.dart # Settings model + adapter
datasources/
settings_local_datasource.dart # Interface
settings_local_datasource_hive.dart # Hive implementation
Database Statistics
Use getDatabaseStats() to monitor database usage:
final stats = HiveDatabase.instance.getStatistics();
print(stats);
// Output:
// {
// 'products': 10,
// 'categories': 5,
// 'cartItems': 0,
// 'transactions': 0,
// 'isInitialized': true
// }
Maintenance
Compact Database
Optimize storage and improve performance:
await HiveDatabase.instance.compactAll();
Reset Database
Clear all data and reseed:
final initializer = DatabaseInitializer(HiveDatabase.instance);
await initializer.resetDatabase();
Clear Specific Data
// Clear cart only
await HiveDatabase.instance.clearCart();
// Clear all data except settings
await HiveDatabase.instance.clearAllData();
Best Practices
- Type Safety: Always use type-safe boxes (
Box<T>) - Key Strategy: Use string IDs as keys for easy lookup
- Bulk Operations: Use
putAll()for multiple items - Error Handling: Wrap all operations in try-catch blocks
- Compaction: Regularly compact boxes in production
- Backups: Implement backup strategy for transactions
- Migration: Plan for schema migrations as app evolves
- Testing: Test all database operations thoroughly
Performance Considerations
- Products Box: ~10-1000 items expected
- Categories Box: ~5-50 items expected
- Cart Box: ~0-20 items expected
- Transactions Box: Growing over time (consider archiving)
- Settings Box: Single item only
Optimization Tips
- Use lazy boxes for large datasets
- Implement pagination for transaction history
- Clear old transactions periodically
- Cache frequently accessed queries
- Use RepaintBoundary for grid items
Security Notes
- Sensitive data (prices, transactions) stored unencrypted
- Consider using
HiveAesCipherfor encryption if needed - Validate all data before storage
- Implement proper access controls in production
- Never expose raw Hive boxes to UI layer
Migration Strategy
When schema changes are needed:
- Increment type IDs or use versioning
- Create migration scripts
- Test migration thoroughly
- Backup data before migration
- Provide rollback mechanism
Example migration:
Future<void> migrateToV2() async {
final versionBox = await Hive.openBox('version');
final currentVersion = versionBox.get('schema_version', defaultValue: 1);
if (currentVersion < 2) {
// Perform migration
// Update schema version
await versionBox.put('schema_version', 2);
}
}
Last Updated: 2025-10-10 Schema Version: 1.0.0 Hive CE Version: 2.6.0