14 KiB
Hive CE Data Models - Completion Summary
✅ All Models Created Successfully!
A total of 25 Hive CE data models have been created for the Worker mobile app, covering all features from the database schema.
📊 Created Models by Feature
1. Authentication (2 models)
- ✅
user_model.dart- Type ID: 0- Maps to
userstable - Includes: user info, loyalty tier, points, company info, referral
- Maps to
- ✅
user_session_model.dart- Type ID: 1- Maps to
user_sessionstable - Includes: session management, device info, tokens
- Maps to
2. Products (2 models)
- ✅
product_model.dart- Type ID: 2- Maps to
productstable - Includes: product details, images, specifications, pricing
- Maps to
- ✅
stock_level_model.dart- Type ID: 3- Maps to
stock_levelstable - Includes: inventory tracking, warehouse info
- Maps to
3. Cart (2 models)
- ✅
cart_model.dart- Type ID: 4- Maps to
cartstable - Includes: cart totals, sync status
- Maps to
- ✅
cart_item_model.dart- Type ID: 5- Maps to
cart_itemstable - Includes: product items, quantities, prices
- Maps to
4. Orders (4 models)
- ✅
order_model.dart- Type ID: 6- Maps to
orderstable - Includes: order details, status, addresses, delivery
- Maps to
- ✅
order_item_model.dart- Type ID: 7- Maps to
order_itemstable - Includes: line items, discounts
- Maps to
- ✅
invoice_model.dart- Type ID: 8- Maps to
invoicestable - Includes: invoice details, payment status, amounts
- Maps to
- ✅
payment_line_model.dart- Type ID: 9- Maps to
payment_linestable - Includes: payment records, methods, receipts
- Maps to
5. Loyalty (4 models)
- ✅
loyalty_point_entry_model.dart- Type ID: 10- Maps to
loyalty_point_entriestable - Includes: points transactions, balance, complaints
- Maps to
- ✅
gift_catalog_model.dart- Type ID: 11- Maps to
gift_catalogtable - Includes: available rewards, points cost
- Maps to
- ✅
redeemed_gift_model.dart- Type ID: 12- Maps to
redeemed_giftstable - Includes: user gifts, vouchers, QR codes
- Maps to
- ✅
points_record_model.dart- Type ID: 13- Maps to
points_recordstable - Includes: invoice submissions for points
- Maps to
6. Projects (2 models)
- ✅
project_submission_model.dart- Type ID: 14- Maps to
project_submissionstable - Includes: project photos, review status
- Maps to
- ✅
design_request_model.dart- Type ID: 15- Maps to
design_requeststable - Includes: design requirements, assignments
- Maps to
7. Quotes (2 models)
- ✅
quote_model.dart- Type ID: 16- Maps to
quotestable - Includes: quotation details, validity, conversion
- Maps to
- ✅
quote_item_model.dart- Type ID: 17- Maps to
quote_itemstable - Includes: quoted products, negotiated prices
- Maps to
8. Chat (2 models)
- ✅
chat_room_model.dart- Type ID: 18- Maps to
chat_roomstable - Includes: room info, participants, related entities
- Maps to
- ✅
message_model.dart- Type ID: 19- Maps to
chat_messagestable - Includes: message content, attachments, read status
- Maps to
9. Notifications (1 model)
- ✅
notification_model.dart- Type ID: 20- Maps to
notificationstable - Includes: notification type, data, read status
- Maps to
10. Showrooms (2 models)
- ✅
showroom_model.dart- Type ID: 21- Maps to
showroomstable - Includes: showroom details, images, 360 view
- Maps to
- ✅
showroom_product_model.dart- Type ID: 22- Maps to
showroom_productstable - Includes: products used in showrooms
- Maps to
11. Account (2 models)
- ✅
payment_reminder_model.dart- Type ID: 23- Maps to
payment_reminderstable - Includes: reminder scheduling, status
- Maps to
- ✅
audit_log_model.dart- Type ID: 24- Maps to
audit_logstable - Includes: user actions, changes tracking
- Maps to
🎯 Enum Types Created (21 enums)
All enum types are defined in /Users/ssg/project/worker/lib/core/database/models/enums.dart:
- UserRole (Type ID: 30)
- UserStatus (Type ID: 31)
- LoyaltyTier (Type ID: 32)
- OrderStatus (Type ID: 33)
- InvoiceType (Type ID: 34)
- InvoiceStatus (Type ID: 35)
- PaymentMethod (Type ID: 36)
- PaymentStatus (Type ID: 37)
- EntryType (Type ID: 38)
- EntrySource (Type ID: 39)
- ComplaintStatus (Type ID: 40)
- GiftCategory (Type ID: 41)
- GiftStatus (Type ID: 42)
- PointsStatus (Type ID: 43)
- ProjectType (Type ID: 44)
- SubmissionStatus (Type ID: 45)
- DesignStatus (Type ID: 46)
- QuoteStatus (Type ID: 47)
- RoomType (Type ID: 48)
- ContentType (Type ID: 49)
- ReminderType (Type ID: 50)
📦 Model Features
Each model includes:
- ✅
@HiveTypeannotation with unique Type ID - ✅
@HiveFieldannotations for all fields - ✅
fromJson()factory constructor for API deserialization - ✅
toJson()method for API serialization - ✅ Helper methods for JSONB fields (get as Map/List)
- ✅ Computed properties and validation
- ✅
copyWith()method for immutability - ✅
toString()override - ✅ Equality operators (
==andhashCode) - ✅ Comprehensive documentation
🚀 Next Steps
1. Generate Type Adapters
Run the Hive code generator to create .g.dart files:
cd /Users/ssg/project/worker
dart run build_runner build --delete-conflicting-outputs
This will generate adapter files for all models and enums.
2. Register Adapters
Create or update Hive initialization file (e.g., lib/core/database/hive_service.dart):
import 'package:hive_ce/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
// Import all generated adapters
import 'package:worker/core/database/models/enums.dart';
import 'package:worker/features/auth/data/models/user_model.dart';
import 'package:worker/features/auth/data/models/user_session_model.dart';
// ... import all other models
class HiveService {
static Future<void> init() async {
await Hive.initFlutter();
// Register all enum adapters
Hive.registerAdapter(UserRoleAdapter());
Hive.registerAdapter(UserStatusAdapter());
Hive.registerAdapter(LoyaltyTierAdapter());
Hive.registerAdapter(OrderStatusAdapter());
Hive.registerAdapter(InvoiceTypeAdapter());
Hive.registerAdapter(InvoiceStatusAdapter());
Hive.registerAdapter(PaymentMethodAdapter());
Hive.registerAdapter(PaymentStatusAdapter());
Hive.registerAdapter(EntryTypeAdapter());
Hive.registerAdapter(EntrySourceAdapter());
Hive.registerAdapter(ComplaintStatusAdapter());
Hive.registerAdapter(GiftCategoryAdapter());
Hive.registerAdapter(GiftStatusAdapter());
Hive.registerAdapter(PointsStatusAdapter());
Hive.registerAdapter(ProjectTypeAdapter());
Hive.registerAdapter(SubmissionStatusAdapter());
Hive.registerAdapter(DesignStatusAdapter());
Hive.registerAdapter(QuoteStatusAdapter());
Hive.registerAdapter(RoomTypeAdapter());
Hive.registerAdapter(ContentTypeAdapter());
Hive.registerAdapter(ReminderTypeAdapter());
// Register all model adapters
Hive.registerAdapter(UserModelAdapter());
Hive.registerAdapter(UserSessionModelAdapter());
Hive.registerAdapter(ProductModelAdapter());
Hive.registerAdapter(StockLevelModelAdapter());
Hive.registerAdapter(CartModelAdapter());
Hive.registerAdapter(CartItemModelAdapter());
Hive.registerAdapter(OrderModelAdapter());
Hive.registerAdapter(OrderItemModelAdapter());
Hive.registerAdapter(InvoiceModelAdapter());
Hive.registerAdapter(PaymentLineModelAdapter());
Hive.registerAdapter(LoyaltyPointEntryModelAdapter());
Hive.registerAdapter(GiftCatalogModelAdapter());
Hive.registerAdapter(RedeemedGiftModelAdapter());
Hive.registerAdapter(PointsRecordModelAdapter());
Hive.registerAdapter(ProjectSubmissionModelAdapter());
Hive.registerAdapter(DesignRequestModelAdapter());
Hive.registerAdapter(QuoteModelAdapter());
Hive.registerAdapter(QuoteItemModelAdapter());
Hive.registerAdapter(ChatRoomModelAdapter());
Hive.registerAdapter(MessageModelAdapter());
Hive.registerAdapter(NotificationModelAdapter());
Hive.registerAdapter(ShowroomModelAdapter());
Hive.registerAdapter(ShowroomProductModelAdapter());
Hive.registerAdapter(PaymentReminderModelAdapter());
Hive.registerAdapter(AuditLogModelAdapter());
// Open boxes
await Hive.openBox(HiveBoxNames.userBox);
await Hive.openBox(HiveBoxNames.productBox);
await Hive.openBox(HiveBoxNames.cartBox);
await Hive.openBox(HiveBoxNames.orderBox);
await Hive.openBox(HiveBoxNames.loyaltyBox);
await Hive.openBox(HiveBoxNames.projectBox);
await Hive.openBox(HiveBoxNames.notificationBox);
// ... open all other boxes
}
}
3. Create Datasources
Implement local datasources using these models:
// Example: lib/features/auth/data/datasources/auth_local_datasource.dart
class AuthLocalDataSource {
final Box userBox;
Future<void> cacheUser(UserModel user) async {
await userBox.put(HiveKeys.currentUser, user);
}
UserModel? getCachedUser() {
return userBox.get(HiveKeys.currentUser) as UserModel?;
}
}
4. Update Repository Implementations
Use models in repository implementations for caching:
class ProductRepositoryImpl implements ProductRepository {
final ProductRemoteDataSource remoteDataSource;
final ProductLocalDataSource localDataSource;
@override
Future<List<Product>> getProducts() async {
try {
// Try to fetch from API
final products = await remoteDataSource.getProducts();
// Cache locally
await localDataSource.cacheProducts(products);
return products;
} catch (e) {
// Return cached data on error
return localDataSource.getCachedProducts();
}
}
}
📁 File Structure
lib/features/
├── auth/data/models/
│ ├── user_model.dart ✅
│ ├── user_model.g.dart (generated)
│ ├── user_session_model.dart ✅
│ └── user_session_model.g.dart (generated)
├── products/data/models/
│ ├── product_model.dart ✅
│ ├── product_model.g.dart (generated)
│ ├── stock_level_model.dart ✅
│ └── stock_level_model.g.dart (generated)
├── cart/data/models/
│ ├── cart_model.dart ✅
│ ├── cart_model.g.dart (generated)
│ ├── cart_item_model.dart ✅
│ └── cart_item_model.g.dart (generated)
├── orders/data/models/
│ ├── order_model.dart ✅
│ ├── order_model.g.dart (generated)
│ ├── order_item_model.dart ✅
│ ├── order_item_model.g.dart (generated)
│ ├── invoice_model.dart ✅
│ ├── invoice_model.g.dart (generated)
│ ├── payment_line_model.dart ✅
│ └── payment_line_model.g.dart (generated)
├── loyalty/data/models/
│ ├── loyalty_point_entry_model.dart ✅
│ ├── loyalty_point_entry_model.g.dart (generated)
│ ├── gift_catalog_model.dart ✅
│ ├── gift_catalog_model.g.dart (generated)
│ ├── redeemed_gift_model.dart ✅
│ ├── redeemed_gift_model.g.dart (generated)
│ ├── points_record_model.dart ✅
│ └── points_record_model.g.dart (generated)
├── projects/data/models/
│ ├── project_submission_model.dart ✅
│ ├── project_submission_model.g.dart (generated)
│ ├── design_request_model.dart ✅
│ └── design_request_model.g.dart (generated)
├── quotes/data/models/
│ ├── quote_model.dart ✅
│ ├── quote_model.g.dart (generated)
│ ├── quote_item_model.dart ✅
│ └── quote_item_model.g.dart (generated)
├── chat/data/models/
│ ├── chat_room_model.dart ✅
│ ├── chat_room_model.g.dart (generated)
│ ├── message_model.dart ✅
│ └── message_model.g.dart (generated)
├── notifications/data/models/
│ ├── notification_model.dart ✅
│ └── notification_model.g.dart (generated)
├── showrooms/data/models/
│ ├── showroom_model.dart ✅
│ ├── showroom_model.g.dart (generated)
│ ├── showroom_product_model.dart ✅
│ └── showroom_product_model.g.dart (generated)
└── account/data/models/
├── payment_reminder_model.dart ✅
├── payment_reminder_model.g.dart (generated)
├── audit_log_model.dart ✅
└── audit_log_model.g.dart (generated)
⚠️ Important Notes
Type ID Management
- All Type IDs are unique across the app (0-24 for models, 30-50 for enums)
- Never change a Type ID once assigned - it will break existing cached data
- Type IDs are centrally managed in
/Users/ssg/project/worker/lib/core/constants/storage_constants.dart
JSONB Field Handling
- All JSONB fields from database are stored as JSON-encoded strings in Hive
- Helper methods provide easy access to parsed data (e.g.,
companyInfoMap,participantsList) - Always use try-catch when decoding JSON fields
DateTime Support
- Hive CE natively supports DateTime
- No need for custom serialization
- Use
DateTime.parse()for JSON andtoIso8601String()for API
Best Practices
- Always extend
HiveObjectfor automatic key management - Use sequential field numbering (0, 1, 2, ...)
- Include comprehensive documentation
- Implement helper methods for computed properties
- Handle null values appropriately
🎉 Summary
- ✅ 25 data models created
- ✅ 21 enum types defined
- ✅ All database tables mapped
- ✅ Complete JSON serialization/deserialization
- ✅ Comprehensive helper methods
- ✅ Full documentation
- ✅ Type-safe implementations
- ✅ Clean architecture compliant
The Worker app now has a complete, production-ready Hive CE local database implementation for offline-first functionality and API response caching!
📚 Reference Documents
HIVE_MODELS_REFERENCE.md- Detailed reference and templates/Users/ssg/project/worker/database.md- Original database schema/Users/ssg/project/worker/lib/core/constants/storage_constants.dart- Type IDs and constants/Users/ssg/project/worker/lib/core/database/models/enums.dart- All enum definitions
Generated: 2025-10-24 Status: ✅ Complete and ready for code generation