Skip to content

Commit 6d95b5a

Browse files
Copilotgraknol
andcommitted
Add advanced query builder demo and comprehensive documentation
Co-authored-by: graknol <1364029+graknol@users.noreply.github.com>
1 parent fc6c7e7 commit 6d95b5a

File tree

3 files changed

+857
-7
lines changed

3 files changed

+857
-7
lines changed

example/README.md

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
# 🏭 Shop Floor & Advanced Query Demo Applications
2+
3+
This directory contains comprehensive demonstration applications showcasing the full power of `declarative_sqlite` for building offline-first collaborative data sync applications.
4+
5+
## 🚀 Demo Applications
6+
7+
### 1. Shop Floor Console Demo (`shop_floor_console_demo.dart`)
8+
9+
A complete demonstration of offline-first collaborative data synchronization for a shop floor/order management system.
10+
11+
**Run the demo:**
12+
```bash
13+
dart run example/shop_floor_console_demo.dart
14+
```
15+
16+
**Key Features Demonstrated:**
17+
-**Order Management** with reactive filtering and search
18+
-**Collaborative Notes** with GUID-based offline-first creation
19+
-**Conflict Resolution** using Last-Write-Wins timestamps
20+
-**Offline-First Workflow** with sync operation tracking
21+
-**GUID Primary Keys** for collision-free offline data creation
22+
23+
### 2. Advanced Query Builder Demo (`advanced_query_builder_demo.dart`)
24+
25+
A sophisticated demonstration of complex database query patterns using declarative_sqlite's query capabilities.
26+
27+
**Run the demo:**
28+
```bash
29+
dart run example/advanced_query_builder_demo.dart
30+
```
31+
32+
**Key Features Demonstrated:**
33+
-**Basic & Advanced Filtering** with complex WHERE conditions
34+
-**Aggregation Queries** with COUNT, SUM, AVG, MIN, MAX
35+
-**Join Operations** including INNER JOIN, LEFT JOIN, three-way joins
36+
-**Subquery Patterns** with correlated subqueries and EXISTS clauses
37+
-**View-Style Queries** with CTEs and analytical functions
38+
39+
### 3. Flutter Demo App (`flutter_demo/`)
40+
41+
A complete Flutter application demonstrating professional UI patterns with offline-first data sync.
42+
43+
**Features:**
44+
- 📱 **Material Design 3** UI with reactive updates
45+
- 🔄 **Real-time Data Streams** using StreamBuilder
46+
- 🎯 **Advanced Filtering** with status, priority, and search
47+
- 📝 **Collaborative Notes** with offline creation and sync tracking
48+
- 🏗️ **Clean Architecture** with separated concerns
49+
50+
## 🎯 Scenarios Demonstrated
51+
52+
### Offline-First Collaborative Data Sync
53+
54+
All demos showcase production-ready patterns for building offline-first applications:
55+
56+
1. **Client-First Data Creation**
57+
- GUIDs generated client-side to prevent ID collisions
58+
- Data created without server connectivity
59+
- Sync operations tracked for later server upload
60+
61+
2. **Conflict Resolution**
62+
- Last-Write-Wins (LWW) timestamps for concurrent edits
63+
- Conflict detection and resolution strategies
64+
- Multi-device collaboration scenarios
65+
66+
3. **Reactive Data Access**
67+
- Stream-based UI updates with real-time synchronization
68+
- Query builder integration for complex filtering
69+
- Type-safe database operations
70+
71+
4. **Professional Architecture**
72+
- Declarative schema management with automatic migration
73+
- Centralized database service with global access
74+
- Clean separation of concerns and reusable components
75+
76+
## 📊 Database Schema Examples
77+
78+
### Shop Floor Schema
79+
```dart
80+
SchemaBuilder()
81+
// Orders with GUID primary keys
82+
.table('orders', (table) => table
83+
.text('id', (col) => col.primaryKey()) // UUID for offline-first
84+
.text('order_number', (col) => col.notNull().unique())
85+
.text('customer_name', (col) => col.notNull())
86+
.text('status', (col) => col.notNull().withDefaultValue('pending'))
87+
.real('total_amount', (col) => col.withDefaultValue(0.0))
88+
.text('priority', (col) => col.withDefaultValue('medium'))
89+
.index('idx_order_status', ['status']))
90+
91+
// Notes with collaborative editing support
92+
.table('notes', (table) => table
93+
.text('id', (col) => col.primaryKey()) // UUID for offline-first
94+
.text('order_id', (col) => col.notNull())
95+
.text('content', (col) => col.notNull())
96+
.text('author', (col) => col.notNull())
97+
.integer('is_synced', (col) => col.withDefaultValue(0)) // Sync tracking
98+
.text('created_on_device')) // Device attribution
99+
```
100+
101+
### Advanced Query Examples
102+
```dart
103+
// Complex aggregation with joins
104+
final revenueByRegion = await database.rawQuery('''
105+
SELECT
106+
c.region,
107+
COUNT(o.id) as order_count,
108+
SUM(o.total_amount) as total_revenue,
109+
AVG(o.total_amount) as avg_order_value
110+
FROM customers c
111+
JOIN orders o ON c.id = o.customer_id
112+
WHERE o.status = 'completed'
113+
GROUP BY c.region
114+
ORDER BY total_revenue DESC
115+
''');
116+
117+
// Subquery patterns
118+
final topCustomers = await database.rawQuery('''
119+
SELECT company_name, industry, region
120+
FROM customers
121+
WHERE id IN (
122+
SELECT customer_id
123+
FROM orders
124+
WHERE total_amount > (
125+
SELECT AVG(total_amount) FROM orders WHERE status = 'completed'
126+
)
127+
)
128+
''');
129+
```
130+
131+
## 🛠️ Technical Highlights
132+
133+
### Declarative Schema Management
134+
- **Type-Safe Schema Definition**: Define database structure with Dart code
135+
- **Automatic Migration**: Handle schema changes seamlessly
136+
- **Index Management**: Optimize query performance with declarative indexes
137+
- **Constraint Support**: Primary keys, unique constraints, and NOT NULL validation
138+
139+
### Advanced Data Access
140+
- **Query Builder Integration**: Complex filtering with type safety
141+
- **Stream-Based Updates**: Reactive UI patterns with automatic data refresh
142+
- **Bulk Operations**: Efficient handling of large datasets
143+
- **Error Handling**: Comprehensive error management with user feedback
144+
145+
### Offline-First Architecture
146+
- **GUID Generation**: Collision-free client-side ID creation
147+
- **Sync Operation Tracking**: Monitor operations requiring server sync
148+
- **Conflict Resolution**: LWW timestamps for collaborative editing
149+
- **Device Attribution**: Track which device/user made changes
150+
151+
### Professional UI Patterns (Flutter Demo)
152+
- **Material Design 3**: Modern Flutter UI with consistent theming
153+
- **Reactive Architecture**: StreamBuilder integration for real-time updates
154+
- **Component Separation**: Reusable widgets with clean interfaces
155+
- **State Management**: Proper state handling with global database service
156+
157+
## 🎮 How to Use the Demos
158+
159+
### Quick Start
160+
1. **Run the Console Demo**: See immediate results in your terminal
161+
```bash
162+
dart run example/shop_floor_console_demo.dart
163+
```
164+
165+
2. **Explore Advanced Queries**: Learn complex SQL patterns
166+
```bash
167+
dart run example/advanced_query_builder_demo.dart
168+
```
169+
170+
3. **Study the Flutter App**: Examine professional UI implementation
171+
```bash
172+
cd example/flutter_demo
173+
# Examine the code structure and patterns
174+
```
175+
176+
### Learning Path
177+
1. **Start with Console Demo**: Understand core concepts and data flow
178+
2. **Explore Query Builder**: Learn advanced database query patterns
179+
3. **Study Flutter Implementation**: See how to build production UI
180+
4. **Examine Schema Design**: Understand offline-first database patterns
181+
182+
## 📚 Key Learning Outcomes
183+
184+
### Database Design
185+
- Design schemas for offline-first collaborative applications
186+
- Use GUIDs effectively for distributed data creation
187+
- Implement conflict resolution strategies
188+
- Create efficient indexes for query performance
189+
190+
### Application Architecture
191+
- Structure applications for offline-first scenarios
192+
- Implement reactive data access patterns
193+
- Design clean separation of concerns
194+
- Handle complex query requirements
195+
196+
### User Experience
197+
- Build responsive UIs that update automatically
198+
- Provide clear feedback for sync status
199+
- Handle error conditions gracefully
200+
- Support collaborative editing workflows
201+
202+
## 🔬 Production Readiness
203+
204+
These demos showcase production-ready patterns suitable for:
205+
206+
- **Industrial Applications**: Shop floor management, inventory tracking
207+
- **Collaborative Tools**: Multi-user editing, real-time updates
208+
- **Mobile Applications**: Offline-first mobile apps with sync
209+
- **Enterprise Software**: Complex data relationships and reporting
210+
- **IoT Systems**: Device data collection and synchronization
211+
212+
The patterns demonstrated here have been designed to scale from prototype to production with minimal changes, providing a solid foundation for building sophisticated offline-first applications.
213+
214+
## 🚀 Next Steps
215+
216+
After exploring these demos, consider:
217+
218+
1. **Integrate Server Sync**: Add REST API integration for actual server synchronization
219+
2. **Add User Authentication**: Implement user management and permissions
220+
3. **Extend Schema**: Add more complex relationships and business logic
221+
4. **Performance Optimization**: Implement advanced caching and indexing strategies
222+
5. **Testing Strategy**: Add comprehensive testing for offline scenarios
223+
224+
These demos provide the foundation for building production-grade offline-first collaborative applications with declarative_sqlite.

0 commit comments

Comments
 (0)