Entity Relationship Diagram
Dokumentasi lengkap database schema dengan ERD (Entity Relationship Diagram) menggunakan Mermaid.🎯 Database Overview
MStore menggunakan MySQL 9.3.0 sebagai primary database dengan struktur multi-tenant merchant-scoped. Total Tables: 120 tablesTotal Foreign Keys: 209 relationships
Migration Tool: Atlas HCL
ORM: GORM v1.30
📊 Domain Architecture
Database MStore diorganisir dalam 12 domain utama:| Domain | Tables | Description |
|---|---|---|
| Core Multi-Tenant | 8 | Merchants, Branches, Users, Roles |
| Transaction & POS | 12 | Transactions, Payments, Cashier Shifts |
| Inventory Management | 15 | Inventory, Warehouse, Stock, Movements |
| Product & Catalog | 10 | Products, Bundles, Prices, Addons |
| Procurement | 8 | Purchase Orders, Receivings, Suppliers |
| Production | 7 | Production Orders, Consumptions, Results |
| Accounting | 12 | Chart of Accounts, Journal Entries, AP/AR |
| Approval System | 5 | Approval Flows, Requests, Logs |
| Promotion & Pricing | 7 | Promotions, Conditions, Rewards |
| Banking & Finance | 6 | Bank Reconciliation, Mutations |
| Audit & Logging | 8 | Audit Logs, Activity Logs, Notifications |
| Supporting | 22 | Regions, Tax Rates, Config, Lookup |
📊 Core Entities
1. Multi-Tenant Core
2. Transaction Flow
3. Inventory & Warehouse
4. Inventory Transfer
5. Product & Bundle
6. Approval Flow
7. Accounting & Journal
🔑 Key Relationships
Multi-Tenant Isolation
Every table hasmerchant_id untuk data isolation:
Foreign Key Constraints
Enforced at Database Level:Unique Constraints
Merchant-Scoped Uniqueness:📈 Indexing Strategy
Performance Indexes
🔍 Common Queries
1. Get Branch Stock Summary
2. Transaction with Items
3. Pending Approvals for User
4. Inventory Movement History
🛠️ Migration Management
Atlas HCL Structure
Running Migrations
📊 Database Statistics
Table Sizes (Estimated)
| Table | Estimated Rows | Growth Rate |
|---|---|---|
| transactions | 1M+ | High (daily) |
| transaction_items | 5M+ | High (daily) |
| inventory_movements | 10M+ | High (daily) |
| warehouse_stocks | 100K | Medium |
| inventory | 50K | Low |
| products | 100K | Low |
| approval_requests | 500K | Medium |
| approval_logs | 2M+ | Medium |
| payments | 1M+ | High (daily) |
Storage Requirements
Per Merchant (Average):- Transactions: ~500MB/year
- Inventory Movements: ~1GB/year
- Audit Logs: ~200MB/year
- Total: ~2GB/year per merchant
- Transactional data: 3 years
- Audit logs: 7 years
- Archived data: S3/cold storage
💡 Best Practices
DO ✅
- Always filter by
merchant_idfirst - Use composite indexes untuk common queries
- Implement soft deletes (
deleted_at) - Use GORM hooks untuk audit fields
- Implement database connection pooling
- Use prepared statements (GORM default)
- Monitor slow query log
- Regular ANALYZE TABLE untuk statistics
DON’T ❌
- Jangan skip
merchant_idfilter - Jangan gunakan SELECT * di production
- Jangan skip indexes untuk FK
- Jangan hardcode IDs
- Jangan skip transaction untuk atomic operations
- Jangan allow NULL di FK columns
- Jangan skip backup strategy