Database Architecture Overview
Firebase Realtime Database
Used for real-time driver tracking, order requests, and location data that requires low-latency updates.
Cloud Firestore
Handles chat messages, user profiles, and data requiring complex queries and better offline support.
SQLite (Local)
Stores order history locally for quick access and offline viewing.
Firebase Realtime Database Schema
The Realtime Database is structured as a JSON tree with the following root-level nodes:Schema Structure
Drivers Collection
Stores real-time driver information and availability.Drivers Schema Details
Drivers Schema Details
Path:
Example:
/Drivers/<driverId>| Field | Type | Description |
|---|---|---|
displayName | string | Driver’s full name |
phoneNo | string | Contact phone number |
id | string | Unique driver identifier |
lat | number | Current latitude position |
long | number | Current longitude position |
status | boolean | Availability status (true = available) |
requests | object | Pending delivery requests |
Driver locations are updated in real-time via Firebase listeners in
map.dart:49. The app listens to changes at /Drivers/<driverId> to track driver movement during deliveries.Buyers Collection
Stores buyer/customer data and their order history.Buyers Schema Details
Buyers Schema Details
Path:
Example:
/buyers/<userId>| Field | Type | Description |
|---|---|---|
displayName | string | Buyer’s name |
phoneNo | string | Contact phone number |
lat | number | Current/last known latitude |
long | number | Current/last known longitude |
deviceToken | string | FCM token for push notifications |
requests | object | Active delivery requests |
transit | object | Orders currently in delivery |
Order Object Structure
Orders are stored in both driver and buyer nodes with consistent structure:O:<timestamp>
Example: O:1678901234567
Database Operations
Cloud Firestore Schema
Firestore is used for structured data requiring complex queries and better offline support.Collections Structure
Users Collection
Users Collection Schema
Users Collection Schema
Path:
Example Document:
/users/<userId>| Field | Type | Description |
|---|---|---|
displayName | string | User’s full name |
phoneNo | string | Phone number |
photoUrl | string | Profile picture URL |
pushToken | string | FCM device token |
aboutMe | string | User bio/description |
chattingWith | string | Currently chatting with user ID |
Drivers Collection (Firestore)
Mirrors some driver data from Realtime Database for chat functionality. Path:/drivers/<driverId>
| Field | Type | Description |
|---|---|---|
displayName | string | Driver’s name |
phoneNo | string | Contact number |
photoUrl | string | Profile picture |
status | boolean | Online/offline status |
Messages Collection
Stores all chat messages between buyers and drivers.Messages Collection Schema
Messages Collection Schema
Path: Message Document:
Example:
/messages/<groupChatId>/<groupChatId>/<messageId>Group Chat ID Format: Messages are grouped by a composite key created from sender and receiver IDs:chat.dart
| Field | Type | Description |
|---|---|---|
idFrom | string | Sender’s user ID |
idTo | string | Recipient’s user ID |
timestamp | string | Message timestamp (milliseconds) |
content | string | Message content (text/URL) |
type | number | Message type (0=text, 1=image, 2=sticker) |
Firestore Operations
Local SQLite Storage
Trackmart uses SQLite for local storage of order history, enabling offline access and faster loading.History Table Schema
REQUESTED- Order placed, awaiting driver acceptanceTRANSIT- Order accepted, delivery in progressDELIVERED- Order completed
SQLite Operations
Data Synchronization
Realtime Database ↔ Firestore Sync
Certain data is mirrored between Realtime Database and Firestore:Token Updates
FCM tokens are stored in both
/users/<userId>/deviceToken (RTDB) and /users/<userId>/pushToken (Firestore).Cloud ↔ Local Sync
Order history is synced from Firebase to local SQLite:home_page.dart
Database Indexes
Realtime Database Indexes
Required indexes in Firebase Console:firebase-indexes.json
Firestore Indexes
Composite indexes:- Collection:
messages/<groupChatId>/<groupChatId>- Fields:
timestamp(Descending) - Query scope: Collection
- Fields:
Data Retention
Security Considerations
Firebase Rules
Implement proper security rules to restrict access:
- Users can only read/write their own data
- Drivers can only update their own location
- Messages require authentication
Data Encryption
- Use Firebase Authentication for secure access
- Enable SSL/TLS for all connections
- Store sensitive tokens in secure storage
Related Documentation
Architecture Overview
Learn about the overall app architecture
State Management
Understand state management patterns
API Integration
Explore external API usage